11 KiB
11 KiB
In [ ]:
import pandas as pd
# Numerical or binary attributes
num_or_bin= ['Income', 'Kidhome','Recency', 'MntWines', 'MntFruits', 'MntMeatProducts', 'MntFishProducts', 'MntSweetProducts','MntGoldProds', 'NumDealsPurchases', 'NumWebPurchases',
'NumCatalogPurchases', 'NumStorePurchases', 'NumWebVisitsMonth','AcceptedCmp3', 'AcceptedCmp4', 'AcceptedCmp5', 'AcceptedCmp1','AcceptedCmp2']
# Categorical attributes
cat = ['Education','Marital_Status']
# Load the data source
pd.set_option('display.max_columns', None)
df = pd.read_csv("https://raw.githubusercontent.com/rpi-techfundamentals/website_fall_2021/master/site/public/marketing_campaign.csv", delimiter='\t')
df.head()In [ ]:
raise NotImplementedError()
# YOUR CODE HEREIn [ ]:
# Answer
'''
all_cols=num_or_bin+cat
X=pd.get_dummies(df[all_cols],columns=cat,drop_first=True)
X.loc[X['Income'].isnull(),'Income']=X['Income'].median()
'''In [ ]:
#Please do not modify/delete this cell
# 1 Test Cell
#this tests the columns.
ecol = set(['Income', 'Kidhome', 'Recency', 'MntWines', 'MntFruits',
'MntMeatProducts', 'MntFishProducts', 'MntSweetProducts',
'MntGoldProds', 'NumDealsPurchases', 'NumWebPurchases',
'NumCatalogPurchases', 'NumStorePurchases', 'NumWebVisitsMonth',
'AcceptedCmp3', 'AcceptedCmp4', 'AcceptedCmp5', 'AcceptedCmp1',
'AcceptedCmp2', 'Education_Basic', 'Education_Graduation',
'Education_Master', 'Education_PhD', 'Marital_Status_Alone',
'Marital_Status_Divorced', 'Marital_Status_Married',
'Marital_Status_Single', 'Marital_Status_Together',
'Marital_Status_Widow', 'Marital_Status_YOLO'])
assert len(ecol-set(X.columns))==0
assert len(set(X.columns))-len(ecol)==0In [ ]:
#Please do not modify/delete this cell
# 2 Test Cell
#this tests the columns.
assert X.shape==(2240, 30)
q2=X.copy()
q2['Response']=df['Response']
q2['Year_Birth']=df['Year_Birth']
q2['Dt_Customer']=df['Dt_Customer']
q2.to_csv('q2.csv',index=False)In [ ]:
#Please do not modify/delete this cell
# 3 Test Cell
assert len(X.loc[X['Income']==51381.5,'Income'])==24In [ ]:
#Please do not modify/delete this cell
# 4 Test Cell
assert X.isnull().sum().sum()==0In [ ]:
# Reload data
import pandas as pd
df_cl=pd.read_csv('q2.csv')In [ ]:
raise NotImplementedError()
# YOUR CODE HEREIn [ ]:
# Answer
'''
df_cl['age']=(2021-df_cl['Year_Birth']).astype(int)
df_cl['Dt_Customer_tmp']=df_cl['Dt_Customer'].apply(lambda x:x[3:5])
df_cl.loc[df_cl['Dt_Customer_tmp']=='12','holiday']=1
df_cl.loc[df_cl['Dt_Customer_tmp']!='12','holiday']=0
df_cl['holiday']=df_cl['holiday'].astype(int)
'''In [ ]:
#Please do not modify/delete this cell
# 5 Test Cell
assert df_cl['age'].sum()==116915In [ ]:
#Please do not modify/delete this cell
# 6 Test Cell
import numpy as np
assert isinstance(df_cl['age'][0], (np.int64,np.int32))In [ ]:
#Please do not modify/delete this cell
# 7 Test Cell
assert df_cl['holiday'].sum()==175In [ ]:
#Please do not modify/delete this cell
# 8 Test Cell
import numpy as np
assert isinstance(df_cl['holiday'][0], (np.int64,np.int32))In [ ]:
import pandas as pd
df_cl2=pd.read_csv('dfcl2.csv')
y_reload=df_cl2['Response']
X_reload=df_cl2.drop(['Response'], axis=1)In [ ]:
raise NotImplementedError()
# YOUR CODE HEREIn [ ]:
# Answer
'''
y_reload=df_cl2['Response']
X_reload=df_cl2.drop(['Response'], axis=1)
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X_reload,y_reload,
train_size=0.5,
test_size=0.5,
random_state=100,
stratify=y_reload)
'''In [ ]:
#Please do not modify/delete this cell
# 9 Test Cell
assert X_train.shape==(1120, 31)
assert X_test.shape==(1120, 31)In [ ]:
#Please do not modify/delete this cell
# 10 Test Cell
assert y_train.sum()==y_test.sum()In [ ]:
import pandas as pd
df_cl2=pd.read_csv('dfcl2.csv')
y_reload=df_cl2['Response']
X_reload=df_cl2.drop(['Response'], axis=1)
X_train2, X_test2, y_train2, y_test2 = train_test_split(X_reload, y_reload, test_size=0.3, random_state = 100)In [ ]:
from sklearn import metrics
from sklearn.ensemble import GradientBoostingClassifier
#use this classifier
classifier = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)In [ ]:
raise NotImplementedError()
# YOUR CODE HEREIn [ ]:
# Answer
'''
classifier.fit(X_train2,y_train2)
test_y_pred=classifier.predict(X_test2)
test_y_pred_prob=classifier.predict_proba(X_test2)[:,1]
test_acc=metrics.accuracy_score(y_test2,test_y_pred)
test_roc=metrics.roc_auc_score(y_test2,test_y_pred_prob)
'''In [ ]:
#Please do not modify/delete this cell
# 11 Test Cell
assert round(test_acc,3)==0.860In [ ]:
#Please do not modify/delete this cell
# 12 Test Cell
assert round(test_roc,3)==0.846