Normalizing Features
You might have noticed that our previous model wasn't exactly stellar. This is because we missed a key technique used in machine learning: normalization. Normalizing features takes all of our data and fits variables to a similar scale and range. When we previously performed gradient descent, the budget and imdbVotes features had a much larger impact on our steps and the resulting output. That's not necessarily due to those features being more predictive of the gross domestic sales, but rather simply because the values of those features were much higher then the imdbRating or Metascore features which had much more narrow ranges. To account for this, we'll start normalizing our data, and transform back to the raw version when needed.
import pandas as pd
%matplotlib inline
df = pd.read_excel('movie_data_detailed_with_ols.xlsx')
df.head()
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
budget | domgross | title | Response_Json | Year | imdbRating | Metascore | imdbVotes | Model | |
---|---|---|---|---|---|---|---|---|---|
0 | 13000000 | 25682380 | 21 & Over | 0 | 2008 | 6.8 | 48 | 206513 | 4.912759e+07 |
1 | 45658735 | 13414714 | Dredd 3D | 0 | 2012 | 0.0 | 0 | 0 | 2.267265e+05 |
2 | 20000000 | 53107035 | 12 Years a Slave | 0 | 2013 | 8.1 | 96 | 537525 | 1.626624e+08 |
3 | 61000000 | 75612460 | 2 Guns | 0 | 2013 | 6.7 | 55 | 173726 | 7.723381e+07 |
4 | 40000000 | 95020213 | 42 | 0 | 2013 | 7.5 | 62 | 74170 | 4.151958e+07 |
1. Basic Norm function
Write a function norm(col) that takes in a pandas series, and rescales the data to have a minimum of zero and a maximum of 1. Think about how you can do this by simply using the minimum and maximum of the column.
def norm(col):
#Define your function here
return None
2. Apply your norm function to the X feature columns
X = df[['budget', 'imdbRating',
'Metascore', 'imdbVotes']]
y = df['domgross']
#Your code here
3. Try writing a slightly different normalization function: the mean normaliztion.
Here's how its defined:
mean_normalized_x =
def norm(col):
#Define your function here
return None