<jupyter_start><jupyter_text>Inspect<jupyter_code>import pandas as pd
import numpy as np
#need the encoding set to "ISO-8859-1" so it will read the weird characters properly 
movies = pd.read_csv('https://raw.githubusercontent.com/afroman32/Unit-2-Build/master/movies.csv',
                     encoding = "ISO-8859-1")
print(movies.shape)
movies.head()
movies['score'].describe()<jupyter_output><empty_output><jupyter_text>Wrangle<jupyter_code># director track record, movies success by director
# make sure to train on features that would come before the target
# number of unique directors
movies['director'].nunique()
# create a list of directors
directors = list(movies['director'])

# remove the duplicates from the list
directors = list(dict.fromkeys(directors))

# check length of directors, should be 2759 to match unique directors
print(len(directors))
type(directors)
# test to see average for directors
sub = movies[movies['director'] == movies['director'][3]]
sub['gross'].sum()/len(sub['gross'])
ave_gross = []

# run through each director and average the movie gross revenue
for i in range(0,len(directors)):

  # create a subset of all directors in index i
  sub = movies[movies['director'] == directors[i]]

  # average the gross revenues of each movie
  average=sub['gross'].sum()/len(sub['gross'])

  # cast float to int to remove decimal points then add it to ave_gross list
  ave_gross.append(int(average))

# check if ave_gross has correct number of values
print(len(ave_gross))
print(directors[i])
sub
movies[movies['director']==directors[-2]]
ave_gross[-1]
# create a dictionary with directors and their average gross
dir_ave = {directors[i]: ave_gross[i] for i in range(len(directors))}
len(dir_ave)
print(directors[0])
res = list(dir_ave.keys()).index('<NAME>') 
res
# pass the dictionary to the movies dataframe
movies['dir_ave_gross']= movies['director'].map(dir_ave)
movies.head()
# number of unique writers
movies['writer'].nunique()
# create a list of writers
writers = list(movies['writer'])

# remove the duplicates from the list
writers = list(dict.fromkeys(writers))

# check length of writers, should be 4199 to match unique writers
print(len(writers))
type(writers)
# test to see average for random writer
sub = movies[movies['writer'] == movies['writer'][3]]
sub['gross'].sum()/len(sub['gross'])
writer_gross = []

# run through each writers and average the movie gross revenue
for i in range(0,len(writers)):
  # create a subset of all writers in index i
  sub = movies[movies['writer'] == writers[i]]

  # average the gross revenues of each movie
  average=sub['gross'].sum()/len(sub['gross'])

  # cast float to int to remove decimal points then add it to ave_gross list
  writer_gross.append(int(average))

# check if writer_gross has correct number of values, expect 4199
len(writer_gross)
# create a dictionary with writers and their average gross
dir_ave = {writers[i]: writer_gross[i] for i in range(len(writers))}
# pass the dictionary to the movies dataframe
movies['writer_ave_gross']= movies['writer'].map(dir_ave)
movies.head()<jupyter_output><empty_output><jupyter_text>Train, Val, Test Split (60,20,20)<jupyter_code>movies.sort_values(by = 'released', inplace = True)
movies.head()
movies.reset_index(inplace = True, drop  = True)
movies.head()
# set end year for training data set
train_lim = movies['year'].min()+19

# set beginning year for test data
test_lim = movies['year'].max()-4
# movies in years less than training end year
train = movies[movies['year']<= train_lim]

# all movies in years greater than training end year
val_temp = movies[movies['year'] > train_lim]

# movies in years less than test year and greater than training end year
val = val_temp[val_temp['year'] < test_lim]

# movies in years greater than test year
test = movies[movies['year']>=test_lim]
train.shape, val.shape, test.shape<jupyter_output><empty_output><jupyter_text>Assignment 1 Baseline<jupyter_code>average = movies['gross'].mean()
print(f'{average:,.0f}')
from sklearn.metrics import mean_absolute_error

train['gross'].mean()

y_train = train['gross']
y_test = test['gross']
base_guess = y_train.mean()


# test error
y_base_pred = [base_guess] * len(y_test)
mae_test = mean_absolute_error(y_test, y_base_pred)

print(f'Mean Absolute Error: ${mae_test:,.2f}')<jupyter_output>Mean Absolute Error: $45,726,662.13<jupyter_text>Basic Model<jupyter_code>!pip install category_encoders==2.*
# set target
target = 'gross'

# set features
features = ['genre', 'rating', 'year', 'dir_ave_gross', 'writer_ave_gross']

# set X matrices and y vectors
X_train = train[features]
y_train = train[target]

X_val = val[features]
y_val = val[target]

X_test = test[features]
y_test = test[target]
import category_encoders as ce
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
from sklearn.pipeline import make_pipeline
# from math import sqrt

pipeline = make_pipeline(
    ce.OrdinalEncoder(),
    # SimpleImputer(strategy = 'mean'),
    StandardScaler(),
    LinearRegression()
)

# Fit on training set
pipeline.fit(X_train, y_train)

# make predictions
y_pred = pipeline.predict(X_test)

# test mean absolute error
mae = mean_absolute_error(y_test, y_pred)
print(f'Test Mean Absolute Error: {mae:,.2f}')<jupyter_output>Test Mean Absolute Error: 19,734,155.80
StarcoderdataJupyterStructuredCleanDedup