Kaggle competition: Don't Get Kicked!
full['Ticket'] = full['Ticket'].map(getTicket)
 ticketDf = pd.get_dummies(full['Ticket'],prefix='Ticket')
 full = pd.concat([full,ticketDf],axis=1)
 full.drop(['Ticket'],axis=1,inplace=True)
 full.head()<import_modules>
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectFromModel<drop_column>
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()
full_noid = full.drop(['PassengerId'],axis=1 )<prepare_x_and_y>
train_data.isnull().sum()
def reverse(full):
full_c = full.copy(deep=True)
 type(full_c)
 train_num= 891
train_X = full_c.drop(columns ='Survived',axis=1 ).loc[0:train_num-1,:]
train_y = full.loc[0:train_num-1,'Survived']
pred_X = full_c.drop(columns ='Survived',axis=1 ).loc[train_num:,:]
return train_X,train_y,pred_X<prepare_x_and_y>
test_data.isnull().sum()
train_X,train_y,pred_X = reverse(full_noid )<train_model>
test_data["Transmission"].value_counts()
clf = RandomForestClassifier(n_estimators=100,max_features='sqrt')
 clf = clf.fit(train_X,train_y )<categorify>
train_data["Transmission"].replace("Manual","MANUAL",inplace=True )
slf = SelectFromModel(clf,prefit=True)
 train_X_new = slf.transform(train_X)
 print('特征选择前训练特征个数：',train_X.shape[1],
'
特征选择后训练特征个数：',train_X_new.shape[1])
 pred_X_new = slf.transform(pred_X)
 print('特征选择前预测特征个数：',pred_X.shape[1],
'
特征选择后预测特征个数：',pred_X_new.shape[1] )<import_modules>
train_data["Color"].fillna(value="Color_unk",inplace=True )
from sklearn.linear_model import LogisticRegression,LogisticRegressionCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble.gradient_boosting import GradientBoostingClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier,XGBRFClassifier
from sklearn.model_selection import cross_val_score<define_search_model>
test_data["Color"].fillna(value="Color_unk",inplace=True )
lr = LogisticRegression()
rfc = RandomForestClassifier()
gboosting = GradientBoostingClassifier()
svc = SVC()
xgb = XGBClassifier()
xgbrfc = XGBRFClassifier()
models = [lr,rfc,gboosting,svc,xgb,xgbrfc]<compute_train_metric>
train_data["SubModel"].value_counts()
for model in models:
print('Cross validation of:{0}'.format(model.__class__))
score = cross_val_score(model,X_train,y_train,cv=5,scoring='accuracy')
 score = score.mean()
print('score = {0}'.format(score))<import_modules>
train_data["Transmission"].fillna(value="Transmission_unk",inplace=True )
from sklearn.model_selection import GridSearchCV<import_modules>
test_data["Transmission"].fillna(value="Transmission_unk",inplace=True )
from sklearn.model_selection import GridSearchCV<train_on_grid>
train_data["WheelType"].value_counts()
param_test1 = {
'n_estimators':range(20,81,10)
 }
gsearch1 = GridSearchCV(estimator=GradientBoostingClassifier(
 learning_rate=0.1,
max_depth=6,
min_samples_split=5,
min_samples_leaf=2,
max_features='sqrt',
subsample=0.8,
random_state=10 
),param_grid=param_test1,scoring="accuracy",n_jobs=4,iid=False,cv=5 
)
 gsearch1.fit(X_train,y_train)
 n_estimators = gsearch1.best_params_['n_estimators']
print(gsearch1.scoring)
 print(gsearch1.best_params_)
 print(gsearch1.best_score_ )<train_on_grid>
train_data["WheelType"].fillna(value="WheelType_unk",inplace=True )
param_test2 = {
'max_depth':range(2,8),
'min_samples_split':range(2,10)
 }
gsearch2 = GridSearchCV(estimator=GradientBoostingClassifier(
 learning_rate=0.1,
n_estimators=n_estimators,
min_samples_leaf=2,
max_features='sqrt',
subsample=0.8,
random_state=10 
),param_grid=param_test2,scoring="accuracy",n_jobs=4,iid=False,cv=5 
)
 gsearch2.fit(X_train,y_train)
 max_depth = gsearch2.best_params_['max_depth']
print(gsearch2.scoring)
 print(gsearch2.best_params_)
 print(gsearch2.best_score_ )<train_on_grid>
test_data["WheelType"].fillna(value="WheelType_unk",inplace=True )
param_test3 = {
'min_samples_split':range(2,10),
'min_samples_leaf':range(2,20)
 }
gsearch3 = GridSearchCV(estimator=GradientBoostingClassifier(
 learning_rate=0.1,
max_depth=max_depth,
n_estimators=n_estimators,
max_features='sqrt',
subsample=0.8,
random_state=10 
),param_grid=param_test3,scoring="accuracy",n_jobs=4,iid=False,cv=5 
)
 gsearch3.fit(X_train,y_train)
 min_samples_split=gsearch3.best_params_['min_samples_split']
min_samples_leaf=gsearch3.best_params_['min_samples_leaf']
print(gsearch3.scoring)
 print(gsearch3.best_params_)
 print(gsearch3.best_score_ )<train_on_grid>
train_data["Nationality"].fillna(value="Nationality_unk",inplace=True )
param_test4 = {
'subsample':[0.6,0.7,0.75,0.8,0.85,0.9]
}
gsearch4 = GridSearchCV(estimator=GradientBoostingClassifier(
 learning_rate=0.1,
max_depth=max_depth,
n_estimators=n_estimators,
max_features='sqrt',
min_samples_split=min_samples_split,
min_samples_leaf=min_samples_leaf,
random_state=10 
),param_grid=param_test4,scoring="accuracy",n_jobs=4,iid=False,cv=5 
)
 gsearch4.fit(X_train,y_train)
 subsample=gsearch4.best_params_['subsample']
print(gsearch4.scoring)
 print(gsearch4.best_params_)
 print(gsearch4.best_score_ )<train_model>
test_data["Nationality"].fillna(value="Nationality_unk",inplace=True )
gbm=GradientBoostingClassifier(
 learning_rate=0.01,
max_depth=max_depth,
n_estimators=n_estimators*10,
max_features='sqrt',
min_samples_split=min_samples_split,
min_samples_leaf=min_samples_leaf,
subsample=subsample,
random_state=10 
)
 gbm.fit(train_X_new,train_y )<predict_on_test>
train_data["TopThreeAmericanName"].value_counts()
pred_y = gbm.predict(pred_X_new )<data_type_conversions>
train_data["Size"].fillna(value="Size_unk",inplace=True )
pred_y = pred_y.astype(int )<create_dataframe>
test_data["Size"].fillna(value="Size_unk",inplace=True )
passengerId = full.loc[891:,'PassengerId']
predDf = pd.DataFrame(
 {'PassengerId':passengerId,
'Survived':pred_y})
 predDf.shape<save_to_csv>
train_data["TopThreeAmericanName"].fillna(value="TopThreeAmericanName_unk",inplace=True )
predDf.to_csv('titanic_pred.csv',index=False )<load_from_csv>
test_data["TopThreeAmericanName"].fillna(value="TopThreeAmericanName_unk",inplace=True )
train = pd.read_csv(".. /input/train.csv")
 train.shape<load_from_csv>
train_data["PRIMEUNIT"].fillna(value="PRIMEUNIT_unk",inplace=True )
test = pd.read_csv(".. /input/test.csv")
 test.shape<prepare_x_and_y>
test_data["PRIMEUNIT"].fillna(value="PRIMEUNIT_unk",inplace=True )
img_width = 28
img_height = 28
nclasses = 10
classes = np.arange(10)
 labels = [ str(c)for c in classes ]
train_X = train.iloc[:,1:].values.reshape(train.shape[0], img_width, img_height, 1)
 train_y = train.iloc[:,0].values<prepare_x_and_y>
train_data["AUCGUART"].fillna(value="AUCGUART_unk",inplace=True )
test_X = test.iloc[:,0:].values.reshape(test.shape[0], img_width, img_height, 1)
<feature_engineering>
test_data["AUCGUART"].fillna(value="AUCGUART_unk",inplace=True )
train_X = train_X / train_X.max()
test_X = test_X / test_X.max()<import_modules>
train_data.drop(["Model","Trim","SubModel","WheelTypeID","MMRAcquisitionAuctionAveragePrice","MMRAcquisitionAuctionCleanPrice","MMRAcquisitionRetailAveragePrice","MMRAcquisitonRetailCleanPrice","MMRCurrentAuctionAveragePrice","MMRCurrentAuctionCleanPrice","MMRCurrentRetailAveragePrice","MMRCurrentRetailCleanPrice","PurchDate"],axis=1,inplace=True )
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, Dropout, BatchNormalization, MaxPool2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
from tensorflow import set_random_seed 
<train_model>
test_data.drop(["Model","Trim","SubModel","WheelTypeID","MMRAcquisitionAuctionAveragePrice","MMRAcquisitionAuctionCleanPrice","MMRAcquisitionRetailAveragePrice","MMRAcquisitonRetailCleanPrice","MMRCurrentAuctionAveragePrice","MMRCurrentAuctionCleanPrice","MMRCurrentRetailAveragePrice","MMRCurrentRetailCleanPrice","PurchDate"],axis=1,inplace=True )
class MNIST_CNN:

 def build(self, img_rows, img_cols, nclasses=10):
input_shape=(img_rows, img_cols, 1)
 model = Sequential()
model.add(Conv2D(32,(3,3), activation="relu", input_shape=(img_rows, img_cols, 1),
kernel_initializer="glorot_normal", padding="same"))
model.add(Conv2D(32,(3,3), activation="relu",
kernel_initializer="glorot_normal", padding="same"))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3), activation="relu",
kernel_initializer="glorot_normal", padding="same"))
model.add(Conv2D(64,(3,3), activation="relu",
kernel_initializer="glorot_normal", padding="same"))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(128,(3,3), activation="relu",
kernel_initializer="glorot_normal", padding="same"))
model.add(Conv2D(128,(3,3), activation="relu",
kernel_initializer="glorot_normal", padding="same"))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(300, activation="relu"))
model.add(Dropout(0.25))
model.add(Dense(nclasses, activation="softmax"))
self.model = model
def compile(self, **kw):
kw1 = dict(optimizer="adam", loss="categorical_crossentropy", metrics=["categorical_accuracy"])
 kw1.update(kw)
 self.model.compile(**kw1)
 def fit(self, *arg, **kw):
return self.model.fit(*arg, **kw)
 mdl = MNIST_CNN()
mdl.build(img_width, img_height)
 mdl.compile()
mdl.model.summary() 
<init_hyperparams>
not_categorical=train_data.drop(["RefId","IsBadBuy"],axis=1 ).columns[train_data.drop(["RefId","IsBadBuy"],axis=1 ).dtypes!='object']
DEBUG = 0
K = 3 if DEBUG else 10
set_random_seed(777)
 skf = StratifiedKFold(n_splits=K, shuffle=True)
 train_y_cat = to_categorical(train_y)
 histories = []
test_pred = np.zeros(( len(test_X), nclasses), dtype=np.float64)
 train_pred = np.zeros(( len(train_X), nclasses), dtype=np.float64)
 val_pred = np.zeros(( len(train_X), nclasses), dtype=np.float64)
 train_accs = []
val_accs = []
for i,(train_idx, val_idx)in enumerate(skf.split(train_X, train_y)) :
t0 = time.time()
batch_size=256 if DEBUG else 64
mdl = MNIST_CNN()
mdl.build(img_width, img_height)
 mdl.compile()
histories.append(
 mdl.model.fit_generator(
 datagen.flow(train_X[train_idx], train_y_cat[train_idx], batch_size=batch_size),
epochs=5 if DEBUG else 200,
verbose=1 if DEBUG else 0,
steps_per_epoch=len(train_idx)/ batch_size,
validation_data=(train_X[val_idx], train_y_cat[val_idx]), 
), 
)
 test_pred += mdl.model.predict(test_X)
 train_pred += mdl.model.predict(train_X)
 val_pred[val_idx] += mdl.model.predict(train_X[val_idx])
 train_accs.append(accuracy_score(train_y, mdl.model.predict(train_X ).argmax(axis=1)))
 val_accs.append(accuracy_score(train_y[val_idx], val_pred[val_idx].argmax(axis=1)))
 print("******* [dt={0}] accuracy_scores at fold {1}: train={2} val={3}".format(
 time.time() - t0, i + 1, train_accs[-1], val_accs[-1]))
test_y = np.argmax(test_pred, axis=1)
 val_y = np.argmax(val_pred, axis=1 )<compute_test_metric>
for i in not_categorical:
maximum=np.max(train_data[i])
 train_data[i]=train_data[i]/maximum
max_test=np.max(test_data[i])
 test_data[i]=test_data[i]/max_test
print("global fit accuracy_scores: train={0} val={1}".format(
 accuracy_score(train_y, train_pred.argmax(axis=1)) ,
accuracy_score(train_y, val_pred.argmax(axis=1))))
print(f"{K}-Fold train accuracy = {np.mean(train_accs):0.4f} +- {np.std(train_accs):0.4f}")
 print(f"{K}-Fold cross validation accuracy = {np.mean(val_accs):0.4f} +- {np.std(val_accs):0.4f}" )<save_to_csv>
test_data["VehicleAge"].value_counts()
df_test = pd.DataFrame({'ImageId': 1 + np.arange(len(test_X)) ,
'Label': test_y })
 df_test.to_csv('submit.csv', index=False)
<load_from_csv>
categorical=train_data.drop(["RefId","IsBadBuy"],axis=1 ).columns[train_data.drop(["RefId","IsBadBuy"],axis=1 ).dtypes=='object']
PATH = '.. /input/digit-recognizer'
df_train = pd.read_csv(os.path.join(PATH, 'train.csv'))
train_y = df_train['label'].values
train_x = df_train.drop(['label'], axis=1 ).values
df_test = pd.read_csv(os.path.join(PATH, 'test.csv'))
test_x = df_test.values
print(train_x.shape)
 print(train_y.shape)
 print(test_x.shape )<define_variables>
pd.get_dummies(train_data[categorical[0]] )
IMG_SIZE = 32<data_type_conversions>
for i in categorical:
dum=pd.get_dummies(train_data[i])
 dum.columns=str(i)+"_"+dum.columns
train_data=pd.concat([train_data,dum],axis=1)
 train_data.drop(i,inplace=True,axis=1)
 dum=pd.get_dummies(test_data[i])
 dum.columns=str(i)+"_"+dum.columns
test_data=pd.concat([test_data,dum],axis=1)
 test_data.drop(i,inplace=True,axis=1 )
def resize(img_array):
tmp = np.empty(( img_array.shape[0], IMG_SIZE, IMG_SIZE))
for i in range(len(img_array)) :
img = img_array[i].reshape(28, 28 ).astype('uint8')
 img = cv2.resize(img,(IMG_SIZE, IMG_SIZE))
img = img.astype('float32')/255
tmp[i] = img
return tmp
train_x_resize = resize(train_x)
 test_x_resize = resize(test_x )<categorify>
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))
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))
train_y_final = to_categorical(train_y, num_classes=10)
 print(train_y_final.shape )<choose_model_class>
test_data=test_data[train_data.drop("IsBadBuy",axis=1 ).columns]
vgg19 = VGG19(weights = 'imagenet',
include_top = False,
input_shape=(IMG_SIZE, IMG_SIZE, 3)
)
 model = Sequential()
model.add(vgg19)
 model.add(Flatten())
 model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
 model.summary()<split>
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(train_x_final, train_y_final, test_size=0.2, random_state=2019)
 print(x_train.shape)
 print(x_test.shape)
 print(y_train.shape)
 print(y_test.shape )<choose_model_class>
from sklearn.model_selection import train_test_split
es = EarlyStopping(monitor='val_acc', verbose=1, patience=5)
 mc = ModelCheckpoint(filepath='mnist-vgg19.h5', verbose=1, monitor='val_acc')
 cb = [es, mc]<train_model>
x=train_data.drop(["RefId","IsBadBuy"],axis=1)
 y=train_data["IsBadBuy"]
history = model.fit(x_train, y_train,
epochs=100,
batch_size=128,
validation_data=(x_test, y_test),
callbacks=cb )<predict_on_test>
x_train,x_val,y_train,y_val=train_test_split(x,y,random_state=42 )
preds = model.predict(test_x_final, batch_size=128 )<prepare_output>
from sklearn.neighbors import KNeighborsClassifier
results = np.argmax(preds, axis=-1)
 results.shape<save_to_csv>
from sklearn.neighbors import KNeighborsClassifier
sub = pd.read_csv(os.path.join(PATH, 'sample_submission.csv'))
sub.head()
df = pd.DataFrame({'ImageId': sub['ImageId'], 'Label': results})
 df.to_csv('submission.csv', index=False)
 os.listdir('./' )<set_options>
from sklearn.neighbors import KNeighborsClassifier
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
knn = KNeighborsClassifier(n_neighbors=11 )
from fastai import *
from fastai.vision import *
import os
import pandas as pd
import numpy as np
from pathlib import Path
from PIL import Image<set_options>
knn.fit(x_train,y_train )
warnings.simplefilter('ignore' )<load_from_csv>
knn.score(x_val,y_val )
train_df = pd.read_csv(input_dir/"train.csv")
 train_df.head(3 )<load_from_csv>
pred=knn.predict(test_data.drop("RefId",axis=1))
test_df = pd.read_csv(input_dir/"test.csv")
 test_df.head(3 )<define_variables>
submission=pd.DataFrame(data=pred,columns=["IsBadBuy"] )
train_dir = Path(".. /train")
 test_dir = Path(".. /test" )<sort_values>
submission["RefId"]=test_data["RefId"]
sorted(os.listdir(train_dir))<data_type_conversions>
submission.set_index("RefId",inplace=True )
for index, row in train_df.iterrows() :
label,digit = row[0], row[1:]
filepath = train_dir/str(label)
 filename = f"{index}.jpg"
digit = digit.values
digit = digit.reshape(28,28)
 digit = digit.astype(np.uint8)
 img = Image.fromarray(digit)
 img.save(filepath/filename )<data_type_conversions>
submission.to_csv("submission.csv" )