Path: blob/master/lessons/lesson_03/code/distributions and dummy variables (done).ipynb
1904 views
Lesson 3: Demos
Although the mean and median both give us some sense of the center of a distribution, they aren't always the same. The median gives us a value that splits the data into two halves while the mean is a numeric average, so extreme values can have a significant impact on the mean.
In a symmetric distribution, the mean and median will be the same. Let's investigate with a density plot:
In the plot above, the mean and median are both so close to zero that the red median line lies on top of the thicker black line drawn at the mean.
In skewed distributions, the mean tends to get pulled in the direction of the skew, while the median tends to resist the effects of skew:
Notice that the mean is also influenced heavily by outliers, while the median resists the influence of outliers:
Since the median tends to resist the effects of skewness and outliers, it is known as a "robust" statistic.
The median generally gives a better sense of the typical value in a distribution with significant skew or outliers.
unclear what this is cell is...
Skewness and Kurtosis
Skewness measures the skew or asymmetry of a distribution while Kurtosis measures the "peakedness" of a distribution.
We won't go into the exact calculations behind these, but they are essentially just statistics that take the idea of variance a step further: while variance involves squaring deviations from the mean, skewness involves cubing deviations from the mean, and kurtosis involves raising deviations from the mean to the 4th power.
Pandas has built in functions for checking skewness and kurtosis, df.skew() and df.kurt() respectively:
To explore these two measures further, let's create some dummy data and inspect it:
Types of distributions
All together
Skewness
Now let's check the skewness of each of these distributions.
Since skewness measures asymmetry, we'd expect to see low skewness for all of the distributions except the skewed one, because all the others are roughly symmetric:
Kurtosis
Now let's check kurtosis. Since kurtosis measures peakedness, we'd expect the flat (uniform) distribution to have low kurtosis while the distributions with sharper peaks should have higher kurtosis.
As we can see from the output, the normally distributed data has a kurtosis near zero, the flat distribution has negative kurtosis, and the two pointier distributions have positive kurtosis.
Class Variable Demo
Class/Dummy Variables
We want to represent categorical variables numerically, but we can't simply code them as 0=rural, 1=suburban, 2=urban because that would imply an ordered relationship between suburban and urban (suggesting that urban is somehow "twice" the suburban category, which doesn't make sense).
Why do we only need two dummy variables, not three? Because two dummies capture all of the information about the Area feature, and implicitly defines rural as the reference level.
In general, if you have a categorical feature with k levels, you create k-1 dummy variables.
Handling Categorical Predictors with Two Categories
Up to now, all of our predictors have been numeric. What if one of our predictors was categorical?
Let's create a new feature called "Size," and randomly assign observations to be small or large:
For scikit-learn, we need to represent all data numerically.
If the feature only has two categories, we can simply create a dummy variable that represents the categories as a binary value.
Handling Categorical Predictors with More than Two Categories
Let's create a new feature called Area, and randomly assign observations to be rural, suburban, or urban:
We have to represent Area numerically, but we can't simply code it as 0=rural, 1=suburban, 2=urban because that would imply an ordered relationship between suburban and urban (and thus urban is somehow "twice" the suburban category).
Instead, we create another dummy variable:
Create three dummy variables using get_dummies, then exclude the first dummy column
my_categorical_var_dummies = pd.get_dummies(my_categorical_var, prefix='Area').iloc[:, 1:]