Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<train_model>
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from lightgbm import LGBMRegressor
from catboost import CatBoostRegressor
import seaborn as sns
m = RandomForestRegressor(n_jobs=-1, n_estimators=400, oob_score=True, max_features=0.5)
 m.fit(X_train, y_train)
 score(m,X_train, y_train )<save_to_csv>
train=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.head()
df_preds = pd.DataFrame(columns=['Prediction'],index=X_test.index, data=np.square(np.exp(m.predict(X_test))))
df_preds.to_csv('submission1', index=True, index_label='Id')
 df_preds.head()<prepare_x_and_y>
train.isnull().any()
X_train, X_test = prcs(df_joined.copy() , fe=['id'])
 y_train = df_train['revenue'].copy().apply(np.sqrt ).apply(np.log )<train_model>
miss_col=[col for col in train.columns if train[col].isnull().any() ]
print(miss_col )
m = RandomForestRegressor(n_jobs=-1, n_estimators=400, oob_score=True, max_features=0.5)
 m.fit(X_train, y_train)
 score(m,X_train, y_train )<save_to_csv>
for col in miss_col:
train[col]=train[col].fillna(train[col].mode() [0])
df_preds = pd.DataFrame(columns=['Prediction'],index=X_test.index, data=np.square(np.exp(m.predict(X_test))))
df_preds.to_csv('submission2.csv', index=True, index_label='Id')
 df_preds.head()<prepare_x_and_y>
train.isnull().sum()
X_train, X_test = prcs(df_joined.copy() , fe=['id', 'dummies'])
 y_train = df_train['revenue'].copy().apply(np.sqrt ).apply(np.log )<train_model>
train.isnull().sum()
m = RandomForestRegressor(n_jobs=-1, n_estimators=400, oob_score=True, max_features=0.5)
 m.fit(X_train, y_train)
 score(m,X_train, y_train )<save_to_csv>
LE=LabelEncoder()
for col in train.select_dtypes(include=['object']):
train[col]=LE.fit_transform(train[col] )
df_preds = pd.DataFrame(columns=['Prediction'],index=X_test.index, data=np.square(np.exp(m.predict(X_test))))
df_preds.to_csv('submission3.csv', index=True, index_label='Id')
 df_preds.head()<prepare_x_and_y>
saleprice_scaled = StandardScaler().fit_transform(train['SalePrice'][:,np.newaxis])
 low_range = saleprice_scaled[saleprice_scaled[:,0].argsort() ][:10]
high_range= saleprice_scaled[saleprice_scaled[:,0].argsort() ][-10:]
print('outer range(low)of the distribution:')
 print(low_range)
 print('
outer range(high)of the distribution:')
 print(high_range )
X_train, X_test = prcs(df_joined.copy() , fe=['id', 'dummies', 'city'])
 y_train = df_train['revenue'].copy().apply(np.sqrt ).apply(np.log )<train_model>
train['TotalSF'] = train['TotalBsmtSF'] + train['1stFlrSF'] + train['2ndFlrSF']
print(train)
 train.drop(['TotalBsmtSF','1stFlrSF','2ndFlrSF'],axis=1 )
m = RandomForestRegressor(n_jobs=-1, n_estimators=400, oob_score=True, max_features=0.5)
 m.fit(X_train, y_train)
 score(m,X_train, y_train )<save_to_csv>
test=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 test.isnull().sum()
df_preds = pd.DataFrame(columns=['Prediction'],index=X_test.index, data=np.square(np.exp(m.predict(X_test))))
df_preds.to_csv('submission4.csv', index=True, index_label='Id')
 df_preds.head()<prepare_x_and_y>
miss_test=[col for col in test.columns if test[col].isnull().any() ]
print(miss_test )
X_train, X_test = prcs(df_joined.copy() , fe=['id', 'dummies', 'city', 'city_group'])
 y_train = df_train['revenue'].copy().apply(np.sqrt ).apply(np.log )<train_model>
for col in miss_test:
test[col]=test[col].fillna(test[col].mode() [0] )
m = RandomForestRegressor(n_jobs=-1, n_estimators=400, oob_score=True, max_features=0.5)
 m.fit(X_train, y_train)
 score(m,X_train, y_train )<save_to_csv>
for col in test.select_dtypes(include=['object']):
test[col]=LE.fit_transform(test[col])
 test.head()
df_preds = pd.DataFrame(columns=['Prediction'],index=X_test.index, data=np.square(np.exp(m.predict(X_test))))
df_preds.to_csv('submission5.csv', index=True, index_label='Id')
 df_preds.head()<prepare_x_and_y>
test['TotalSF'] = test['TotalBsmtSF'] + test['1stFlrSF'] + test['2ndFlrSF']
print(test)
 test.drop(['TotalBsmtSF','1stFlrSF','2ndFlrSF'],axis=1 )
X_train, X_test = prcs(df_joined.copy() , fe=['id', 'dummies', 'city', 'city_group'])
 y_train = df_train['revenue'].copy().apply(np.sqrt ).apply(np.log )<train_model>
X_train=train.drop(["SalePrice"],axis=1)
 Y_train=train["SalePrice"]
print(X_train )
m = RandomForestRegressor(n_jobs=-1, n_estimators=4000, oob_score=True, max_features=0.5)
 m.fit(X_train, y_train)
 score(m,X_train, y_train )<save_to_csv>
x_train , x_test , y_train , y_test = train_test_split(X_train , Y_train ,test_size = 0.3,random_state = 3 )
df_preds = pd.DataFrame(columns=['Prediction'],index=X_test.index, data=np.square(np.exp(m.predict(X_test))))
df_preds.to_csv('submission6.csv', index=True, index_label='Id')
 df_preds.head()<import_modules>
lightgbm = LGBMRegressor(objective='regression',
num_leaves=8,
learning_rate=0.0385,
n_estimators=3500,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
verbose= 0,
)
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
from tensorflow.keras.applications import EfficientNetB3<load_from_csv>
lightgbm1 = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=5000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
verbose=-1, 
)
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )<categorify>
Catmodel= CatBoostRegressor(iterations=20, learning_rate=1, depth=3, verbose= 0 )
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
X_test = test / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 X_test = X_test.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train, num_classes = 10 )<choose_model_class>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )<choose_model_class>
Catmodel.fit(x_train,y_train )
enet = EfficientNetB3(input_shape=(32, 32, 3), weights='imagenet',include_top=False )<concatenate>
cs1 = Catmodel.score(x_train,y_train)
 print(cs1 )
X_train = np.pad(X_train,(( 0,0),(2,2),(2,2),(0,0)) , mode='constant')
 X_train.shape<concatenate>
cs2 = Catmodel.score(x_test,y_test)
 print(cs2 )
X_train = np.squeeze(X_train, axis=-1)
 X_train = stacked_img = np.stack(( X_train,)*3, axis=-1)
 X_train.shape<choose_model_class>
lightgbm.fit(x_train,y_train)
 lightgbm1.fit(x_train,y_train)
nets = 2
model = [0] *nets
for j in range(nets):
model[j] = Sequential(enet)
 model[j].add(Flatten())
 model[j].add(Dense(units=1024, use_bias=True, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dense(units=512, use_bias=True, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout (.4))
model[j].add(Dense(units=256, use_bias=True, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout (.4))
model[j].add(Dense(units=10, use_bias=True, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )<train_model>
ls1 = lightgbm.score(x_train,y_train)
 print(ls1 )
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = [0] * nets
epochs = 45
for j in range(nets):
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.1)
 history[j] = model[j].fit_generator(datagen.flow(X_train2,Y_train2, batch_size=64),
epochs = epochs, steps_per_epoch = X_train2.shape[0]//64,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("EffNet {0:d}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 j+1,epochs,max(history[j].history['accuracy']),max(history[j].history['val_accuracy'])) )<concatenate>
lg1 = lightgbm1.score(x_train,y_train)
 print(lg1 )
X_test = np.pad(X_test,(( 0,0),(2,2),(2,2),(0,0)) , mode='constant')
 X_test = np.squeeze(X_test, axis=-1)
 X_test = stacked_img = np.stack(( X_test,)*3, axis=-1)
 X_test.shape<save_to_csv>
ls2 = lightgbm.score(x_test,y_test)
 print(ls2 )
results = np.zeros(( X_test.shape[0],10))
for j in range(nets):
results = results + model[j].predict(X_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("MNIST_EffNet_Ensemble.csv",index=False )<prepare_x_and_y>
lg2 = lightgbm1.score(x_test,y_test)
 print(lg2 )
X_test1a = test / 255.0
X_test1a = X_test1a.values.reshape(-1,28,28,1 )<load_from_csv>
Catmodel.fit(X_train,Y_train )
mnist_test = pd.read_csv(".. /input/mnist-in-csv/mnist_test.csv")
 mnist_train = pd.read_csv(".. /input/mnist-in-csv/mnist_train.csv" )<load_from_csv>
lightgbm.fit(X_train,Y_train)
 lightgbm1.fit(X_train,Y_train)
sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv" )<feature_engineering>
prediction_lightgbm = lightgbm.predict(test)
 prediction_lightgbm1 = lightgbm1.predict(test)
 print('Ran' )
test['dataset'] = 'test'<feature_engineering>
prediction_catbooster = Catmodel.predict(test )
<concatenate><EOS> 
submit=pd.DataFrame()
submit['Id']=test['Id']
submit['SalePrice']=prediction_lightgbm1
submit.to_csv('submission.csv',index=False)
 print('Ran' )