Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<load_from_csv>
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv" )<feature_engineering>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
all_data = pd.concat(( train.loc[:,'MSSubClass':'SaleCondition'],
test.loc[:,'MSSubClass':'SaleCondition']))
train["SalePrice"] = np.log1p(train["SalePrice"])
 numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
skewed_feats = train[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
all_data[skewed_feats] = np.log1p(all_data[skewed_feats])
 all_data = pd.get_dummies(all_data)
 all_data = all_data.fillna(all_data.mean())
 X_train = all_data[:train.shape[0]]
X_test = all_data[train.shape[0]:]
y = train.SalePrice<train_on_grid>
X_train=train.drop(labels = ["label"],axis = 1)
 Y_train=train['label']
print(X_train.shape)
 print(Y_train.shape )
model_lasso = LassoCV(alphas = [1, 0.1, 0.001, 0.0005] ).fit(X_train, y )<find_best_params>
Y_train.value_counts()
rmse_cv(model_lasso ).mean()<train_on_grid>
X_train=X_train.astype('float32')/255
test=test.astype('float32')/255
alphas = [10, 1, 0.1, 0.01, 0.001, 0.0005, 0.0003]
lassos = [LassoCV(alphas = [a] ).fit(X_train, y)for a in alphas] 
<define_variables>
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )
nonzeros = [sum(c != 0)for c in coef]<find_best_model_class>
Y_train = to_categorical(Y_train, num_classes = 10 )
model_ridge = Ridge(alpha=10 ).fit(X_train, y)
 model_lasso = LassoCV(alphas=[0.0003] ).fit(X_train, y)
 ridge1_preds = np.expm1(model_ridge.predict(X_test))
lasso1_preds = np.expm1(model_lasso.predict(X_test))
y_lasso = model_lasso.predict(X_train)
 y_ridge = model_ridge.predict(X_train)
 y_lasso_test = model_lasso.predict(X_test)
 y_ridge_test = model_ridge.predict(X_test )<feature_engineering>
X_train, X_valid, Y_train, Y_valid = train_test_split(X_train, Y_train, test_size = 0.1, random_state=42 )
X_train['y_lasso'] = y_lasso
X_train['y_ridge'] = y_ridge
X_test['y_lasso'] = y_lasso_test
X_test['y_ridge'] = y_ridge_test<train_model>
from keras.layers import Input,InputLayer, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout
from keras.models import Sequential,Model
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint,LearningRateScheduler
import keras
from keras import backend as K
stacked_ridge = Ridge(alpha=10 ).fit(X_train, y )<predict_on_test>
inputShape=(28,28,1)
 input = Input(inputShape)
 x = Conv2D(64,(3,3),strides =(1,1),name='layer_conv1',padding='same' )(input)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = MaxPooling2D(( 2,2),name='maxPool1' )(x)
 x = Conv2D(64,(3,3),strides =(1,1),name='layer_conv2',padding='same' )(x)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = MaxPooling2D(( 2,2),name='maxPool2' )(x)
 x = Conv2D(32,(3,3),strides =(1,1),name='conv3',padding='same' )(x)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = MaxPooling2D(( 2,2),name='maxPool3' )(x)
 x = Flatten()(x)
 x = Dense(64,activation = 'relu',name='fc0' )(x)
 x = Dropout(0.25 )(x)
 x = Dense(32,activation = 'relu',name='fc1' )(x)
 x = Dropout(0.25 )(x)
 x = Dense(10,activation = 'softmax',name='fc2' )(x)
 model = Model(inputs = input,outputs = x,name='Predict')
preds = np.expm1(stacked_ridge.predict(X_test)) 
<create_dataframe>
momentum = 0.5
sgd = SGD(lr=0.01, momentum=momentum, decay=0.0, nesterov=False)
 model.compile(loss='categorical_crossentropy',optimizer=sgd, metrics=['accuracy'] )
sub = pd.DataFrame({"Id": test.Id, "SalePrice": lasso1_preds} )<save_to_csv>
def step_decay(epoch):
initial_lrate=0.1
drop=0.6
epochs_drop = 3.0
lrate= initial_lrate * math.pow(drop,
math.floor(( 1+epoch)/epochs_drop))
return lrate
lrate = LearningRateScheduler(step_decay)
 callbacks_list = [ lrate]
sub.to_csv('submission.csv', index=False )<save_to_csv>
history=model.fit(X_train, Y_train, validation_data=(X_valid, Y_valid),
epochs=35,callbacks=callbacks_list,verbose=1 )
sub.to_csv('submission.csv', index=False )<import_modules>
print("on valid data")
 pred1=model.evaluate(X_valid,Y_valid)
 print("accuaracy", str(pred1[1]*100))
print("Total loss",str(pred1[0]*100))
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
from xgboost import XGBRegressor
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np<load_from_csv>
display_activation(activations, 8, 8, 1 )
train_data = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test_data = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 full_data = pd.concat([train_data,test_data] ).reset_index(drop = True)
 sale_price = train_data['SalePrice'].reset_index(drop=True)
 del full_data['SalePrice']
print(f'Train dataframe contains {train_data.shape[0]} rows and {train_data.shape[1]} columns.
 ')
 print(f'Test dataframe contains {test_data.shape[0]} rows and {test_data.shape[1]} columns.
 ')
 print(f'The merged dataframe contains {full_data.shape[0]} rows and {full_data.shape[1]} columns.' )<count_missing_values>
display_activation(activations, 8, 8, 7 )
cols_to_drop = []
for column in full_data:
if full_data[column].isnull().sum() / len(full_data)>= 0.4:
cols_to_drop.append(column)
 full_data.drop(cols_to_drop, axis=1, inplace=True)
 print(f'{len(cols_to_drop)} columns dropped, the full dataset now comprises of {full_data.shape[1]} variables.' )<normalization>
Y_prediction = model.predict(X_valid)
 Y_pred_classes = np.argmax(Y_prediction,axis = 1)
 Y_true = np.argmax(Y_valid,axis = 1)
 confusion_mtx = confusion_matrix(Y_true, Y_pred_classes )
scaler = MinMaxScaler()
columns = full_data.columns.values
for column in columns:
if full_data[column].dtype == np.int64 or full_data[column].dtype == np.float64:
full_data[column] = full_data[column].fillna(full_data[column].median())
 full_data[column] = scaler.fit_transform(np.array(full_data[column] ).reshape(-1,1))
full_data.head()<count_unique_values>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
<data_type_conversions><EOS> 
submissions=pd.DataFrame({"ImageId": list(range(1,len(results)+1)) ,
"Label": results})
 submissions.to_csv("re2-submission.csv", index=False, header=True )