Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<choose_model_class>
%matplotlib inline
warnings.filterwarnings("ignore")
<choose_model_class>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 print('Size of the training dataset',df_train.shape)
 print('Size of the testing dataset',df_test.shape )
<choose_model_class>
def eda(df_train, df_test):
df_train_explore = df_train.copy()
df_test_explore = df_test.copy()
col = df_test_explore.columns
f = 16
for i in range(1, len(col)) :
print(col[i])
 t = df_train_explore[col[i]].dtypes
plt.figure(figsize =(28, 4))
df_train_explore[col[i]].dropna(inplace = True)
 df_test_explore[col[i]].dropna(inplace = True)
 if t != 'object':
plt.subplot(1, 4, 1)
 sns.distplot(df_train_explore[col[i]], color = 'seagreen', hist_kws = {"alpha": 0.5})
 plt.ylabel('Frequency', fontsize = f)
 plt.xlabel(col[i], fontsize = f)
 plt.title('Training set')
 plt.subplot(1, 4, 2)
 sns.distplot(df_test_explore[col[i]], color = 'seagreen', hist_kws = {"alpha": 0.5})
 plt.ylabel('Frequency', fontsize = f)
 plt.xlabel(col[i], fontsize = f)
 plt.title('Test set')
 plt.subplot(1, 4, 3)
 sns.scatterplot(x = col[i], y = 'SalePrice', data = df_train_explore, color = 'seagreen', alpha = 0.8)
 plt.ylabel('Sale Price',fontsize = f)
 plt.xlabel(col[i], fontsize = f)
 if len(df_train_explore[col[i]].unique())> 20:
df_train_explore[col[i]], bins=pd.cut(df_train_explore[col[i]], 10, retbins = True, duplicates = 'drop')
 grouped_train = df_train_explore.groupby(col[i])
 grouped_mean_train = grouped_train['SalePrice'].mean()
if grouped_mean_train.index.values.dtype == 'int64':
x = grouped_mean_train.index.values
else:
x =(bins[0:len(bins)-1] + bins[1:len(bins)])/2
plt.subplot(1, 4, 4)
 g = sns.lineplot(x, grouped_mean_train.values, color = 'seagreen',alpha = 0.8, marker = 'o')
 plt.ylabel('Mean Sale Price',fontsize = f)
 plt.xlabel(col[i], fontsize = f)
 else:
grouped_train = df_train_explore.groupby(col[i])
 grouped_test = df_test_explore.groupby(col[i])
 grouped_train_count = grouped_train.count()
grouped_test_count = grouped_test.count()
grouped_mean_train = grouped_train['SalePrice'].mean()
plt.subplot(1, 4, 1)
 g = sns.barplot(grouped_train_count.index.values, grouped_train_count['Id'].values, color = 'seagreen', alpha = 0.7)
 plt.ylabel('Frequency', fontsize = f)
 g.set_xticklabels(grouped_train_count.index.values, rotation = 90, fontsize = f)
 plt.title('Training set')
 plt.subplot(1, 4, 2)
 g = sns.barplot(grouped_test_count.index.values, grouped_test_count['Id'].values, color = 'seagreen', alpha = 0.7)
 g.set_xticklabels(grouped_test_count.index.values, rotation = 90, fontsize = f)
 plt.ylabel('Frequency',fontsize = f)
 g.set_xticklabels(grouped_test_count.index.values, rotation = 90, fontsize = f)
 plt.title('Test set')
 plt.subplot(1, 4, 3)
 g = sns.scatterplot(x = col[i], y = 'SalePrice', data = df_train_explore, color = 'seagreen', alpha = 0.8)
 plt.ylabel('Sale Price', fontsize = f)
 g.set_xticklabels(grouped_train_count.index.values, rotation = 90, fontsize = f)
 plt.xlabel('', fontsize = f)
 plt.subplot(1, 4, 4)
 g = sns.lineplot(grouped_mean_train.index.values, grouped_mean_train.values, color = 'seagreen', alpha = 0.8, marker = 'o')
 g.set_xticklabels(grouped_mean_train.index.values, rotation = 90, fontsize = f)
 plt.ylabel('Mean Sale Price', fontsize = f)
 plt.show()
<choose_model_class>
eda(df_train, df_test )
<choose_model_class>
def clean_data(df):
df = df.replace({'Ex': 5, 'Gd': 4, 'TA': 3, 'Fa': 2, 'Po': 1})
 df = df.replace({'Grvl': 1, 'Pave': 2})
 df['Alley'].fillna(0, inplace = True)
 df['GarageYrBlt'].fillna(0, inplace = True)
 df['LotFrontage'] = df.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.median()))
df = df.replace({'LotShape':{'IR3': 1, 'IR2': 2, 'IR1': 3, 'Reg': 4}})
 df['LotShape'].fillna(df['LotShape'].value_counts().idxmax() , inplace=True)
 df['MSZoning'].fillna(df['MSZoning'].value_counts().idxmax() , inplace=True)
 df['Utilities'].fillna(df['Utilities'].value_counts().idxmax() , inplace=True)
 df = df.replace({'Utilities':{'ELO': 1, 'NoSeWa': 2, 'NoSewr': 3, 'AllPub': 4}})
 df['LandSlope'].fillna(df['LandSlope'].value_counts().idxmax() , inplace = True)
 df = df.replace({'LandSlope':{'Gtl': 1, 'Mod': 2, 'Sev': 3}})
 df['Exterior1st'].fillna(df['Exterior1st'].value_counts().idxmax() , inplace = True)
 df['Exterior2nd'].fillna(df['Exterior2nd'].value_counts().idxmax() , inplace = True)
 df['MasVnrType'].fillna(df['MasVnrType'].value_counts().idxmax() , inplace = True)
 df['MasVnrArea'].fillna(0, inplace = True)
 df['BsmtFinType1'].fillna('NoBsmt', inplace = True)
 df['BsmtFinType2'].fillna('NoBsmt', inplace = True)
 df['BsmtCond'].fillna(0, inplace = True)
 df['BsmtQual'].fillna(0, inplace = True)
 df['BsmtExposure'].fillna(0, inplace = True)
 df = df.replace({'BsmtExposure':{'Av': 3, 'Mn': 2, 'No': 1}})
 df['BsmtFinSF1'].fillna(0, inplace = True)
 df['BsmtFinSF2'].fillna(0, inplace = True)
 df['BsmtUnfSF'].fillna(0, inplace = True)
 df['TotalBsmtSF'].fillna(0, inplace = True)
 df = df.replace({'CentralAir':{'Y': 1, 'N': 0}})
 df['1stFlrSF'].fillna(0, inplace = True)
 df['2ndFlrSF'].fillna(0, inplace = True)
 df['LowQualFinSF'].fillna(0, inplace = True)
 df['BsmtFullBath'].fillna(0, inplace = True)
 df['BsmtHalfBath'].fillna(0, inplace = True)
 df['FullBath'].fillna(0, inplace = True)
 df['HalfBath'].fillna(0, inplace = True)
 df['KitchenQual'].fillna(df['KitchenQual'].value_counts().idxmax() ,inplace = True)
 df['Functional'].fillna('Typ',inplace = True)
 df = df.replace({'Functional':{'Typ': 7, 'Min1': 6, 'Maj1': 5, 'Min2': 4, 'Mod': 3, 'Maj2': 2, 'Sev': 1, 'Sal': 0}})
 df['FireplaceQu'].fillna(0, inplace = True)
 df['GarageType'].fillna('NoGarage', inplace = True)
 df['GarageFinish'].fillna(0, inplace = True)
 df = df.replace({'GarageFinish':{'Fin': 3, 'RFn': 2, 'Unf': 1}})
 df['GarageArea'].fillna(0, inplace = True)
 df['GarageCars'].fillna(0, inplace = True)
 df['GarageQual'].fillna(0, inplace = True)
 df['GarageCond'].fillna(0, inplace = True)
 df = df.replace({'PavedDrive':{'Y': 2, 'P': 1, 'N': 0}})
 df['Electrical'].fillna(df['Electrical'].value_counts().idxmax() , inplace = True)
 df['PoolQC'].fillna(0, inplace = True)
 df['Fence'].fillna('NoFence', inplace = True)
 df['MiscFeature'].fillna('NoMiF', inplace = True)
 df['SaleType'].fillna(df['SaleType'].value_counts().idxmax() , inplace = True)
 return df
model = Sequential()
model.add(Conv2D(32,kernel_size=3,padding='same',activation='relu',
input_shape=(28,28,1)))
 model.add(Conv2D(32,kernel_size=3,padding='same',activation='relu',
input_shape=(28,28,1)))
 model.add(MaxPool2D())
 model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Conv2D(64,kernel_size=3,padding='same',activation='relu',
input_shape=(28,28,1)))
 model.add(Conv2D(64,kernel_size=3,padding='same',activation='relu',
input_shape=(28,28,1)))
 model.add(MaxPool2D())
 model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
<train_model>
def num_to_cat(df):
df['YrSold'] = df['YrSold'].astype(str)
 return df
print("-------------------------------------------------------------------")
 print('my model')
 history = model.fit_generator(augmented_dataset.flow(X_train,y_train,batch_size=batchsize),
epochs=epochs, validation_data=(X_test,y_test),
steps_per_epoch=X_train.shape[0]//batchsize,
callbacks=[learning_rate_reduction])
 print("OuO")
 fig, ax = plt.subplots(2,1)
 ax[0].plot(history.history['loss'], color='b', label="Training loss")
 legend = ax[0].legend(loc='best', shadow=True)
 ax[1].plot(history.history['accuracy'], color='b', label="Training accuracy")
 legend = ax[1].legend(loc='best', shadow=True )<save_to_csv>
def remove_columns(df):
col = ['Id', 'Utilities', 'Street', 'PoolArea' ,'LowQualFinSF', 'Alley', 'EnclosedPorch', '3SsnPorch', 'PoolQC',
'KitchenAbvGr']
df.drop(columns = col, inplace = True)
 return df
predictions = model.predict(test)
 pre_res = np.argmax(predictions,axis = 1)
 res = pd.Series(pre_res,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),res],axis = 1)
 submission.to_csv("mnist_submission.csv",index=False )<load_from_csv>
def add_features(df):
df['YrBltAndRemod'] = df['YearBuilt'] + df['YearRemodAdd']
df['TotalSF'] = df['TotalBsmtSF'] + df['1stFlrSF'] + df['2ndFlrSF']
df['Total_sqr_footage'] =(df['BsmtFinSF1'] + df['BsmtFinSF2'] + df['1stFlrSF'] + df['2ndFlrSF'])
 df['Total_Bathrooms'] =(df['FullBath'] +(0.5 * df['HalfBath'])+ df['BsmtFullBath'] +(0.5 * df['BsmtHalfBath']))
df['Total_porch_sf'] =(df['OpenPorchSF'] + df['3SsnPorch'] + df['EnclosedPorch'] + df['ScreenPorch'] + df['WoodDeckSF'])
 df['haspool'] = df['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 df['has2ndfloor'] = df['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 df['hasgarage'] = df['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 df['hasbsmt'] = df['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 df['hasfireplace'] = df['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
 return df
train_data = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test_data = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 train_data.head()<categorify>
def find_outliers(df):
outliers = df[df['GrLivArea'] > 4000].index
return outliers
pd.get_dummies(train_data["label"] ).values<categorify>
def add_polynomial(df):
col = ['OverallQual', 'ExterQual', 'BsmtQual', 'TotalBsmtSF', 'GrLivArea', 'GarageArea', 'GarageQual', 'KitchenQual',
'TotalSF']
for c in col:
c_2 = c + '-2'
c_3 = c + '-3'
c_sqrt = c + '-sqrt'
df[c_2] = df[c]**2
df[c_3] = df[c]**3
df[c_sqrt] = np.sqrt(df[c])
 return df
train_data_labels = pd.get_dummies(train_data["label"] ).values
train_data_pixels =(train_data.iloc[:,1:].values)/255.0
test_data_pixels =(test_data.values)/255.0<split>
def transf_skewed(df):
numerical = [var for var in df.columns if df[var].dtype != 'object' and var not in ['Id', 'SalePrice']]
skew_features = df[numerical].apply(lambda x: skew(x)).sort_values(ascending=False)
 high_skew = skew_features[skew_features > 0.5]
skew_index = high_skew.index
for i in skew_index:
df[i] = boxcox1p(df[i], boxcox_normmax(df[i] + 1))
return df
x_train,x_test,y_train,y_test = train_test_split(train_data_pixels,train_data_labels,test_size=0.2,random_state=42 )<choose_model_class>
def one_hot_encoding(df):
df = pd.get_dummies(df)
 return df
def build_model() :
model = Sequential()
model.add(Input(shape=(28,28,1,)))
 model.add(Conv2D(filters=64,kernel_size=(3,3),padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.25))
model.add(Conv2D(filters=64,kernel_size=(6,6),padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(Conv2D(filters=64,kernel_size=(3,3),padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.25))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters=128,kernel_size=(3,3),padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.25))
model.add(Conv2D(filters=128,kernel_size=(6,6),padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(Conv2D(filters=128,kernel_size=(3,3),padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.25))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters=256,kernel_size=(3,3),padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.25))
model.add(Conv2D(filters=256,kernel_size=(6,6),padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(Conv2D(filters=256,kernel_size=(3,3),padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.25))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2),padding="same"))
model.add(Dropout(0.25))
model.add(Conv2D(filters=32,kernel_size=(2,2),padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(SpatialDropout2D(0.25))
model.add(Flatten())
 model.add(Dense(256))
model.add(Activation("relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10))
model.add(Activation("softmax"))
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer ,metrics=["accuracy"], loss = "categorical_crossentropy")
 return model<choose_model_class>
def label_encoding(train):
obj = [var for var in train.columns if train[var].dtype == 'object']
for c in obj:
le = LabelEncoder()
train[c] = le.fit_transform(train[c])
 return train
train_image_generator = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-06,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
brightness_range=None,
shear_range=0.1,
zoom_range=0.1,
channel_shift_range=0.0,
fill_mode="nearest",
cval=0.0,
horizontal_flip=False,
vertical_flip=False,
rescale=None,
preprocessing_function=None,
data_format=None,
validation_split=0.0,
dtype=None, 
)
 train_image_generator.fit(x_train )<train_model>
s = df_train.shape[0]
y = df_train['SalePrice']
y_log = np.log1p(y)
 df_train = df_train.drop(columns = 'SalePrice')
 outliers=find_outliers(df_train)
 y_log.drop(outliers, inplace = True)
 X = pd.concat([df_train, df_test] )
callback_lr = ReduceLROnPlateau(monitor='val_acc',patience=2,factor=0.5,min_lr=0.00001,verbose=1)
 epochs = 50
batch_size = 256
history = model.fit_generator(train_image_generator.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(x_test,y_test),
verbose = 1, steps_per_epoch=x_train.shape[0] // batch_size
, callbacks=[callback_lr] )<train_model>
score = pd.DataFrame(columns = ['model', 'score'] )
model.fit(x_test,y_test,epochs=10,batch_size=512,validation_data=(x_train,y_train),callbacks=[callback_lr] )<save_to_csv>
cleaned_X = clean_data(X)
 remove_columns(cleaned_X)
 label_encoding(cleaned_X)
 X_train_TM = cleaned_X.iloc[:s, :]
X_test_TM = cleaned_X.iloc[s:, :]
X_train_TM.drop(outliers, inplace = True)
 print('Missing values in the cleaned training set:', X_train_TM.isnull().sum().sum())
 print('Missing values in the cleaned testing set:', X_test_TM.isnull().sum().sum() )
ypred = model.predict(test_data_pixels)
 ypred = ypred.argmax(-1)
 subimageid = [x for x in range(1,len(test_data_pixels)+1)]
submission = pd.DataFrame({"ImageId":subimageid,"Label":ypred})
 submission.to_csv("submission.csv",index=False )<set_options>
xgb_model = xgb.XGBRegressor(learning_rate = 0.01, n_estimators = 5800,
max_depth = 2, min_child_weight = 1,
gamma = 0, subsample = 0.5,
colsample_bytree = 0.5,
objective = 'reg:linear', nthread = -1,
scale_pos_weight = 1, seed = 27,
reg_alpha = 0.2, random_state = 42)
 sc = cross_val_score(xgb_model, X_train_TM, y_log, scoring = "neg_mean_squared_error", cv = 10)
 np.sqrt(-sc.mean())
 score = score.append({'model': 'XGBoost', 'score': np.sqrt(-sc.mean())}, ignore_index = True)
 print('The score of the XGBoost model:', np.sqrt(-sc.mean()))
%reload_ext autoreload
%autoreload 2<load_from_csv>
rforest_model = RandomForestRegressor(criterion = 'mse', max_depth = 18,
max_features = 0.4, min_samples_leaf = 1, min_samples_split = 2,
min_weight_fraction_leaf = 0, n_estimators = 5500, random_state = 0)
 sc = cross_val_score(rforest_model, X_train_TM, y_log, scoring = "neg_mean_squared_error", cv = 10)
 np.sqrt(-sc.mean())
 score = score.append({'model': 'RandomForest', 'score': np.sqrt(-sc.mean())}, ignore_index = True)
 print('The score of the RandomForest model:', np.sqrt(-sc.mean()))
train=pd.read_csv(inputs/"train.csv")
 train.head(3 )<load_from_csv>
gbr_model = GradientBoostingRegressor(loss = 'lad', learning_rate = 0.025, n_estimators = 5500,
subsample = 0.5, criterion = 'friedman_mse',
min_samples_split = 7, min_samples_leaf = 1, min_weight_fraction_leaf = 0.0,
max_depth = 2, min_impurity_decrease = 0.0, min_impurity_split = None, init = None,
random_state = 42, max_features = 'sqrt', alpha = 0.9, verbose = 0,
max_leaf_nodes = None, warm_start = False, presort = 'auto', validation_fraction = 0.3,
n_iter_no_change = None, tol = 0.0001)
 sc = cross_val_score(gbr_model, X_train_TM, y_log, scoring = "neg_mean_squared_error", cv = 10)
 np.sqrt(-sc.mean())
 score = score.append({'model': 'GradientBoosting', 'score': np.sqrt(-sc.mean())}, ignore_index = True)
 print('The score of the GradientBoosting model:', np.sqrt(-sc.mean()))
test=pd.read_csv(inputs/"test.csv")
 test.head(3 )<define_variables>
lgb_model = lgb.LGBMRegressor(objective = 'regression', num_leaves = 5,
learning_rate = 0.009, n_estimators = 5500,
max_bin = 200, bagging_fraction = 0.705,
bagging_freq = 5, feature_fraction = 0.2,
feature_fraction_seed = 9, bagging_seed = 9,
min_data_in_leaf = 2)
 sc = cross_val_score(lgb_model, X_train_TM, y_log, scoring = "neg_mean_squared_error", cv = 10)
 np.sqrt(-sc.mean())
 score = score.append({'model': 'LightGBM', 'score': np.sqrt(-sc.mean())}, ignore_index = True)
 print('The score of the LightGBM model:', np.sqrt(-sc.mean()))
tfms = get_transforms(do_flip=False)
 tr=Path(".. /train")
 te=Path(".. /test" )<sort_values>
cleaned_X = clean_data(X)
 cleaned_X = add_features(cleaned_X)
 cleaned_X = add_polynomial(cleaned_X)
 cleaned_X = num_to_cat(cleaned_X)
 cleaned_X = transf_skewed(cleaned_X)
 numerical = [var for var in cleaned_X if cleaned_X[var].dtype != 'object']
columns_scaling =(cleaned_X[numerical].max() > 3 ).index.values
robust_scaler = RobustScaler()
cleaned_X[columns_scaling] = robust_scaler.fit_transform(cleaned_X[columns_scaling])
 remove_columns(cleaned_X)
 cleaned_X = one_hot_encoding(cleaned_X)
 X_train_LM = cleaned_X.iloc[:s, :]
X_test_LM = cleaned_X.iloc[s:, :]
X_train_LM.drop(outliers, inplace = True)
 print('Missing values in the cleaned training set:', X_train_LM.isnull().sum().sum())
 print('Missing values in the cleaned testing set:', X_test_LM.isnull().sum().sum() )
sorted(os.listdir(tr))<data_type_conversions>
lasso_model = linear_model.Lasso(alpha = 0.00046, max_iter = 500, fit_intercept = True)
 sc = cross_val_score(lasso_model, X_train_LM, y_log, scoring = "neg_mean_squared_error", cv = 10)
 score = score.append({'model': 'Lasso', 'score': np.sqrt(-sc.mean())}, ignore_index = True)
 print('The score of the Lasso model:', np.sqrt(-sc.mean()))
for index, row in train.iterrows() :
label,digit = row[0], row[1:]
filepath = tr/str(label)
 filename = f"{index}.jpg"
digit = digit.values
digit = digit.reshape(28,28)
 digit = digit.astype(np.uint8)
 img = Image.fromarray(digit)
 img.save(filepath/filename)
<data_type_conversions>
elastic_model = linear_model.ElasticNet(alpha = 0.00046, l1_ratio = 1, fit_intercept = True, normalize = False,
precompute = False, max_iter = 500, copy_X = True, tol = 0.001, warm_start = False,
positive = False, random_state = 42, selection = 'cyclic')
 sc = cross_val_score(elastic_model, X_train_LM, y_log, scoring = "neg_mean_squared_error", cv = 10)
 score = score.append({'model': 'ElasticNet', 'score': np.sqrt(-sc.mean())}, ignore_index = True)
 print('The score of the ElasticNet model:', np.sqrt(-sc.mean()))
for index, digit in test.iterrows() :
filepath = te
filename = f"{index}.jpg"
digit = digit.values
digit = digit.reshape(28,28)
 digit = digit.astype(np.uint8)
 img = Image.fromarray(digit)
 img.save(filepath/filename )<load_pretrained>
lassolars_model = LassoLarsIC(criterion = 'aic', eps = 0.3)
 sc = cross_val_score(lassolars_model, X_train_LM, y_log, scoring = "neg_mean_squared_error", cv = 10)
 score = score.append({'model': 'LassoLars', 'score': np.sqrt(-sc.mean())}, ignore_index = True)
 print('The score of the LassoLars model:', np.sqrt(-sc.mean()))
data = ImageDataBunch.from_folder(path=".. /train",test=".. /test",ds_tfms=tfms, valid_pct=0.2,bs=32,size=24 ).normalize(imagenet_stats )<define_variables>
svr_model = SVR(C= 20, epsilon = 0.0117, gamma = 0.00041)
 sc = cross_val_score(svr_model, X_train_LM, y_log, scoring = "neg_mean_squared_error", cv = 10)
 score = score.append({'model': 'SVR', 'score': np.sqrt(-sc.mean())}, ignore_index = True)
 print('The score of the SVR model:', np.sqrt(-sc.mean()))
data.show_batch(rows=3 ,figsize=(5,5))<choose_model_class>
bridge_model = BayesianRidge(alpha_1 = 45, alpha_2 = 0, compute_score = False,
copy_X = True, fit_intercept = True, lambda_1 = 0, lambda_2 = 0,
n_iter = 500, normalize = False, tol = 0.001, verbose = False)
 sc = cross_val_score(bridge_model, X_train_LM, y_log, scoring = "neg_mean_squared_error", cv = 10)
 score = score.append({'model': 'BayesianRidge', 'score': np.sqrt(-sc.mean())}, ignore_index = True)
 print('The score of the BayesianRidge model:', np.sqrt(-sc.mean()))
learn = cnn_learner(data, models.resnet50, metrics=accuracy, model_dir = Path('.. /kaggle/input/ResNet-50'), callback_fns=ShowGraph )<train_model>
cleaned_X = clean_data(X)
 cleaned_X = add_features(cleaned_X)
 cleaned_X = add_polynomial(cleaned_X)
 cleaned_X = num_to_cat(cleaned_X)
 cleaned_X = transf_skewed(cleaned_X)
 remove_columns(cleaned_X)
 cleaned_X = one_hot_encoding(cleaned_X)
 X_train_LM2 = cleaned_X.iloc[:s, :]
X_test_LM2 = cleaned_X.iloc[s:, :]
X_train_LM2.drop(outliers, inplace = True)
 print('Missing values in the cleaned training set:', X_train_LM2.isnull().sum().sum())
 print('Missing values in the cleaned testing set:', X_test_LM2.isnull().sum().sum() )
learn.fit_one_cycle(5 )<save_model>
kridge_model = KernelRidge(alpha = 7.5)
 sc = cross_val_score(kridge_model, X_train_LM2, y_log, scoring = "neg_mean_squared_error", cv = 10)
 score = score.append({'model': 'KernelRidge', 'score': np.sqrt(-sc.mean())}, ignore_index = True)
 print('The score of the KernelRidge model:', np.sqrt(-sc.mean()))
learn.save("501" )<train_model>
X_train, X_val, y_train, y_val = train_test_split(X_train_TM, y_log, test_size = 0.3,random_state = 0)
 xgb_model.fit(X_train, y_train)
 y_val_pred_xgb = np.expm1(xgb_model.predict(X_val))
print('XGB:', np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_xgb)))
 gbr_model.fit(X_train, y_train)
 y_val_pred_gbr = np.expm1(gbr_model.predict(X_val))
gbr_score = np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_gbr))
print('GradientBoosting:', gbr_score)
 lgb_model.fit(X_train, y_train)
 y_val_pred_lgb = np.expm1(lgb_model.predict(X_val))
lgb_score = np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_lgb))
print('LightGBM:', lgb_score)
 X_train, X_val, y_train, y_val=train_test_split(X_train_LM, y_log, test_size = 0.3, random_state = 0)
 lasso_model.fit(X_train, y_train)
 y_val_pred_lasso = np.expm1(lasso_model.predict(X_val))
print('Lasso:', np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_lasso)))
 elastic_model.fit(X_train, y_train)
 y_val_pred_elastic = np.expm1(elastic_model.predict(X_val))
print("ElasticNet:",np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_elastic)))
 bridge_model.fit(X_train, y_train)
 y_val_pred_bridge = np.expm1(bridge_model.predict(X_val))
print('BRidge:',np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_bridge)))
 svr_model.fit(X_train, y_train)
 y_val_pred_svr = np.expm1(svr_model.predict(X_val))
print('SVR:',np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_svr)))
 X_train, X_val, y_train, y_val = train_test_split(X_train_LM2, y_log, test_size=0.3,random_state = 0)
 kridge_model.fit(X_train, y_train)
 y_val_pred_kridge = np.expm1(kridge_model.predict(X_val))
print('Kridge:', np.sqrt(mean_squared_log_error(np.expm1(y_val), y_val_pred_kridge)) )
learn.unfreeze()
learn.fit_one_cycle(5,max_lr=slice(1e-3,1e-1))<save_model>
d = {'y': np.expm1(y_val), 'XGB': y_val_pred_xgb, 'GBR': y_val_pred_gbr, 'LGB': y_val_pred_lgb,
'Lasso': y_val_pred_lasso, 'ElasticNet': y_val_pred_elastic, 'BRidge': y_val_pred_bridge,
'SVR': y_val_pred_svr, 'KRidge': y_val_pred_kridge}
prediction_val = pd.DataFrame(data = d)
 prediction_val_x = prediction_val[['ElasticNet','BRidge','GBR','SVR']]
prediction_val_y = prediction_val['y']
prediction_val.head(10 )
learn.save("502" )<predict_on_test>
reg = LinearRegression()
reg.fit(prediction_val_x, prediction_val_y)
 stacked_prediction = reg.predict(prediction_val_x)
 c = prediction_val_x.columns.values
for i in range(0, len(reg.coef_)) :
print(np.round(reg.coef_[i], decimals = 3 ).astype(str)+ ' x ' + c[i])
 print('Score:' + np.sqrt(mean_squared_log_error(np.expm1(y_val), stacked_prediction)).astype(str))
class_score,y=learn.get_preds(DatasetType.Test )<define_variables>
xgb_model.fit(X_train_TM, y_log)
 y_test_pred_xgb = np.expm1(xgb_model.predict(X_test_TM))
probs= class_score[0].tolist()
[f"{index}: {probs[index]}" for index in range(len(probs)) ]<prepare_output>
gbr_model.fit(X_train_TM, y_log)
 y_test_pred_gbr = np.expm1(gbr_model.predict(X_test_TM))
class_score=np.argmax(class_score,axis=1 )<load_from_csv>
lgb_model.fit(X_train_TM, y_log)
 y_test_pred_lgb = np.expm1(lgb_model.predict(X_test_TM))
samplesub=pd.read_csv(inputs/"sample_submission.csv")
 samplesub.head()<define_variables>
lasso_model.fit(X_train_LM, y_log)
 y_test_pred_lasso = np.expm1(lasso_model.predict(X_test_LM))
ImageId = [os.path.splitext(path)[0] for path in os.listdir(te)]
ImageId = [int(path)for path in ImageId]
ImageId = [ID+1 for ID in ImageId]
ImageId[:5]<create_dataframe>
elastic_model.fit(X_train_LM, y_log)
 y_test_pred_elastic = np.expm1(elastic_model.predict(X_test_LM))
subs=pd.DataFrame({"ImageId":ImageId,"Label":class_score} )<save_to_csv>
bridge_model.fit(X_train_LM, y_log)
 y_test_pred_bridge = np.expm1(bridge_model.predict(X_test_LM))
subs.to_csv("submission.csv",index=False)
 subs.head(3 )<import_modules>
svr_model.fit(X_train_LM, y_log)
 y_test_pred_svr = np.expm1(svr_model.predict(X_test_LM))
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import VGG16
from keras.models import Model
from keras.layers import Dense
from keras.layers import Flatten
from keras.models import Sequential
from keras.optimizers import SGD, Adam
import matplotlib.pyplot as plt
from keras import Input
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator<load_from_csv>
kridge_model.fit(X_train_LM2, y_log)
 y_test_pred_kridge = np.expm1(kridge_model.predict(X_test_LM2))
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 target = train['label']
train = train.drop(['label'], axis=1)
<data_type_conversions>
av =(y_test_pred_xgb + y_test_pred_elastic + y_test_pred_kridge + y_test_pred_bridge + y_test_pred_gbr + y_test_pred_lgb +\
y_test_pred_svr)/7
pred_av = {'Id': df_test['Id'], 'SalePrice': av}
prediction_av = pd.DataFrame(data = pred_av)
 prediction_av.head(5 )
full = pd.concat([train, test])
 full=full.to_numpy()
full=full.reshape(-1, 28, 28)
 full.shape<concatenate>
d = {'XGB': y_test_pred_xgb, 'GBR': y_test_pred_gbr, 'LGB': y_test_pred_lgb, 'Lasso': y_test_pred_lasso,
'ElasticNet': y_test_pred_elastic, 'BRidge': y_test_pred_bridge, 'SVR': y_test_pred_svr, 'KRidge': y_test_pred_kridge}
prediction_test = pd.DataFrame(data = d)
 prediction_test.head()
full = np.pad(full,(( 0,0),(2,2),(2,2)) , mode='constant')
 full = stacked_img = np.stack([full, full, full], axis=3)
 full.shape<categorify>
prediction_test_x = prediction_test[['ElasticNet','BRidge','GBR','SVR']]
prediction_test_x.head()
full=full.astype("float32")
 full = full/255
train=full[:42000, :, :, :]
test=full[42000:, :, :, :]<split>
stacked_prediction_test = reg.predict(prediction_test_x)
 df_stacked_prediction_test = pd.DataFrame(data = {'Id': df_test['Id'], 'SalePrice': stacked_prediction_test})
 df_stacked_prediction_test.head()
<prepare_output><EOS> 
df_stacked_prediction_test.to_csv('mysubmission.csv', index = False )