Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<choose_model_class>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV
from sklearn.preprocessing import MinMaxScaler, RobustScaler
from sklearn.metrics import mean_squared_error, explained_variance_score
from sklearn.linear_model import RidgeCV, Ridge
from sklearn.ensemble import VotingRegressor, GradientBoostingRegressor
from sklearn.ensemble import RandomForestRegressor, StackingRegressor
from xgboost import XGBRegressor
def build_model(num_classes):
inputs = layers.Input(shape=(img_height, img_width, 3))
x = img_augmentation(inputs)
 model = EfficientNetB2(include_top=False, input_tensor=x, weights="imagenet")
 model.trainable = False
x = layers.GlobalAveragePooling2D(name="avg_pool" )(model.output)
 x = layers.BatchNormalization()(x)
 x = layers.Dropout(0.3, name = 'drop2' )(x)
 x = layers.Dense(100, activation = "relu" )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dropout(0.2, name = 'drop1' )(x)
 x = layers.Dense(50, activation = "relu" )(x)
 x = layers.BatchNormalization()(x)
 top_dropout_rate = 0.2
x = layers.Dropout(top_dropout_rate, name="top_dropout" )(x)
 outputs = layers.Dense(num_classes, activation="softmax", name="pred" )(x)
 model = tf.keras.Model(inputs, outputs, name="EfficientNet")
 optimizer = optimizer=tf.keras.optimizers.Nadam(learning_rate=1e-2)
 model.compile(
 optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"] 
)
 return model<train_model>
%matplotlib inline
plt.rcParams['figure.figsize'] =(25, 15 )
epochs = 30
hist = model.fit(train_ds, epochs=epochs, verbose=2 )<choose_model_class>
data = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 data.drop(columns=['Id'], inplace=True )
def unfreeze_model(model):
for layer in model.layers[-35:]:
if not isinstance(layer, layers.BatchNormalization):
layer.trainable = True
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4)
 model.compile(
 optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"] 
)
 unfreeze_model(model)
 model.summary()<train_model>
data.columns[data.isna().any() ].tolist()
epochs = 30
hist = model.fit(train_ds, epochs=epochs, verbose=2)
 plot_hist(hist )<train_model>
def cleanNumericalData(df, feature):
mode = df[feature].dropna().value_counts().index[0]
df[feature] = df[feature].replace(np.nan, mode)
 return df[feature]
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-5)
 model.compile(
 optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"] 
)
 epochs = 20
hist = model.fit(train_ds, epochs=epochs, verbose=2)
 plot_hist(hist)
 optimizer = tf.keras.optimizers.Adam(learning_rate=1e-6)
 model.compile(
 optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"] 
)
 epochs = 10
hist = model.fit(train_ds, epochs=epochs, verbose=2)
 plot_hist(hist )<prepare_output>
nonObjCols = data.select_dtypes(exclude=['object'] ).columns
for feature in nonObjCols:
data[feature] = cleanNumericalData(data, feature )
test = []
for file in os.listdir(test_dir):
test.append(['test/{}'.format(file), file])
 test = pd.DataFrame(test, columns=['filepath', 'file'])
 print(test.head(2))
test.shape<train_model>
data['MasVnrType'] = data['MasVnrType'].replace(np.nan, data.MasVnrType.value_counts().index[0])
 data['Electrical'] = data['Electrical'].replace(np.nan, data.Electrical.value_counts().index[0] )
x_test = np.zeros(( len(test), img_height, img_width, 3), dtype='float32')
 for i, filepath in tqdm(enumerate(test['filepath'])) :
img = keras.preprocessing.image.load_img(os.path.join(data_dir, filepath), target_size =(img_height, img_width))
x = keras.preprocessing.image.img_to_array(img)
 x_test[i] = x
print('test Images shape: {} size: {:,}'.format(x_test.shape, x_test.size))<predict_on_test>
data = pd.get_dummies(data)
 columns = data.columns
data = pd.DataFrame(data, columns = columns)
 y = data.SalePrice.values
X = data.loc[:, data.columns != 'SalePrice'].values
one_hot_prediction = model.predict(x_test)
 one_hot_prediction[0:2]<prepare_output>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=18 )
preds = np.argmax(one_hot_prediction, axis=1)
 print(preds.shape)
 print(preds)
 prediction = [class_names[i] for i in preds]
print(len(prediction))
print(prediction )<prepare_output>
inScale = RobustScaler()
outScale = RobustScaler()
X_train = inScale.fit_transform(X_train)
 y_train = outScale.fit_transform(y_train.reshape(-1, 1))
X_test = inScale.transform(X_test)
 y_test = outScale.transform(y_test.reshape(-1, 1))
my_submission = pd.DataFrame({'file': test['file'], 'species': prediction})
 print(my_submission.head(5))
print(my_submission.shape )<save_to_csv>
estimators = [('ridge', ridge),('ridgecv', rcv),('randomforest', rf),('gradientboost', gbr),('xgboost', xb)]
sr = StackingRegressor(estimators=estimators, final_estimator=RandomForestRegressor(n_estimators=100, random_state=42, n_jobs=-1), cv = 10 ).fit(X_train, y_train.ravel())
 data_predicted = sr.predict(X_test)
 MAX = 200
x = range(len(data_predicted)) [0:MAX]
plt.scatter(x,y_test[0:MAX],color='g', linewidths=5)
 plt.scatter(x,data_predicted[0:MAX],color='r', linewidths=3)
 plt.plot(data_predicted[0:MAX], color = 'r', linewidth=2)
 mse = mean_squared_error(y_test, data_predicted)
 print("MSE: %.4f" % mse)
 evs = explained_variance_score(y_test, data_predicted)
 print("EVS: %.4f" % evs)
 score = sr.score(X_test, y_test)
 print("Score: %.4f" % score )
my_submission.to_csv('output.csv', index=False )<import_modules>
data = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 data.head()
id_values = data.Id.values
import numpy as np
import pandas as pd
import os 
<set_options>
nonObjCols = data.select_dtypes(exclude=['object'] ).columns
for feature in nonObjCols:
data[feature] = cleanNumericalData(data, feature )
%matplotlib inline<load_from_csv>
data['MasVnrType'] = data['MasVnrType'].replace(np.nan, data.MasVnrType.value_counts().index[0])
 data['Electrical'] = data['Electrical'].replace(np.nan, data.Electrical.value_counts().index[0] )
data_dir = '.. /input/plant-seedlings-classification/'
train_dir = os.path.join(data_dir, 'train')
 test_dir = os.path.join(data_dir, 'test')
 sample_submission = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))<define_variables>
data = pd.get_dummies(data)
 tcol = data.columns
train_dir = pathlib.Path(train_dir)
 image_count = len(list(train_dir.glob('*/*.png')))
 print(image_count )<define_variables>
diffCols = [x for x in columns if x not in tcol]
data[diffCols] = 0
data = pd.DataFrame(data, columns = columns)
 data.drop(['SalePrice'], axis=1, inplace=True )
batch_size = 32
img_height = 224
img_width = 224<train_model>
for col in data.columns:
if(data[col].isna().sum() > 0):
print(col )
train_ds = tf.keras.preprocessing.image_dataset_from_directory(train_dir,
validation_split = 0.1,
subset = "training",
seed = 1337,
image_size =(img_height, img_width),
batch_size = batch_size)
 val_ds = tf.keras.preprocessing.image_dataset_from_directory(train_dir,
validation_split = 0.1,
subset = "validation",
seed = 1337,
image_size =(img_height, img_width),
batch_size = batch_size )<define_variables>
data = inScale.transform(data)
 y = vr.predict(data)
 Predicted_Values = outScale.inverse_transform(y.reshape(-1, 1))
Predicted_Values.shape
class_names = train_ds.class_names
num_classes = len(class_names)
 print(class_names)
 print(num_classes)
<choose_model_class>
result = pd.DataFrame(id_values, columns=['Id'])
 result['SalePrice'] = Predicted_Values
result
<categorify><EOS> 
result.to_csv("Result.csv", index=False )