Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<categorify>
%matplotlib notebook
sns.set_style('darkgrid')
Y_val=to_categorical(Y_val, num_classes=10)
 Y_train=to_categorical(Y_train, num_classes=10 )<choose_model_class>
original_data = pd.read_csv('.. /input/train.csv')
 print('\tFeature\t\t\tdtype\tunique')
 for col in original_data.columns:
print('%15s\t\t%10s\t%d'%(col,original_data[col].dtype,original_data[col].nunique()))
original_data = original_data.drop('Id',axis=1)
 quantitative_features = original_data.select_dtypes('number' ).columns
quantitative_features = quantitative_features[:-1]
qualitative_features = original_data.select_dtypes('object' ).columns
print('Houses: %d , columns: %d
'%(original_data.shape[0],original_data.shape[1]))
print('%d numerical features, %d qualitative features'%(len(quantitative_features),len(qualitative_features)) )
def create_model() :
VGG = VGG16(
 input_shape=(32, 32, 3),
weights='imagenet',
include_top=False, 
)
 model =tf.keras.Sequential([VGG,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 return model<train_model>
def qualitative_to_quantitative(df):
transformed_data = df.copy()
for col in qualitative_features:
ordering = pd.DataFrame()
ordering['vals'] = original_data[col].unique()
ordering.index = ordering.vals
ordering['mean_price'] = original_data[[col,'SalePrice']].groupby(col ).mean() ['SalePrice']
ordering['rank'] = original_data[[col,'SalePrice']].groupby(col ).mean() ['SalePrice'].rank()
ordering_dict = ordering.to_dict() ['rank']
transformed_data[col] = transformed_data[col].replace(to_replace=ordering_dict)
 return transformed_data
massaged_data = qualitative_to_quantitative(original_data )
im = ImageDataGenerator(zoom_range=0.1, rotation_range=15, height_shift_range= 0.05, width_shift_range= 0.05)
 flow=im.flow(X_train, Y_train, batch_size=32 )<choose_model_class>
y = massaged_data.SalePrice
all_data = massaged_data.drop('SalePrice',axis=1)
 X_train, X_test, y_train, y_test = train_test_split(all_data,y,random_state=2)
 imputer = SimpleImputer()
X_train = imputer.fit_transform(X_train)
 X_test = imputer.fit_transform(X_test)
 plot_different_fitting_routines(X_train,y_train,X_test,y_test )
mymod=create_model()
mymod.summary()
mymod.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'] )<predict_on_test>
y = massaged_data.SalePrice
numerical_data = massaged_data.drop('SalePrice',axis=1)
 numerical_data = numerical_data[quantitative_features]
X_train, X_test, y_train, y_test = train_test_split(numerical_data,y,random_state=1)
 imputer = SimpleImputer()
X_train = imputer.fit_transform(X_train)
 X_test = imputer.fit_transform(X_test)
 plot_different_fitting_routines(X_train,y_train,X_test,y_test )
predictions_val = np.zeros(shape=(5, X_val.shape[0], 10))
predictions_test = np.zeros(shape=(5, test.shape[0], 10))
for i in range(5):
print('training model ', i+1, '...')
 perf = mymod.fit_generator(flow, epochs=10, steps_per_epoch= X_train.shape[0]/32, validation_data=(X_val, Y_val), verbose=0)
 pred_val = mymod.predict(X_val)
 pred_test = mymod.predict(test)
 predictions_val[i, :, :] = pred_val
predictions_test[i, :, :] = pred_test
pvall = np.zeros(shape=(X_val.shape[0], 10))
ptall = np.zeros(shape=(test.shape[0], 10))
for i in range(10):
pv1 = np.mean(predictions_val[:, :, i], axis=0)
 pt1 = np.mean(predictions_test[:, :, i], axis=0)
 pvall[:, i] = pv1
ptall[:, i] = pt1
preds_val= np.argmax(pvall,axis = 1)
 preds_test = np.argmax(ptall, axis=1)
 val_acc = np.mean(preds_val == Y_val_access)
 print('Overall validation accuracy is', val_acc)
<save_to_csv>
mse = []
thresholds = np.arange(0.0,0.8,0.1)
 for thr in thresholds:
y = massaged_data.SalePrice
some_data = massaged_data.drop('SalePrice',axis=1)
 some_data = some_data[select_features(thr)]
X_train, X_test, y_train, y_test = train_test_split(some_data,y,random_state=1)
 imputer = SimpleImputer()
X_train = imputer.fit_transform(X_train)
 X_test = imputer.fit_transform(X_test)
 mse.append(mse_different_fitting_routines(X_train,y_train,X_test,y_test))
mse = np.array(mse ).T
plt.figure(figsize=(14,5))
plt.plot(thresholds,mse[0], label = 'regression')
 plt.plot(thresholds,mse[1], label = 'ridge')
 plt.plot(thresholds,mse[2], label = 'lasso')
 plt.plot(thresholds,mse[3], label = 'lassolars')
 plt.plot(thresholds,mse[4], label = 'bayes')
 plt.xlabel('Correlation threshold')
 plt.ylabel('MSE')
 plt.yscale('log')
 plt.gca().invert_xaxis()
plt.legend()
preds= pd.Series(preds_test,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),preds],axis = 1)
 submission.to_csv("MINST.csv",index=False )<set_options>
y = massaged_data.SalePrice
all_data = massaged_data.drop('SalePrice',axis=1)
 X_train, X_test, y_train, y_test = train_test_split(all_data,y,random_state=2)
 imputer = SimpleImputer()
X_train = imputer.fit_transform(X_train)
 X_test = imputer.fit_transform(X_test)
 plot_different_fitting_routines(X_train,y_train,X_test,y_test )
warnings.filterwarnings('ignore')
 seed_val = 42
random.seed(seed_val)
 np.random.seed(seed_val )<set_options>
prunned_data = massaged_data.drop('PoolQC',axis=1)
 prunned_data = prunned_data.drop('GarageYrBlt',axis=1)
black_red = [
'
]
plt.style.use('fivethirtyeight' )<load_from_csv>
y = prunned_data.SalePrice
all_data = prunned_data.drop('SalePrice',axis=1)
 X_train, X_test, y_train, y_test = train_test_split(all_data,y,random_state=2)
 imputer = SimpleImputer()
X_train = imputer.fit_transform(X_train)
 X_test = imputer.fit_transform(X_test)
 plot_different_fitting_routines(X_train,y_train,X_test,y_test )
train = pd.read_csv('.. /input/melanomaextendedtabular/external_upsampled_tabular.csv')
 test = pd.read_csv('.. /input/melanomaextendedtabular/test_tabular.csv')
 sample = pd.read_csv('.. /input/melanomaextendedtabular/sample_submission.csv' )<rename_columns>
logy = np.log(y)
 all_data = prunned_data.drop('SalePrice',axis=1)
 X_train, X_test, logy_train, logy_test = train_test_split(all_data,logy,random_state=2)
 imputer = SimpleImputer()
X_train = imputer.fit_transform(X_train)
 X_test = imputer.fit_transform(X_test)
 plot_different_fitting_routines(X_train,logy_train,X_test,logy_test,log=True )
train.columns = [
'img_name', 'sex', 'age', 'location', 'target','width','height'
]
test.columns = ['img_name', 'sex', 'age', 'location','width','height'] 
<categorify>
logy = np.log(y)
 all_data = prunned_data.drop('SalePrice',axis=1)
 all_data['GrLivArea'] = np.log(all_data['GrLivArea'])
 all_data['1stFlrSF'] = np.log(all_data['1stFlrSF'])
 all_data['ExterQual'] = np.log(all_data['ExterQual'])
 X_train, X_test, logy_train, logy_test = train_test_split(all_data,logy,random_state=2)
 imputer = SimpleImputer()
X_train = imputer.fit_transform(X_train)
 X_test = imputer.fit_transform(X_test)
 plot_different_fitting_routines(X_train,logy_train,X_test,logy_test,log=True )
for df in [train, test]:
df['location'].fillna('unknown', inplace=True)
 train['sex'].fillna('unknown', inplace=True)
 train['age'].fillna(-1, inplace=True )<statistical_test>
logy = np.log(y)
 all_data = prunned_data.drop('SalePrice',axis=1)
 all_data['GrLivArea'] = np.log(all_data['GrLivArea'])
 all_data['1stFlrSF'] = np.log(all_data['1stFlrSF'])
 all_data['ExterQual'] = np.log(all_data['ExterQual'])
 all_data['GarageCars'] = np.power(all_data['GarageCars'],2)
 all_data['BsmtQual'] = np.power(all_data['BsmtQual'],2)
 all_data['OverallQual'] = np.power(all_data['OverallQual'],2)
 X_train, X_test, logy_train, logy_test = train_test_split(all_data,logy,random_state=2)
 imputer = SimpleImputer()
X_train = imputer.fit_transform(X_train)
 X_test = imputer.fit_transform(X_test)
 plot_different_fitting_routines(X_train,logy_train,X_test,logy_test,log=True )
ids_train = train.location.values
ids_test = test.location.values
ids_train_set = set(ids_train)
 ids_test_set = set(ids_test)
 location_not_overlap = list(ids_train_set.symmetric_difference(ids_test_set))
n_overlap = len(location_not_overlap)
 if n_overlap == 0:
print(
 f'There are no different body parts occuring between train and test set...' 
)
 else:
print('There are some non-overlapping values between train and test set!
')
 print(f'Different ones are:
{pd.Series(np.setdiff1d(( train.location.value_counts().index), pd.Series(test.location.value_counts().index)))}')
<categorify>
logy = np.log(y)
 all_data = prunned_data.drop('SalePrice',axis=1)
 all_data['GrLivArea'] = np.log(all_data['GrLivArea'])
 all_data['1stFlrSF'] = np.log(all_data['1stFlrSF'])
 all_data['ExterQual'] = np.log(all_data['ExterQual'])
 all_data['GarageCars'] = np.power(all_data['GarageCars'],2)
 all_data['BsmtQual'] = np.power(all_data['BsmtQual'],2)
 all_data['OverallQual'] = np.power(all_data['OverallQual'],2)
 X_train_final, X_test_final, logy_train_final, logy_test_final = train_test_split(all_data,logy,random_state=2)
 imputer = SimpleImputer()
X_train_final = imputer.fit_transform(X_train_final)
 X_test_final = imputer.fit_transform(X_test_final )
train.replace(['anterior torso','lateral torso','posterior torso'], 'torso', inplace=True )<data_type_conversions>
regr = linear_model.LinearRegression(normalize=False)
 regr.fit(X_train_final,logy_train_final)
 plot(regr,X_train_final,logy_train_final,X_test_final,logy_test_final,title='Linear Regression',log=True )
train['res']= train['width'].astype(str)+'x'+train['height'].astype(str)
 test['res']= test['width'].astype(str)+'x'+test['height'].astype(str )<drop_column>
ridge = linear_model.RidgeCV(alphas=(0.1, 1.0, 10.0),normalize=True)
 ridge.fit(X_train_final,logy_train_final)
 plot(ridge,X_train_final,logy_train_final,X_test_final,logy_test_final,title='RidgeCV',log=True)
 print(ridge.alpha_)
 ridge2 = linear_model.RidgeCV(alphas=(0.5, 1.0, 2., 5., 8.) ,normalize=True)
 ridge2.fit(X_train_final,logy_train_final)
 plot(ridge2,X_train_final,logy_train_final,X_test_final,logy_test_final,title='RidgeCV',log=True)
 print(ridge2.alpha_ )
train.drop(['res'], axis=1, inplace=True)
 test.drop(['res'], axis=1, inplace=True )<categorify>
lasso = linear_model.LassoCV(normalize=True)
 lasso.fit(X_train_final,logy_train_final)
 plot(lasso,X_train_final,logy_train_final,X_test_final,logy_test_final,title='LassoCV',log=True)
 print(lasso.alpha_ )
sex_dummies = pd.get_dummies(train['sex'], prefix='sex')
 train = pd.concat([train, sex_dummies], axis=1)
 sex_dummies = pd.get_dummies(test['sex'], prefix='sex')
 test = pd.concat([test, sex_dummies], axis=1)
 train.drop(['sex'], axis=1, inplace=True)
 test.drop(['sex'], axis=1, inplace=True )<categorify>
lassolars = linear_model.LassoLarsCV(normalize=True)
 lassolars.fit(X_train_final,logy_train_final)
 plot(lassolars,X_train_final,logy_train_final,X_test_final,logy_test_final,title='LassoLarsCV',log=True)
 print(lassolars.alpha_ )
anatom_dummies = pd.get_dummies(train['location'], prefix='anatom')
 train = pd.concat([train, anatom_dummies], axis=1)
 anatom_dummies = pd.get_dummies(test['location'], prefix='anatom')
 test = pd.concat([test, anatom_dummies], axis=1)
 train.drop('location', axis=1, inplace=True)
 test.drop(['location'], axis=1, inplace=True )<drop_column>
bayes = linear_model.BayesianRidge(normalize=True)
 bayes.fit(X_train_final,logy_train_final)
 plot(bayes,X_train_final,logy_train_final,X_test_final,logy_test_final,title='Bayes',log=True )
for df in [train, test]:
df.drop('img_name', axis=1, inplace=True )<import_modules>
submission_data = pd.read_csv('.. /input/test.csv')
 ids = submission_data.Id
submission_data = submission_data.drop('Id',axis=1)
 submission_data = qualitative_to_quantitative(submission_data)
 submission_data = submission_data.drop('PoolQC',axis=1)
 submission_data = submission_data.drop('GarageYrBlt',axis=1)
 submission_data['GrLivArea'] = np.log(submission_data['GrLivArea'])
 submission_data['1stFlrSF'] = np.log(submission_data['1stFlrSF'])
 submission_data['ExterQual'] = np.log(submission_data['ExterQual'])
 submission_data['GarageCars'] = np.power(submission_data['GarageCars'],2)
 submission_data['BsmtQual'] = np.power(submission_data['BsmtQual'],2)
 submission_data['OverallQual'] = np.power(submission_data['OverallQual'],2)
 imputer = SimpleImputer()
X_submission = imputer.fit_transform(submission_data)
 predicted_prices_log = lassolars.predict(X_submission)
 predicted_prices = np.exp(predicted_prices_log)
 fig, ax = plt.subplots(1,1)
 ax.plot(ids.values,predicted_prices,marker='o',linestyle='None',label='train' )
<prepare_x_and_y><EOS> 
np.savetxt('submission_lassolars.csv',np.dstack(( ids,predicted_prices)).reshape(1459 , 2),delimiter=',',fmt='%d,%f',header='Id,SalePrice',comments='' )