Data Science

Get Started. It's Free
or sign up with your email address
Data Science by Mind Map: Data Science

1. Machine Learning

1.1. Types of Machine Learning

1.1.1. Human Supervision Supervised; training data you feed algo includes desired solutions, called labels Classification; train with examples of spam or ham email. Must learn how to classify new emails Regression; predict a target numeric value (price of car), given features (milage, age, brand, etc.) called predictors. (Some regression algos can be used for classification) k-Nearest Neighbors Support Vector Machines (SVMs) Decision Trees and Random Forests Neural Networks (*can also be unsupervised and semisupervised) Unsupervised; the training data is not labeled; learn without a teacher Clustering Visualization and dimensionality reduction; Visualization - output a 2d/3d representation of data to preserve as much structure as they can (e.g., trying to keep separate clusters in the input space from overlapping); Dimensionality reduction - goal is to simplify the data without losing too much info (can merge several correlated features into one, called feature extraction) => good idea to reduce dimension of training data using a dimensionality reduction algo before feeding it to another ML algo. 1. Will runfaster 2. Take up less memory 3. May perform better in some cases Association rule learning; the goal is to dig into large amounts of data and discover interesting relations between attributes Anomoly detection; automatically remove outliers from a dataset before feeding to another algo; system is trained with normal instances, when it sees a new one it can tell if its normal or an anomaly Semisupervised; partially labeled training data => usually a lot of unlabeled data and a little bit of labeled data (cluster similar looking pictures, just need to label the person) Usually are combinations of unsupervised and supervised algos Deep belief networks (DBNs); Based on unsupervised components called restricted Boltzmann machines (RBMs) stacked on top of one another, then fine-tuned using supervised learning techniques Reinforcement Learning; Agent => Learning system, it can observe the environment, select and perform actions and get rewards or penalties in return. Policy => what the best strategy the algo learns by itself to get the most reward over time. A policty defines what action the agent should choose when it is in a given situation For example; Robots learn how to walk, DeepMind's AlphaGo program

1.1.2. Learning Style Online/Batch Learning Batch learning; Incrementally learn Online learning; Learn on the fly from a stream of incoming data Instance-Based/Model-Based; (How well ML systems generalize; ML is about making predictions, with new data never seen before needs to be able to generalize) Instance-Based Model-Based

1.2. Main challenges of machine learning; (Main task is to select a learning algo and train it on some data => two things that can go wrong are 'bad data' and 'bad algo'

1.2.1. Bad Data Insufficient Quantity of Training Data Simple problems need thousands of examples Complex problems like image and speech recognition need millions of examples (unless reusing parts of an existing model) In a famous paper published in 2001, Microsoft researchers Michele Banko and Eric Brill showed that very different Machine Learning algorithms, including fairly simple ones, performed almost identically well on a complex problem of natural language disambiguation8 once they were given enough data. As the authors put it: “these results suggest that we may want to reconsider the trade- off between spending time and money on algorithm development versus spending it on corpus development.” The idea that data matters more than algorithms for complex problems was further popularized by Peter Norvig et al. in a paper titled “The Unreasonable Effectiveness of Data” published in 2009.10 It should be noted, however, that small- and medium- sized datasets are still very common, and it is not always easy or cheap to get extra training data, so don’t abandon algorithms just yet. Nonrepresentative Training Data To generalize well, need training data to be representative of new cases you want to generalize to. (True for instance and model-based learning) Too small sample size you will have sampling noise (nonrepresentative data as a result of chance). Large data samples can be nonrepresentative if the sampling method is flawed. Called sampling bias.

1.2.2. Bad Algo

1.3. Terms

1.3.1. Attribute; data type (mileage)

1.3.2. Feature; general means an attribute plus its value (Mileage = 15,000) (can use feature and attribute interchangeably)

1.3.3. Feature extraction; a type of dimensionality reduction, the goal is to simplify the data without losing too much info but merging several correlated features into one

1.3.4. Structured Data;

1.3.5. Unstructured Data;

1.4. Steps for the process

1.4.1. Data Preparation feature selection train/test splittiing sampling

1.5. Feature Engineering;

1.5.1. 1. Imputation (what to do with missing values) (Some algos drop rows, some dont accept datasets with missing values) Drop based on a threshold (70% of values not in then drop row) Numerical Imputation Medians of the columns; Best solution (Average of the columns are sensitive to outliers) Have a default value of missing numbers (Can missing values assume to be zero/NA?) Interpolation methods Categorical Imputation Replace missing values with the maximum occurred value in a column (need to check distribution though => if uniformly distributed and no dominant value, inputting a category like 'Other' might be more sensible since imputation is likely to converge to a random selection).

1.5.2. 2. Handling Outliers; Best way to detect outliers is to visualize the outliers Statistical Methodology; Less precise but are fast Standard Deviation; no trivial solution, but 2 to 4 seems practical Percentiles; set percentage value depending on distribution of data Outlier Dilemma: Drop or Cap Capping them so you can keep data size, which might be better for the final model performance Capping can effect the distribution of the data, thus not better to exaggerate it.

1.5.3. 3. Binning Can be applied to categorical and numerical data Main motivation is to make the model more robust and prevent overfitting, however, it has a cost to the performance. Every time you bid something => sacrifice info and make data more regularized. For numerical columns, except for obvious overfitting cases, binning might be redundant for some kind of algorithms, due to its effect on model performance. For categorical data, the labels with low frequencies probably affect the robustness of statistical models negatively. So assigning a general category to these less frequent values helps to keep the robustness of the model. #Numerical Binning Example data['bin'] = pd.cut(data['value'], bins=[0,30,70,100], labels=["Low", "Mid", "High"]) value bin 0 2 Low 1 45 Mid 2 7 Low 3 85 High 4 28 Low #Categorical Binning Example Country 0 Spain 1 Chile 2 Australia 3 Italy 4 Brazil conditions = [ data['Country'].str.contains('Spain'), data['Country'].str.contains('Italy'), data['Country'].str.contains('Chile'), data['Country'].str.contains('Brazil')] choices = ['Europe', 'Europe', 'South America', 'South America'] data['Continent'] =, choices, default='Other') Country Continent 0 Spain Europe 1 Chile South America 2 Australia Other 3 Italy Europe 4 Brazil South America

1.5.4. 4. Log Transformation (most commonly used mathematical transformations) Helps handle skewed data => distribution becomes more approx. to normal Magnitude of order changes => want to capture magnitude of changes, not absolute of change Decreases the effect of outliers; due to the normalization of magnitude differences and the model become more robust A critical note: The data you apply log transform must have only positive values, otherwise you receive an error. Also, you can add 1 to your data before transform it. Thus, you ensure the output of the transformation to be positive. (Log (X+1)) #Log Transform Example data = pd.DataFrame({'value':[2,45, -23, 85, 28, 2, 35, -12]}) data['log+1'] = (data['value']+1).transform(np.log) #Negative Values Handling #Note that the values are different data['log'] = (data['value']-data['value'].min()+1) .transform(np.log) value log(x+1) log(x-min(x)+1) 0 2 1.09861 3.25810 1 45 3.82864 4.23411 2 -23 nan 0.00000 3 85 4.45435 4.69135 4 28 3.36730 3.95124 5 2 1.09861 3.25810 6 35 3.58352 4.07754 7 -12 nan 2.48491

1.5.5. 5. One-Hot Encoding (popular encoding methods in ML) This method spreads the values in a column to multiple flag columns and assigns 0 or 1 to them. These binary values express the relationship between grouped and encoded column. Changes Categorical data to numerical data => enables grouping of categorical data without losing info If you have N distinct values in the column, it is enough to map them to N-1 binary columns, because the missing value can be deducted from other columns. (Helps not use multicollinearity). encoded_columns = pd.get_dummies(data['column']) data = data.join(encoded_columns).drop('column', axis=1)

1.5.6. 6. Grouping Operations; In most machine learning algorithms, every instance is represented by a row in the training dataset, where every column show a different feature of the instance. This kind of data called “Tidy”. Datasets such as transactions rarely fit the definition of tidy data above, because of the multiple rows of an instance. The key point of group by operations is to decide the aggregation functions of the features. Numerical features average and sum functions are usually done Can obtain ratio columns, by using the average of binary columns Categorical features Select label with the highest frequency Make a pivot table Applying a group by function after applying one-hot encoding.

1.5.7. 7. Feature Split (good for making features useful for ML) Extract usable parts of string columns Enable ML algos to comprehend them Make possible to bin and group them Improve model performance by uncovering potential info Split function is a good option, however, there is no one way of splitting features. It depends on the characteristics of the column, how to split it. Two examples... This example is a simple split function for an ordinary name column. The example handles the names longer than two words by taking only the first and last elements and it makes the function robust for corner cases, which should be regarded when manipulating strings like that. Another case for split function is to extract a string part between two chars. The following example shows an implementation of this case by using two split functions in a row.

1.5.8. 8. Scaling (most variables do not have the same range => scaling helps machine learning solve this problem and helps the machine learning algo learn better) (not mandatory but best to apply) (algos based on distance calculations like k-NN or k-Means will need to have scaled continuous features as model input) Normalization (or min-max normalization) Scales all values in a fixed range between 0 and 1. Does not change the distribution of the feature and due to the decreases standard deviations, the effects of the outliers increases. So before normalization, it is recommended to handle outliers. x_norm = (x - x_min)/(x_max - x_min) Standardization (or z-score normalization) Scales values while taking into account standard deviation. If the standard deviation of features is different, their range will also differ from each other. Reducing the effect of the outliers in the features. z = (x - mu) /

1.5.9. 9. Extracting Date (dates are sometimes hard to understand by alagos and can be represented in numerous formats) Can extract the parts of the date into different columns: year, month, day, etc. Can Extract the time period between the current date and columns in terms of years months, days, etc. Can Extract some specific features from the date: Name of weekday, weekend or not, holiday or not, etc. from datetime import date data = pd.DataFrame({'date': ['01-01-2017', '04-12-2008', '23-06-1988', '25-08-1999', '20-02-1993', ]}) #Transform string to date data['date'] = pd.to_datetime(, format="%d-%m-%Y") #Extracting Year data['year'] = data['date'].dt.year #Extracting Month data['month'] = data['date'].dt.month #Extracting passed years since the date data['passed_years'] = - data['date'].dt.year #Extracting passed months since the date data['passed_months'] = ( - data['date'].dt.year) * 12 + - data['date'].dt.month #Extracting the weekday name of the date data['day_name'] = data['date'].dt.day_name() date year month passed_years passed_months day_name 0 2017-01-01 2017 1 2 26 Sunday 1 2008-12-04 2008 12 11 123 Thursday 2 1988-06-23 1988 6 31 369 Thursday 3 1999-08-25 1999 8 20 235 Wednesday 4 1993-02-20 1993 2 26 313 Saturday

2. Statistical Modeling

3. Data

3.1. Cleaning Data

3.2. Transform Data