Kaggle competition: Digit Recognizer
features['MSSubClass'] = features['MSSubClass'].apply(str)
 features['YrSold'] = features['YrSold'].apply(str)
 features['MoSold'] = features['MoSold'].apply(str )<feature_engineering>
from tensorflow.keras import utils
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout
from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
from sklearn.model_selection import train_test_split
numeric_types = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
numeric_features = []
for i in features.columns:
if features[i].dtypes in numeric_types:
numeric_features.append(i)
 skewed_feats = features[numeric_features].apply(lambda x: skew(x)).sort_values(ascending=False)
 high_skew = skewed_feats[skewed_feats > 0.5]
skew_ind = high_skew.index
for i in skew_ind:
features[i] = boxcox1p(features[i], boxcox_normmax(features[i] + 1))<drop_column>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
features = features.drop(['Utilities', 'Street', 'PoolQC',], axis=1)
 features['TotalSF'] = features['TotalBsmtSF'] + features['1stFlrSF'] + features['2ndFlrSF']
features['TotalFinSF'] = features['BsmtFinSF1'] + features['BsmtFinSF2'] + features['1stFlrSF'] + features['2ndFlrSF']
features['TotalBathrooms'] = features['FullBath'] + features['BsmtFullBath'] +(0.5 * features['HalfBath'])+(0.5 * features['BsmtHalfBath'])
<feature_engineering>
x_train = train.drop(["label"], axis=1 )
features['HasPool'] = features['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 features['Has2ndLvl'] = features['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 features['HasBsmt'] = features['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 features['HasGarage'] = features['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 features.shape<categorify>
y_train = train["label"]
final_feat = pd.get_dummies(features ).reset_index(drop=True)
 final_feat.head()<create_dataframe>
x_test=test
X = final_feat.iloc[:len(y), :]
X_test = final_feat.iloc[len(y):, :]
X.shape, y.shape, X_test.shape
test_types = X_test.dtypes
test_missing = round(( X_test.isnull().sum() /X_test.shape[0]),3)*100
test_explo = pd.DataFrame({'Types': test_types, 'Missing': test_missing} ).sort_values(by=['Missing', 'Types'], ascending=False)
 print(test_explo[test_explo.Missing > 0])
 train_types = X.dtypes
train_missing = round(( X.isnull().sum() /X.shape[0]),3)*100
train_explo = pd.DataFrame({'Types': train_types, 'Missing': train_missing} ).sort_values(by=['Missing', 'Types'], ascending=False)
 print(test_explo[test_explo.Missing > 0] )<train_model>
x_train = np.array(x_train)
 x_test = np.array(x_test)
 x_train=x_train.reshape(x_train.shape[0],28,28,1)
 x_test=x_test.reshape(x_test.shape[0],28,28,1 )
overfit = []
for i in X.columns:
counts = X[i].value_counts()
zeros = counts.iloc[0]
if zeros / len(X)* 100 > 99.94:
overfit.append(i)
 overfit = list(overfit)
 X = X.drop(overfit, axis=1)
 X_test = X_test.drop(overfit, axis=1)
 overfit<compute_train_metric>
y_train = utils.to_categorical(y_train,10 )
kfolds = KFold(n_splits=10, shuffle=True, random_state=42)
 def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
def cv_rmse(model, X=X):
rmse = np.sqrt(-cross_val_score(model, X, y, scoring="neg_mean_squared_error", cv=kfolds))
return(rmse )<define_search_model>
x_train = x_train / 255
x_test = x_test / 255
alphas_ridge = [14.5, 14.6, 14.7, 14.8, 14.9, 15, 15.1, 15.2, 15.3, 15.4, 15.5]
alphas_lasso = [5e-05, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008]
ridge = make_pipeline(RobustScaler() , RidgeCV(alphas=alphas_ridge, cv=kfolds))
lasso = make_pipeline(RobustScaler() , LassoCV(max_iter=1e7, alphas=alphas_lasso, random_state=42, cv=kfolds))
xgboost = XGBRegressor(learning_rate=0.01,n_estimators=3460,
max_depth=3, min_child_weight=0,
gamma=0, subsample=0.7,
colsample_bytree=0.7,
objective='reg:squarederror', nthread=-1,
scale_pos_weight=1, seed=27,
reg_alpha=0.00006)
<choose_model_class>
x_train, x_val, y_train , y_val=train_test_split(x_train,y_train,test_size=0.1 )
stack = StackingCVRegressor(regressors=(ridge, lasso, xgboost), meta_regressor=xgboost, use_features_in_secondary=True)
<train_model>
datagen= ImageDataGenerator(
 zoom_range=0.3,
rotation_range=0.2,
width_shift_range=0.5,
height_shift_range=0.5
)
print('stack')
 stack_gen_model = stack.fit(np.array(X), np.array(y))
print('ridge')
 ridge_model = ridge.fit(X, y)
 print('lasso')
 lasso_model = lasso.fit(X, y)
 print('XGBoost')
 xgb_model = xgboost.fit(X, y )<compute_test_metric>
model = Sequential()
print("RMSLE score on train data: ")
 print(rmsle(y, stack.predict(np.array(X))))<load_from_csv>
model.add(Conv2D(filters=32,kernel_size=(5,5),input_shape=(x_train.shape[1:]),padding="Same",activation="relu"))
model.add(Conv2D(filters=32,kernel_size=(5,5),padding="Same",activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters=64,kernel_size=(5,5),padding="Same",activation="relu"))
model.add(Conv2D(filters=64,kernel_size=(5,5),padding="Same",activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),))
model.add(Dropout(0.25))
model.add(Conv2D(filters=128,kernel_size=(5,5),padding="Same",activation="relu"))
model.add(Conv2D(filters=128,kernel_size=(5,5),padding="Same",activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
 model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(256,activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(10,activation="softmax"))
print('Predict Submission')
 submission = pd.read_csv(f"{input_dir}/sample_submission.csv")
 submission.iloc[:,1] = np.expm1(stack.predict(X_test))<save_to_csv>
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
 print(model.summary() )
submission.to_csv("submission.csv", index=False )<save_to_csv>
lr_reduce=ReduceLROnPlateau(monitor="val_acc",
patience=4,
verbose=1,
factor=0.5,
min_lr=0.00000001
)
submission.to_csv("submission.csv", index=False )<import_modules>
model.fit(datagen.flow(x_train,y_train,batch_size=100),
batch_size=1,
epochs=40,
validation_data=(x_val,y_val),
verbose=1,
callbacks=[checkpoint,lr_reduce] )
import pandas as pd
from tqdm import tqdm<load_from_csv>
model.load_weights("mnistCNN" )
train = pd.read_csv('.. /input/ames-housing-dataset/AmesHousing.csv')
 train.drop(['PID'], axis=1, inplace=True)
 origin = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.columns = origin.columns
test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 print('Train:{} Test:{}'.format(train.shape,test.shape))<drop_column>
predictions = model.predict(x_test )
missing = test.isnull().sum()
missing = missing[missing>0]
train.drop(missing.index, axis=1, inplace=True)
 train.drop(['Electrical'], axis=1, inplace=True)
 test.dropna(axis=1, inplace=True)
 test.drop(['Electrical'], axis=1, inplace=True )<feature_engineering>
predictions=np.argmax(predictions, axis=1 )
l_test = tqdm(range(0, len(test)) , desc='Matching')
 for i in l_test:
for j in range(0, len(train)) :
for k in range(1, len(test.columns)) :
if test.iloc[i,k] == train.iloc[j,k]:
continue
else:
break
else:
submission.iloc[i, 1] = train.iloc[j, -1]
break
l_test.close()<save_to_csv>
submission=pd.DataFrame({"ImageId":range(1,x_test.shape[0]+1),"Label": predictions})
<save_to_csv><EOS> 
submission = submission.to_csv("submission.csv",index=False )