Kaggle competition: Don't Get Kicked!
grid_ext_pred = grid_ext_clf.predict(validation_set_prepared ).astype(int)
 grid_ext_scores =(grid_ext_clf.best_score_ + accuracy_score(grid_ext_pred, validation_labels)) /2
grid_ext_scores<choose_model_class>
train_data = pd.read_csv("/kaggle/input/DontGetKicked/training.csv")
 train_data.head()
ada_clf = AdaBoostClassifier(
 DecisionTreeClassifier(random_state=rnd_state, max_depth=2),
random_state = rnd_state)
 param_grid = {"base_estimator__criterion" : ["gini", "entropy"],
"base_estimator__splitter" : ["best", "random"],
"algorithm" : ["SAMME","SAMME.R"],
"n_estimators" :[10, 100, 200, 250],
"learning_rate": [0.05, 0.5, 1.5, 2.5]}
grid_ada_clf = GridSearchCV(ada_clf, param_grid, cv=skfold, scoring="accuracy", n_jobs= -1, verbose = 1)
 grid_ada_clf.fit(train_set_prepared, survival_labels)
 grid_ada_clf.best_params_
ada_pred = grid_ada_clf.predict(validation_set_prepared ).astype(int)
 grid_ada_scores =(grid_ada_clf.best_score_ + accuracy_score(ada_pred, validation_labels)) /2
grid_ada_scores<train_on_grid>
test_data = pd.read_csv("/kaggle/input/DontGetKicked/test.csv")
 test_data.head()
gb_clf = GradientBoostingClassifier(random_state=rnd_state)
 param_grid = {
'n_estimators' : [25, 50 ,75, 100, 200],
'learning_rate': [0.005 ,0.05, 0.5, 1.5],
'max_depth': [2, 4, 6, 8],
'max_features': [10, 12, 17]
}
grid_gb_clf = GridSearchCV(gb_clf, param_grid, cv=skfold, scoring="accuracy", n_jobs= -1, verbose = 1)
 grid_gb_clf.fit(train_set_prepared, survival_labels)
 gb_pred = grid_gb_clf.predict(validation_set_prepared ).astype(int)
 grid_gb_scores =(grid_gb_clf.best_score_ + accuracy_score(gb_pred, validation_labels)) /2<find_best_params>
train_data.isnull().sum()
grid_gb_clf.best_params_<train_model>
test_data.isnull().sum()
voting_clf = VotingClassifier(
 estimators=[('rf', rnd_forest_clf),('svc', svm_clf),('knn', grid_knn_clf)],
voting='hard')
 voting_clf.fit(train_set_prepared, survival_labels )<compute_train_metric>
train_data['IsBadBuy'].value_counts()
voting_cross_scores = cross_val_score(voting_clf, train_set_prepared, survival_labels, cv=skfold, scoring="accuracy")
 voting_pred = voting_clf.predict(validation_set_prepared ).astype(int)
 voting_scores =(voting_cross_scores.mean() + accuracy_score(voting_pred, validation_labels)) /2
voting_scores<predict_on_test>
train_data['Model'].value_counts()
train_data.drop("Model",axis=1,inplace=True)
 test_data.drop("Model",axis=1,inplace=True )
test_set_prepared = preprocessor.fit_transform(data_cleaner(test_data))
predictions = voting_clf.predict(test_set_prepared ).astype(int )<save_to_csv>
train_data.drop("Trim",axis=1,inplace=True)
 test_data.drop("Trim",axis=1,inplace=True)
 train_data.drop("SubModel",axis=1,inplace=True)
 test_data.drop("SubModel",axis=1,inplace=True )
ids = test_data['PassengerId']
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False )<import_modules>
train_data["Color"].value_counts(dropna=False )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
from tqdm import tqdm
import os
import keras<load_from_csv>
test_data["Color"].value_counts(dropna=False )
Train_Dir = '/kaggle/input/facial-keypoints-detection/training.zip'
Test_Dir = '/kaggle/input/facial-keypoints-detection/test.zip'
lookid_dir = '/kaggle/input/facial-keypoints-detection/IdLookupTable.csv'
train_data = pd.read_csv(Train_Dir)
 test_data = pd.read_csv(Test_Dir)
 lookid_data = pd.read_csv(lookid_dir)
 os.listdir('.. /input' )<count_missing_values>
train_data["Color"].fillna(value="ColorUnknown",inplace=True)
 test_data["Color"].fillna(value="ColorUnknown",inplace=True )
train_data.isnull().sum()<categorify>
train_data["Transmission"].value_counts(dropna=False )
feature_8 = ['left_eye_center_x','left_eye_center_y','right_eye_center_x','right_eye_center_y','nose_tip_x','nose_tip_y','mouth_center_bottom_lip_x','mouth_center_bottom_lip_y','Image']
train_8 = train_data[feature_8].dropna().reset_index()
train_30=train_data.dropna().reset_index()<prepare_x_and_y>
train_data["Transmission"].replace("Manual","MANUAL",inplace=True )
def str_to_int(train1):
images = train1.Image.values
del train1['Image']
del train1['index']
y=train1.values
x = []
for i in tqdm(images):
q=[int(j)for j in i.split() ]
x.append(q)
 x=np.array(x)
 x=x.reshape(-1,96,96,1)
 x=x/255.0
return([x,y] )<import_modules>
test_data["Transmission"].value_counts(dropna=False )
from keras.models import Sequential
from keras.layers import Activation,Convolution2D,MaxPooling2D,BatchNormalization,Flatten,Dense,Dropout,Conv2D,MaxPool2D,ZeroPadding2D
from keras.layers.advanced_activations import LeakyReLU<choose_model_class>
train_data["Transmission"].fillna(value="Trans_unk",inplace=True)
 test_data["Transmission"].fillna(value="Trans_unk",inplace=True )
def create_model(out=8):
model = Sequential()
model.add(Convolution2D(32,(3,3), padding='same', use_bias=False, input_shape=(96,96,1)))
 model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(32,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(64,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(64,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(96,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(96,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(128,(3,3),padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(128,(3,3),padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(256,(3,3),padding='same',use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(256,(3,3),padding='same',use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(512,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(512,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(512,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(out))
model.summary()
model.compile(optimizer = 'adam' , loss = "mean_squared_error", metrics=["mae"])
 return model<choose_model_class>
train_data["WheelTypeID"].value_counts(dropna=False)
 train_data.drop("WheelTypeID",axis=1,inplace=True)
 test_data.drop("WheelTypeID",axis=1,inplace=True )
model_30 = create_model(out=30)
 model_8 = create_model(out=8 )<choose_model_class>
train_data["WheelType"].value_counts(dropna=False )
LR_callback = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=4, verbose=10, factor=.4, min_lr=.00001)
 EarlyStop_callback = keras.callbacks.EarlyStopping(patience=15, restore_best_weights=True )<train_model>
train_data["WheelType"].fillna(value="WheelType_unk",inplace=True)
 test_data["WheelType"].fillna(value="WheelType_unk",inplace=True )
history = model_8.fit(X_train_8,Y_train_8,validation_split=.1,batch_size=64,epochs=50,callbacks=[LR_callback,EarlyStop_callback] )<train_model>
train_data["Nationality"].value_counts(dropna=False )
history = model_30.fit(X_train_30,Y_train_30,validation_split=.1,batch_size=64,epochs=50,callbacks=[LR_callback,EarlyStop_callback])
<predict_on_test>
train_data["Nationality"].fillna(value="Nationality_unk",inplace=True)
 test_data["Nationality"].fillna(value="Nationality_unk",inplace=True )
y_hat_30 = model_30.predict(x)
 y_hat_8 = model_8.predict(x)
 print('Predictions shape', y_hat_30.shape)
 print('Predictions shape', y_hat_8.shape )<categorify>
train_data["Size"].value_counts(dropna=False )
feature_8_index=[0,1,2,3,20,21,28,29]
for i in range(8):
y_hat_30[:,feature_8_index[i]] = y_hat_8[:,i]<define_variables>
train_data["Size"].fillna(value="Size_unk",inplace=True)
 test_data["Size"].fillna(value="Size_unk",inplace=True )
required_features = list(lookid_data['FeatureName'])
 imageID = list(lookid_data['ImageId']-1)
 feature_to_num = dict(zip(required_features[0:30], range(30)) )<define_variables>
train_data["TopThreeAmericanName"].value_counts(dropna=False )
feature_ind = []
for f in required_features:
feature_ind.append(feature_to_num[f] )<define_variables>
train_data["TopThreeAmericanName"].fillna(value="TOP_unk",inplace=True)
 test_data["TopThreeAmericanName"].fillna(value="TOP_unk",inplace=True )
required_pred = []
for x,y in zip(imageID,feature_ind):
required_pred.append(y_hat_30[x, y] )<save_to_csv>
train_data["PRIMEUNIT"].value_counts(dropna=False )
rowid = lookid_data['RowId']
loc30 = pd.Series(required_pred,name = 'Location')
 submission = pd.concat([rowid,loc30],axis = 1)
 submission.to_csv('Predictions.csv',index = False )<set_options>
train_data["PRIMEUNIT"].fillna(value="Prime_unk",inplace=True)
 test_data["PRIMEUNIT"].fillna(value="Prime_unk",inplace=True )
keras.backend.set_image_data_format('channels_last' )<load_from_csv>
train_data["AUCGUART"].value_counts(dropna=False )
mnist_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 mnist_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 X_train = mnist_train.drop('label', axis=1 ).copy()
X_test = mnist_test.copy()
Y_train = mnist_train['label'].copy()<train_model>
test_data["AUCGUART"].value_counts(dropna=False )
print(f"Training Set Size: {X_train.shape}")
 print(f"Test Set Size: {X_test.shape}")
 print(f"Label Size: {Y_train.shape}" )<feature_engineering>
train_data["AUCGUART"].fillna(value="AU_unk",inplace=True)
 test_data["AUCGUART"].fillna(value="AU_unk",inplace=True )
X_train = X_train / 255.0
X_test = X_test / 255.0<prepare_x_and_y>
train_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1)
 test_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1 )
X_train = X_train.values.reshape(-1, 28, 28, 1)
 X_test = X_test.values.reshape(-1, 28, 28, 1 )<split>
train_data["PurchDate"].value_counts()
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.1 )<categorify>
train_data.drop("PurchDate",axis=1,inplace=True)
 test_data.drop("PurchDate",axis=1,inplace=True )
Y_train = keras.utils.to_categorical(Y_train, num_classes=10)
 Y_val = keras.utils.to_categorical(Y_val, num_classes=10 )<choose_model_class>
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'
def CNN() :
model = keras.Sequential()
model.add(layers.Conv2D(32,(3, 3),(1, 1), padding='valid', input_shape=(28, 28, 1), name='conv2d_1_1'))
model.add(layers.Conv2D(32,(3, 3),(1, 1), padding='same', name='conv2d_1_2'))
model.add(layers.BatchNormalization(name='bn_1'))
model.add(layers.Activation('relu', name='relu_1'))
model.add(layers.MaxPooling2D(( 2, 2),(2, 2), padding='valid', name='mp2d_1'))
model.add(layers.Dropout(0.2, name='drop_1'))
model.add(layers.Conv2D(64,(3, 3),(1, 1), padding='valid', name='conv2d_2_1'))
model.add(layers.Conv2D(64,(3, 3),(1, 1), padding='same', name='conv2d_2_2'))
model.add(layers.BatchNormalization(name='bn_2'))
model.add(layers.Activation('relu', name='relu_2'))
model.add(layers.MaxPooling2D(( 2, 2),(2, 2), padding='valid', name='mp2d_2'))
model.add(layers.Dropout(0.2, name='drop_2'))
model.add(layers.Flatten())
 model.add(layers.Dense(100, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
return model<choose_model_class>
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']
not_categorical
model = CNN()<choose_model_class>
for i in not_categorical:
maxium = max(train_data[i])
 train_data[i] = train_data[i]/maxium
maxium_test = max(test_data[i])
 test_data[i] = test_data[i]/maxium_test
model.compile(optimizer='adam', loss='CategoricalCrossentropy', metrics=['accuracy'] )<train_model>
categorical = train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes =='object']
categorical
history = model.fit(X_train, Y_train, validation_data=(X_val, Y_val), batch_size=64, epochs=80, verbose=1 )<predict_on_test>
train_data[categorical[0]]
def predict(model, X, imgs):
s = int(np.sqrt(imgs))
fig, ax = plt.subplots(s, s, sharex=True, sharey=True, figsize=(15, 15))
ax = ax.flatten()
preds = model.predict(X[:imgs])
 for i in range(imgs):
y_pred = np.argmax(preds[i])
 img = X[i].reshape(28, 28)
 ax[i].imshow(img, cmap='Greys', interpolation='nearest')
 ax[i].set_title(f'p: {y_pred}' )<predict_on_test>
pd.get_dummies(train_data[categorical[0]] )
predict(model, X_test, 25 )<predict_on_test>
for i in categorical:
dummies=pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,axis=1,inplace=True)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,axis=1,inplace=True )
y_pred = model.predict(X_test)
 y_pred = np.argmax(y_pred, axis=1 )<save_to_csv>
for i in train_data.drop("IsBadBuy",axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))
y_pred = pd.Series(y_pred, name='Label')
 sub = pd.concat([pd.Series(range(1, 28001), name="ImageId"), y_pred], axis=1)
 sub.to_csv('mnist_submission.csv', index=False )<import_modules>
for i in test_data:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))
from optuna.integration import lightgbm as lgb<load_from_csv>
X = train_data.drop(['RefId','IsBadBuy'],axis=1)
 y = train_data['IsBadBuy']
train=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 train.head()<define_variables>
X_train,X_test,y_train,y_test = train_test_split(X,y )
cols =('MSZoning', 'Street', 'Alley', 'LotShape', 'LandContour', 'Utilities',
'LotConfig', 'LandSlope', 'Neighborhood', 'Condition1', 'Condition2',
'BldgType', 'HouseStyle', 'RoofStyle', 'RoofMatl', 'Exterior1st',
'Exterior2nd', 'MasVnrType', 'ExterQual', 'ExterCond', 'Foundation',
'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2',
'Heating', 'HeatingQC', 'CentralAir', 'Electrical', 'KitchenQual',
'Functional', 'FireplaceQu', 'GarageType', 'GarageFinish', 'GarageQual',
'GarageCond', 'PavedDrive', 'PoolQC', 'Fence', 'MiscFeature',
'SaleType', 'SaleCondition')
 cols_defaults =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(train[c].values))
train[c] = lbl.transform(list(train[c].values))
print('Shape train: {}'.format(train.shape))<categorify>
knn = KNeighborsClassifier(n_neighbors=7)
 knn.fit(X_train,y_train)
 knn.score(X_test,y_test )
for c in cols:
lbl = LabelEncoder()
lbl.fit(list(test[c].values))
test[c] = lbl.transform(list(test[c].values))
print('Shape test: {}'.format(test.shape))<concatenate>
prediction = knn.predict(test_data.drop("RefId",axis=1))
train_test=pd.concat([train,test],axis=0,sort=False)
 train_test.head()<count_missing_values>
submission = pd.DataFrame(data=prediction,columns=["IsBadBuy"])
 submission["RefId"] = test_data["RefId"]
submission.set_index("RefId",inplace=True )
train.isnull().sum()<groupby>
submission.to_csv("submission.csv" )
train_test.groupby('SaleCondition' ).count() ['Id']<sort_values>
train_data = pd.read_csv("/kaggle/input/DontGetKicked/training.csv")
 train_data.head()
corr_dict=correlation_train['SalePrice'].sort_values(ascending=False ).to_dict()
unimportant_columns=[]
for key,value in corr_dict.items() :
if(( value>=-0.1)&(value<=0)) |(value==0.1):
unimportant_columns.append(key)
 unimportant_columns<drop_column>
test_data = pd.read_csv("/kaggle/input/DontGetKicked/test.csv")
 test_data.head()
train2= train.drop(['BsmtFinSF2',
'Utilities',
'BsmtHalfBath',
'MiscVal',
'Id',
'LowQualFinSF',
'YrSold',
'SaleType',
'LotConfig',
'OverallCond',
'MSSubClass',
'BldgType',
'Heating'],axis=1 )<filter>
train_data.isnull().sum()
train3= train.loc[:,['MSZoning', 'LotFrontage', 'LotArea', 'Street', 'Alley', 'LotShape',
'LandContour', 'LandSlope',
'Condition1', 'Condition2',
'HouseStyle', 'OverallQual', 'YearBuilt', 'YearRemodAdd', 'RoofStyle',
'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType', 'MasVnrArea',
'ExterQual', 'ExterCond', 'Foundation', 'BsmtQual', 'BsmtCond',
'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1', 'BsmtFinType2',
'BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC', 'CentralAir', 'Electrical',
'2ndFlrSF', 'GrLivArea', 'BsmtFullBath', 'FullBath',
'HalfBath', 'BedroomAbvGr', 'KitchenAbvGr', 'KitchenQual',
'Functional', 'Fireplaces', 'FireplaceQu', 'GarageType',
'GarageFinish',
'GarageArea', 'GarageQual',
'GarageCond', 'PavedDrive', 'WoodDeckSF', 'OpenPorchSF',
'EnclosedPorch', '3SsnPorch', 'ScreenPorch',
'PoolQC',
'Fence', 'MiscFeature',
'SaleCondition', 'SalePrice']] 
<sort_values>
test_data.isnull().sum()
corr_dict3=correlation_train3['SalePrice'].sort_values(ascending=False ).to_dict()
unimportant_columns_train3=[]
for key,value in corr_dict3.items() :
if(( value>=-0.1)&(value<=0)) |(value==0.1):
unimportant_columns_train3.append(key)
 unimportant_columns_train3<set_options>
train_data['IsBadBuy'].value_counts()
%matplotlib inline<drop_column>
train_data['Model'].value_counts()
train_data.drop("Model",axis=1,inplace=True)
 test_data.drop("Model",axis=1,inplace=True )
test= test.loc[:,['MSZoning', 'LotFrontage', 'LotArea', 'Street', 'Alley', 'LotShape',
'LandContour', 'LandSlope',
'Condition1', 'Condition2',
'HouseStyle', 'OverallQual', 'YearBuilt', 'YearRemodAdd', 'RoofStyle',
'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType', 'MasVnrArea',
'ExterQual', 'ExterCond', 'Foundation', 'BsmtQual', 'BsmtCond',
'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1', 'BsmtFinType2',
'BsmtUnfSF', 'TotalBsmtSF', 'HeatingQC', 'CentralAir', 'Electrical',
'2ndFlrSF', 'GrLivArea', 'BsmtFullBath', 'FullBath',
'HalfBath', 'BedroomAbvGr', 'KitchenAbvGr', 'KitchenQual',
'Functional', 'Fireplaces', 'FireplaceQu', 'GarageType',
'GarageFinish',
'GarageArea', 'GarageQual',
'GarageCond', 'PavedDrive', 'WoodDeckSF', 'OpenPorchSF',
'EnclosedPorch', '3SsnPorch', 'ScreenPorch',
'PoolQC',
'Fence', 'MiscFeature',
'SaleCondition'
]]<prepare_x_and_y>
train_data.drop("Trim",axis=1,inplace=True)
 test_data.drop("Trim",axis=1,inplace=True)
 train_data.drop("SubModel",axis=1,inplace=True)
 test_data.drop("SubModel",axis=1,inplace=True )
X_train = train3.drop([ 'SalePrice'], axis=1)
 y_train = np.log1p(train3['SalePrice'] )<split>
train_data["Color"].value_counts(dropna=False )
y_preds = []
models = []
oof_train = np.zeros(( len(X_train),))
cv = KFold(n_splits=5, shuffle=True, random_state=0)
 params = {
'objective': 'regression',
'metric': 'rmse',
'verbosity': -1,
'boosting_type': 'gbdt',
}
for fold_id,(train_index, valid_index)in enumerate(cv.split(X_train)) :
X_tr = X_train.loc[train_index, :]
X_val = X_train.loc[valid_index, :]
y_tr = y_train[train_index]
y_val = y_train[valid_index]
lgb_train = lgb.Dataset(X_tr,
y_tr, 
)
 lgb_eval = lgb.Dataset(X_val,
y_val,
reference=lgb_train, 
)
 model = lgb.train(params,
lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval=10,
num_boost_round=1000,
early_stopping_rounds=10)
 oof_train[valid_index] = model.predict(X_val,
num_iteration=model.best_iteration)
 y_pred = model.predict(test,
num_iteration=model.best_iteration)
 y_preds.append(y_pred)
 models.append(model )<compute_test_metric>
test_data["Color"].value_counts(dropna=False )
print(f'CV: {np.sqrt(mean_squared_error(y_train, oof_train)) }' )<save_to_csv>
train_data["Color"].fillna(value="ColorUnknown",inplace=True)
 test_data["Color"].fillna(value="ColorUnknown",inplace=True )
sub = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 y_sub = sum(y_preds)/ len(y_preds)
 y_sub = np.expm1(y_sub)
 sub['SalePrice'] = y_sub
sub.to_csv('submission.csv', index=False)
 sub.head()<import_modules>
train_data["Transmission"].value_counts(dropna=False )
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.callbacks import LearningRateScheduler, ModelCheckpoint, ReduceLROnPlateau
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout,Dense,Activation,Conv2D,MaxPooling2D,Flatten, BatchNormalization, MaxPool2D
from tensorflow.keras.metrics import AUC
from kerastuner.tuners import RandomSearch
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator 
<load_from_csv>
train_data["Transmission"].replace("Manual","MANUAL",inplace=True )
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 x_test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 x_train = train.drop(columns = 'label')
 y_train = train['label']
x_train.shape
counter = 0<prepare_x_and_y>
test_data["Transmission"].value_counts(dropna=False )
x_train = x_train.values.reshape(-1, int(np.sqrt(784)) , int(np.sqrt(784)) , 1)/255.0
x_test = x_test.values.reshape(-1, int(np.sqrt(784)) , int(np.sqrt(784)) , 1)/255.0<categorify>
train_data["Transmission"].fillna(value="Trans_unk",inplace=True)
 test_data["Transmission"].fillna(value="Trans_unk",inplace=True )
y_train = to_categorical(y_train, num_classes=10 )<split>
train_data["WheelTypeID"].value_counts(dropna=False)
 train_data.drop("WheelTypeID",axis=1,inplace=True)
 test_data.drop("WheelTypeID",axis=1,inplace=True )
x_train_trim ,x_valid , y_train_trim, y_valid = train_test_split(x_train, y_train, test_size= 0.1 ,random_state = 1455)
 print(f'Training Set size: {x_train_trim.shape[0]}')
 print(f'Validation Set size: {x_valid.shape[0]}' )<choose_model_class>
train_data["WheelType"].value_counts(dropna=False )
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
train_data["WheelType"].fillna(value="WheelType_unk",inplace=True)
 test_data["WheelType"].fillna(value="WheelType_unk",inplace=True )
def build_lrfn(lr_start=1e-4, lr_max=1e-3,
lr_min=0, lr_rampup_epochs=16,
lr_sustain_epochs=0, lr_exp_decay=.8):
def lrfn(epoch):
if epoch < lr_rampup_epochs:
lr =(lr_max - lr_start)/ lr_rampup_epochs * epoch + lr_start
elif epoch < lr_rampup_epochs + lr_sustain_epochs:
lr = lr_max
else:
lr =(lr_max - lr_min)*\
lr_exp_decay**(epoch - lr_rampup_epochs\
- lr_sustain_epochs)+ lr_min
return lr
return lrfn
lrfn = build_lrfn()
lr_schedule = LearningRateScheduler(lrfn, verbose=True)
 checkpoint = ModelCheckpoint(
 filepath='best_weights.hdf5',
save_weights_only=True,
monitor='accuracy',
mode='max',
save_best_only=True)
 optimizer_rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<init_hyperparams>
train_data["Nationality"].value_counts(dropna=False )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(x_train_trim )<train_model>
train_data["Nationality"].fillna(value="Nationality_unk",inplace=True)
 test_data["Nationality"].fillna(value="Nationality_unk",inplace=True )
model.compile(optimizer=optimizer_rmsprop,
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
 batch_size = 512
epochs = 40

 train_history = model.fit_generator(datagen.flow(x_train_trim, y_train_trim, batch_size=batch_size),
epochs = epochs, validation_data =(x_valid,y_valid),
verbose = 2, steps_per_epoch=x_train_trim.shape[0] // batch_size
, callbacks=[lr_schedule, checkpoint] )<train_model>
train_data["Size"].value_counts(dropna=False )
visualize_training(train_history )<predict_on_test>
train_data["Size"].fillna(value="Size_unk",inplace=True)
 test_data["Size"].fillna(value="Size_unk",inplace=True )
model.load_weights('./best_weights.hdf5')
 predictions_probs = model.predict(x_test)
 predictions = np.argmax(predictions_probs, axis = 1)
 counter = 0<save_to_csv>
train_data["TopThreeAmericanName"].value_counts(dropna=False )
ImageId = np.arange(1,x_test.shape[0] + 1)
 output = pd.DataFrame({'ImageId':ImageId, 'Label':predictions})
 output.to_csv('submission.csv', index=False)
 print(output )<set_options>
train_data["TopThreeAmericanName"].fillna(value="TOP_unk",inplace=True)
 test_data["TopThreeAmericanName"].fillna(value="TOP_unk",inplace=True )
%matplotlib inline<load_from_csv>
train_data["PRIMEUNIT"].value_counts(dropna=False )
train_df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 n_pixels = len(train_df.columns)- 1<define_variables>
train_data["PRIMEUNIT"].fillna(value="Prime_unk",inplace=True)
 test_data["PRIMEUNIT"].fillna(value="Prime_unk",inplace=True )
BATCH_SIZE = 64
EPOCHS = 50
VALID_SIZE = 0.15<load_from_csv>
train_data["AUCGUART"].value_counts(dropna=False )
class DatasetMNIST(torch.utils.data.Dataset):
def __init__(self, file_path,
transform = transforms.Compose([transforms.ToPILImage() , transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
):
df = pd.read_csv(file_path)
 if len(df.columns)== n_pixels:
self.X = df.values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = None
else:
self.X = df.iloc[:,1:].values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = torch.from_numpy(df.iloc[:,0].values)
 self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
if self.y is not None:
return self.transform(self.X[idx]), self.y[idx]
else:
return self.transform(self.X[idx] )<load_from_csv>
test_data["AUCGUART"].value_counts(dropna=False )
transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomRotation(0, 0.5),
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,))
])
 train_data = DatasetMNIST('.. /input/digit-recognizer/train.csv', transform= transform)
 valid_train_data = DatasetMNIST('.. /input/digit-recognizer/train.csv', transform= transform)
 test_data = DatasetMNIST('.. /input/digit-recognizer/test.csv', transform= transform)
 num_train = len(train_data)
 indices = list(range(num_train))
np.random.shuffle(indices)
 split = int(np.floor(VALID_SIZE * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 train_loader = torch.utils.data.DataLoader(dataset=train_data,
batch_size=BATCH_SIZE,sampler=train_sampler)
 valid_loader = torch.utils.data.DataLoader(dataset=valid_train_data,
batch_size=BATCH_SIZE,sampler=valid_sampler)
 test_loader = torch.utils.data.DataLoader(dataset=test_data,
batch_size=BATCH_SIZE, shuffle=False )<choose_model_class>
train_data["AUCGUART"].fillna(value="AU_unk",inplace=True)
 test_data["AUCGUART"].fillna(value="AU_unk",inplace=True )
class CNN(nn.Module):
def __init__(self):
super(CNN,self ).__init__()
self.conv = nn.Sequential(
 nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout(p = 0.25),
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout(p = 0.25), 
)
 self.fc = nn.Sequential(
 nn.Linear(64 * 7 * 7, 512),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Dropout(p = 0.5),
nn.Linear(512, 10), 
)
 for m in self.conv.children() :
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2./ n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
 m.bias.data.zero_()
for m in self.fc.children() :
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
 elif isinstance(m, nn.BatchNorm1d):
m.weight.data.fill_(1)
 m.bias.data.zero_()
def forward(self, x):
x= self.conv(x)
 x = x.view(x.size(0), -1)
 x = self.fc(x)
 return F.log_softmax(x, dim=1)
<choose_model_class>
train_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1)
 test_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1 )
model = CNN()
print(model )<choose_model_class>
train_data["PurchDate"].value_counts()
optimizer = torch.optim.Adam(model.parameters() , lr=0.001)
 exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
<choose_model_class>
train_data.drop("PurchDate",axis=1,inplace=True)
 test_data.drop("PurchDate",axis=1,inplace=True )
error = nn.CrossEntropyLoss()<set_options>
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'
if torch.cuda.is_available() :
model = model.cuda()
error = error.cuda()<train_model>
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']
not_categorical
def fit(epoch):
valid_loss_min = np.Inf
running_loss= 0
accuracy_train = 0
model.train()
exp_lr_scheduler.step()
for batch_idx,(data, target)in enumerate(train_loader):
data, target = Variable(data), Variable(target)
 if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
optimizer.zero_grad()
output = model(data)
 loss = error(output, target)
 loss.backward()
optimizer.step()
running_loss += loss.item()
_, top_class = output.topk(1, dim=1)
 equals = top_class == target.view(*top_class.shape)
 accuracy_train += torch.mean(equals.type(torch.FloatTensor))
valid_loss = 0
accuracy_val = 0
with torch.no_grad() :
model.eval()
for data, target in valid_loader:
data, target = Variable(data), Variable(target)
 if torch.cuda.is_available() :
data, target = data.cuda() , target.cuda()
output = model(data)
 _, top_class = output.topk(1, dim=1)
 equals = top_class == target.view(*top_class.shape)
 loss = error(output, target)
 valid_loss += loss.item()
accuracy_val += torch.mean(equals.type(torch.FloatTensor))
model.train()
train_losses.append(running_loss/len(train_loader))
valid_losses.append(valid_loss/len(valid_loader))
history_accuracy_val.append(accuracy_val/len(valid_loader))
history_accuracy_train.append(accuracy_train/len(train_loader))
network_learned = valid_loss < valid_loss_min
if epoch == 1 or epoch % 5 == 0 or network_learned:
print(f"Epoch: {epoch}/{EPOCHS}.. ",
f"Training Loss: {running_loss/len(train_loader):.3f}.. ",
f"Validation Loss: {valid_loss/len(valid_loader):.3f}.. ",
f"Training Accuracy: {accuracy_train/len(train_loader):.3f}.. ",
f"Validation Accuracy: {accuracy_val/len(valid_loader):.3f}")
 if network_learned:
valid_loss_min = valid_loss
torch.save(model.state_dict() , 'model_mtl_mnist.pt')
 print('Detected network improvement, saving current model')
<train_model>
for i in not_categorical:
maxium = max(train_data[i])
 train_data[i] = train_data[i]/maxium
maxium_test = max(test_data[i])
 test_data[i] = test_data[i]/maxium_test
train_losses, valid_losses = [], []
history_accuracy_train,history_accuracy_val = [], []
for epoch in range(EPOCHS):
fit(epoch )<predict_on_test>
categorical = train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes =='object']
categorical
def prediciton() :
model.eval()
test_pred = torch.LongTensor()
for i,data in enumerate(test_loader):
data = Variable(data, requires_grad=True)
 if torch.cuda.is_available() :
data = data.cuda()
output = model(data)
 pred = output.cpu().data.max(1, keepdim=True)[1]
test_pred = torch.cat(( test_pred, pred), dim=0)
 return test_pred<create_dataframe>
train_data[categorical[0]]
test_pred = prediciton()
out_df = pd.DataFrame(np.c_[np.arange(1, len(test_data)+1)[:,None], test_pred.numpy() ],
columns=['ImageId', 'Label'])
 print(out_df )<save_to_csv>
pd.get_dummies(train_data[categorical[0]] )
out_df.to_csv('submission.csv', index=False )<train_model>
for i in categorical:
dummies=pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,axis=1,inplace=True)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,axis=1,inplace=True )
class EarlyStopping:

 def __init__(self, patience=7, verbose=False, delta=0, path='checkpoint.pt'):

 self.patience = patience
self.verbose = verbose
self.counter = 0
self.best_score = None
self.early_stop = False
self.val_loss_min = np.Inf
self.delta = delta
self.path = path
def __call__(self, val_loss, model):
score = -val_loss
if self.best_score is None:
self.best_score = score
self.save_checkpoint(val_loss, model)
 elif score < self.best_score + self.delta:
self.counter += 1
print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
 if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = score
self.save_checkpoint(val_loss, model)
 self.counter = 0
def save_checkpoint(self, val_loss, model):

 if self.verbose:
print(f'Validation loss decreased({self.val_loss_min:.6f} --> {val_loss:.6f} ).Saving model...')
 torch.save(model.state_dict() , self.path)
 self.val_loss_min = val_loss<set_options>
for i in train_data.drop("IsBadBuy",axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))
warnings.filterwarnings('ignore' )<set_options>
for i in test_data:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))
def seed_everything(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 os.environ['PYTHONHASHSEED'] = str(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed = 42
seed_everything(seed )<feature_engineering>
X = train_data.drop(['RefId','IsBadBuy'],axis=1)
 y = train_data['IsBadBuy']
class TweetDataset(torch.utils.data.Dataset):
def __init__(self, df, max_len=96):
self.df = df
self.max_len = max_len
self.labeled = 'selected_text' in df
self.tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file='.. /input/roberta-base/vocab.json',
merges_file='.. /input/roberta-base/merges.txt',
lowercase=True,
add_prefix_space=True)
 def __getitem__(self, index):
data = {}
row = self.df.iloc[index]
ids, masks, tweet, offsets = self.get_input_data(row)
 data['ids'] = ids
data['masks'] = masks
data['tweet'] = tweet
data['offsets'] = offsets
if self.labeled:
start_idx, end_idx = self.get_target_idx(row, tweet, offsets)
 data['start_idx'] = start_idx
data['end_idx'] = end_idx
return data
def __len__(self):
return len(self.df)
 def get_input_data(self, row):
tweet = " " + " ".join(row.text.lower().split())
 encoding = self.tokenizer.encode(tweet)
 sentiment_id = self.tokenizer.encode(row.sentiment ).ids
ids = [0] + sentiment_id + [2, 2] + encoding.ids + [2]
offsets = [(0, 0)] * 4 + encoding.offsets + [(0, 0)]
pad_len = self.max_len - len(ids)
 if pad_len > 0:
ids += [1] * pad_len
offsets += [(0, 0)] * pad_len
ids = torch.tensor(ids)
 masks = torch.where(ids != 1, torch.tensor(1), torch.tensor(0))
offsets = torch.tensor(offsets)
 return ids, masks, tweet, offsets
def get_target_idx(self, row, tweet, offsets):
selected_text = " " + " ".join(row.selected_text.lower().split())
 len_st = len(selected_text)- 1
idx0 = None
idx1 = None
for ind in(i for i, e in enumerate(tweet)if e == selected_text[1]):
if " " + tweet[ind: ind+len_st] == selected_text:
idx0 = ind
idx1 = ind + len_st - 1
break
char_targets = [0] * len(tweet)
 if idx0 != None and idx1 != None:
for ct in range(idx0, idx1 + 1):
char_targets[ct] = 1
target_idx = []
for j,(offset1, offset2)in enumerate(offsets):
if sum(char_targets[offset1: offset2])> 0:
target_idx.append(j)
 start_idx = target_idx[0]
end_idx = target_idx[-1]
return start_idx, end_idx
def get_train_val_loaders(df, train_idx, val_idx, batch_size=8):
train_df = df.iloc[train_idx]
val_df = df.iloc[val_idx]
train_loader = torch.utils.data.DataLoader(
 TweetDataset(train_df),
batch_size=batch_size,
shuffle=True,
num_workers=2,
drop_last=True)
 val_loader = torch.utils.data.DataLoader(
 TweetDataset(val_df),
batch_size=batch_size,
shuffle=False,
num_workers=2)
 dataloaders_dict = {"train": train_loader, "val": val_loader}
return dataloaders_dict
def get_test_loader(df, batch_size=32):
loader = torch.utils.data.DataLoader(
 TweetDataset(df),
batch_size=batch_size,
shuffle=False,
num_workers=2)
 return loader<load_pretrained>
X_train,X_test,y_train,y_test = train_test_split(X,y )
class TweetModel(nn.Module):
def __init__(self):
super(TweetModel, self ).__init__()
config = RobertaConfig.from_pretrained(
 '.. /input/roberta-base/config.json', output_hidden_states=True)
 self.roberta = RobertaModel.from_pretrained(
 '.. /input/roberta-base/pytorch_model.bin', config=config)
 self.dropout = nn.Dropout(0.5)
 self.fc = nn.Linear(config.hidden_size, 2)
 nn.init.normal_(self.fc.weight, std=0.02)
 nn.init.normal_(self.fc.bias, 0)
 def forward(self, input_ids, attention_mask):
_, _, hs = self.roberta(input_ids, attention_mask)
 x = torch.stack([hs[-1], hs[-2], hs[-3], hs[-4]])
 x = torch.mean(x, 0)
 x = self.dropout(x)
 x = self.fc(x)
 start_logits, end_logits = x.split(1, dim=-1)
 start_logits = start_logits.squeeze(-1)
 end_logits = end_logits.squeeze(-1)
 return start_logits, end_logits<compute_train_metric>
knn = KNeighborsClassifier(n_neighbors=7)
 knn.fit(X_train,y_train)
 knn.score(X_test,y_test )
class LabelSmoothLoss(nn.Module):
def __init__(self, smoothing=0.0):
super(LabelSmoothLoss, self ).__init__()
self.smoothing = smoothing
def forward(self, input, target):
log_prob = F.log_softmax(input, dim=-1)
 weight = input.new_ones(input.size())* \
self.smoothing /(input.size(-1)- 1.)
weight.scatter_(-1, target.unsqueeze(-1),(1.- self.smoothing))
loss =(-weight * log_prob ).sum(dim=-1 ).mean()
return loss
def loss_fn(start_logits, end_logits, start_positions, end_positions):
loss_fct = LabelSmoothLoss(smoothing=0.1)
 start_loss = loss_fct(start_logits, start_positions)
 end_loss = loss_fct(end_logits, end_positions)
 total_loss =(start_loss + end_loss)
 return total_loss<statistical_test>
prediction = knn.predict(test_data.drop("RefId",axis=1))
def get_selected_text(text, start_idx, end_idx, offsets):
selected_text = ""
for ix in range(start_idx, end_idx + 1):
selected_text += text[offsets[ix][0]: offsets[ix][1]]
if(ix + 1)< len(offsets)and offsets[ix][1] < offsets[ix + 1][0]:
selected_text += " "
return selected_text
def jaccard(str1, str2):
a = set(str1.lower().split())
 b = set(str2.lower().split())
 c = a.intersection(b)
 return float(len(c)) /(len(a)+ len(b)- len(c))
def compute_jaccard_score(text, start_idx, end_idx, start_logits, end_logits, offsets):
start_pred = np.argmax(start_logits)
 end_pred = np.argmax(end_logits)
 if start_pred > end_pred:
pred = text
else:
pred = get_selected_text(text, start_pred, end_pred, offsets)
 true = get_selected_text(text, start_idx, end_idx, offsets)
 return jaccard(true, pred )<train_model>
submission = pd.DataFrame(data=prediction,columns=["IsBadBuy"])
 submission["RefId"] = test_data["RefId"]
submission.set_index("RefId",inplace=True )
def train_model(model, dataloaders_dict, criterion, optimizer, num_epochs, filename):
model.cuda()
early = EarlyStopping(patience=2,path=filename)
 for epoch in range(num_epochs):
for phase in ['train', 'val']:
if phase == 'train':
model.train()
else:
model.eval()
epoch_loss = 0.0
epoch_jaccard = 0.0
for data in(dataloaders_dict[phase]):
ids = data['ids'].cuda()
masks = data['masks'].cuda()
tweet = data['tweet']
offsets = data['offsets'].numpy()
start_idx = data['start_idx'].cuda()
end_idx = data['end_idx'].cuda()
optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
start_logits, end_logits = model(ids, masks)
 loss = criterion(start_logits, end_logits, start_idx, end_idx)
 if phase == 'train':
loss.backward()
optimizer.step()
epoch_loss += loss.item() * len(ids)
 start_idx = start_idx.cpu().detach().numpy()
end_idx = end_idx.cpu().detach().numpy()
start_logits = torch.softmax(start_logits, dim=1 ).cpu().detach().numpy()
end_logits = torch.softmax(end_logits, dim=1 ).cpu().detach().numpy()
for i in range(len(ids)) :
jaccard_score = compute_jaccard_score(
 tweet[i],
start_idx[i],
end_idx[i],
start_logits[i],
end_logits[i],
offsets[i])
 epoch_jaccard += jaccard_score
epoch_loss = epoch_loss / len(dataloaders_dict[phase].dataset)
 epoch_jaccard = epoch_jaccard / len(dataloaders_dict[phase].dataset)
 print('Epoch {}/{} | {:^5} | Loss: {:.4f} | Jaccard: {:.4f}'.format(
 epoch + 1, num_epochs, phase, epoch_loss, epoch_jaccard))
early(1 - epoch_jaccard, model)
 if early.early_stop:
break<choose_model_class>
submission.to_csv("submission.csv" )