Kaggle competition: House Prices - Advanced Regression Techniques
print("Training dataset(rows, columns):", testandtrain_df.shape)
 print("Validation dataset(rows, columns):", validation_df.shape)
<concatenate>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 print('Train Shape: {}, Train ID Range {} to {}'.format(train.shape,train.Id.min() , train.Id.max()))
print('Test Shape: {}, Test ID Range {} to {}'.format(test.shape,test.Id.min() , test.Id.max()))
temp4_df = pd.concat([testandtrain_df.copy() , validation_df.copy() ], sort = False)
 temp4_df = temp4_df[temp4_df.Fare != 0]
temp4_df.shape 
<concatenate>
all_data = train.append(test,sort = False)
 print(all_data.shape)
 all_data.head(3 )
temp4_df = pd.concat([testandtrain_df.copy() , validation_df.copy() ], sort=False)
 print("Number of passengers in each bin/grouping(out of 1,309 - both dataframes):-
")
 temp4_df.groupby("FareBinned" ).Fare.count()<groupby>
all_data.dtypes.groupby(all_data.dtypes ).count()
testandtrain_df.groupby("FareBinned" ).Survived.agg([("Count", "count"),("Survival(mean)", "mean")], axis = "rows")
<drop_column>
skew_calcs = all_data[numerics].skew().sort_values()
skew_calcs = skew_calcs[skew_calcs >.75]
print(skew_calcs)
 skew_feats = skew_calcs.index
for df in [testandtrain_df, validation_df]:
df.drop(columns = ["Fare"], inplace = True )<groupby>
all_data[skew_feats] = np.log1p(all_data[skew_feats] )
testandtrain_df.groupby("Family_Onboard" ).Survived.agg([("Count", "count"),("Survival(mean)", "mean")], axis = "rows" )<drop_column>
all_data = pd.concat([all_data,pd.get_dummies(all_data[objects])],axis = 1)
 print(all_data.shape )
for df in [testandtrain_df, validation_df]:
df.drop(columns = ["Age", "Family_Onboard"], inplace = True )<categorify>
all_data = all_data.drop(objects,axis = 1)
 print(all_data.shape )
for df in [testandtrain_df, validation_df]:
df['Sex'] = df['Sex'].map({"female": 1, "male": 0} )<categorify>
all_data = all_data.fillna(all_data.mean())
 all_data.head(3 )
dummy_cols = ["Pclass", "Embarked", "Title", "FareBinned", "AgeBinned", "FamilyBinned"]
dummy_prefixes = ["Pclass", "Port", "Title", "Fare", "Age", "Family"]
testandtrain_df = pd.get_dummies(testandtrain_df, columns = dummy_cols, prefix = dummy_prefixes)
 validation_df = pd.get_dummies(validation_df, columns = dummy_cols, prefix = dummy_prefixes )<sort_values>
train_data = all_data[:1460]
test_data = all_data[1460:].drop('SalePrice',axis = 1 )
testandtrain_df.corr() ["Survived"][:].sort_values(ascending = False )<split>
from sklearn.model_selection import train_test_split
print("Survival % split for train.csv data")
 testandtrain_df.Survived.value_counts(normalize = True )<split>
from sklearn.model_selection import train_test_split
X = testandtrain_df.iloc[:, 1:]
y = testandtrain_df.iloc[:, 0]
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify = y, test_size = 0.3, random_state = 0)
<load_from_csv>
X = train_data.drop(['SalePrice'],axis = 1)
 print(X.shape)
 y = train_data['SalePrice']
print(y.shape )
X_train<load_from_csv>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=10 )
X_test<train_model>
def get_mse(model,alpha):
run_model = model(alpha = alpha)
 run_model.fit(X_train,y_train)
 return mean_squared_error(y_pred=run_model.predict(X_test),y_true=y_test )
dt_model = tree.DecisionTreeClassifier(random_state = 0)
 dt_model.fit(X_train, y_train )<compute_test_metric>
pd.DataFrame({'alpha':alphas, 'MSE':ridge_mses} ).sort_values('MSE',ascending = True )
print("Train Accuracy:", dt_model.score(X_train, y_train))
print("Test Accuracy:", dt_model.score(X_test, y_test))<split>
ridge_model = Ridge(alpha = 5)
 ridge_model.fit(X,y)
 coefs = ridge_model.coef_
train_accuracy = []
test_accuracy = []
trial_range = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]
for test_split in trial_range:
temp_X_train, temp_X_test, temp_y_train, temp_y_test = train_test_split(X, y, stratify = y, test_size = test_split, random_state=1)
 temp_dt = tree.DecisionTreeClassifier(random_state = 0)
 temp_dt.fit(temp_X_train, temp_y_train)
 train_accuracy.append(temp_dt.score(temp_X_train, temp_y_train))
test_accuracy.append(temp_dt.score(temp_X_test, temp_y_test))
testtrainsplit_df = pd.DataFrame({"test_split": trial_range, "train_acc": train_accuracy, "test_acc": test_accuracy})
 plt.figure(figsize =(20,5))
plt.plot(testtrainsplit_df["test_split"], testtrainsplit_df["train_acc"], marker="o")
 plt.plot(testtrainsplit_df["test_split"], testtrainsplit_df["test_acc"], marker="o")
 plt.xlabel("Ratio of test data(vs train data)")
 plt.xticks(trial_range)
 plt.ylabel("Model accuracy score")
 plt.title("Decision Tree model accuracy with different test:train ratios")
 plt.legend()<train_model>
labels_and_weights = pd.DataFrame({'field':X.columns,'weight':coefs} )
train_accuracy = []
test_accuracy = []
trial_range = range(1,11)
 for depth in trial_range:
temp_dt = tree.DecisionTreeClassifier(random_state = 0, max_depth = depth)
 temp_dt.fit(X_train, y_train)
 train_accuracy.append(temp_dt.score(X_train, y_train))
test_accuracy.append(temp_dt.score(X_test, y_test))
maxdepth_df = pd.DataFrame({"max_depth": trial_range, "train_acc": train_accuracy, "test_acc": test_accuracy})
 maxdepth_df<define_variables>
predictions = ridge_model.predict(test_data )
max_depth_fixed = 4<train_model>
print(predictions.mean())
 preds = np.expm1(predictions)
 print(preds.mean() )
train_accuracy = []
test_accuracy = []
trial_range = range(2,51)
 for samples in trial_range:
temp_dt = tree.DecisionTreeClassifier(random_state = 0, max_depth = max_depth_fixed, min_samples_leaf = samples)
 temp_dt.fit(X_train, y_train)
 train_accuracy.append(temp_dt.score(X_train, y_train))
test_accuracy.append(temp_dt.score(X_test, y_test))
minsamples_df = pd.DataFrame({"min_samples_leaf": trial_range, "train_acc": train_accuracy, "test_acc": test_accuracy})
 plt.figure(figsize =(20,5))
plt.plot(minsamples_df["min_samples_leaf"], minsamples_df["train_acc"], marker = "o")
 plt.plot(minsamples_df["min_samples_leaf"], minsamples_df["test_acc"], marker = "o")
 plt.xlabel("Minimum samples in terminal nodes")
 plt.xticks(range(0, max(trial_range)+ 2, 2))
plt.ylabel("Model accuracy score")
 plt.title("Decision Tree model accuracy with varying 'min_samples_leaf'")
 plt.legend()<train_model>
sub = pd.DataFrame()
sub['Id'] = test_data['Id']
sub['SalePrice'] = preds
<train_model><EOS> 
sub.to_csv('submission.csv',index=False )