Kaggle competition: Don't Get Kicked!
early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss',
patience=4, verbose=2, mode='auto',
baseline=None, restore_best_weights=True )<train_model>
train_data = pd.read_csv("/kaggle/input/DontGetKicked/training.csv")
 test_data = pd.read_csv("/kaggle/input/DontGetKicked/test.csv")
 train_data.head()
history = model.fit(X, y, batch_size=50, epochs=EPOCHS, verbose = 2, callbacks = [reduce_lr,early_stop], validation_split=0.25,
shuffle=True )<set_options>
train_data.isnull().sum()
history.history<predict_on_test>
test_data.isnull().sum()
pred_X = model.predict(X, verbose = 2 )<categorify>
train_data['IsBadBuy'].value_counts()
ohe = OneHotEncoder()<categorify>
train_data['Model'].value_counts()
ohe_encoded_y = ohe.fit_transform(y.values.reshape(-1, 1))<categorify>
train_data.drop('Model',axis =1, inplace=True)
 test_data.drop('Model',axis =1, inplace=True )
ohe_encoded_y[0].toarray()<find_best_params>
train_data['Trim'].value_counts()
fpr = dict()
tpr = dict()
roc_auc = dict()
fpr["micro"], tpr["micro"], thresholds = roc_curve(ohe_encoded_y.toarray().ravel() , pred_X.ravel())
 roc_auc["micro"] = auc(fpr["micro"], tpr["micro"] )<prepare_output>
train_data.drop('Trim',axis =1, inplace=True)
 test_data.drop('Trim',axis =1, inplace=True )
np.argmax(pred_X,axis=1 )<compute_test_metric>
train_data['SubModel'].value_counts()
precision_recall_fscore_support(y, np.argmax(pred_X,axis=1), average='micro' )<compute_test_metric>
train_data.drop('SubModel',axis =1, inplace=True)
 test_data.drop('SubModel',axis =1, inplace=True )
precision_recall_fscore_support(y, np.argmax(pred_X,axis=1), average='weighted' )<compute_test_metric>
train_data['Color'].value_counts()
precision_recall_fscore_support(y, np.argmax(pred_X,axis=1), average=None )<compute_test_metric>
test_data['Color'].value_counts()
precision_recall_fscore_support(y, np.argmax(pred_X,axis=1), average=None, beta=2 )<compute_test_metric>
train_data['Color'].fillna(value='Color_Unknown',inplace = True)
 test_data['Color'].fillna(value='Color_Unknown',inplace = True )
precision_recall_fscore_support(y, np.argmax(pred_X,axis=1), average=None, beta=0.5 )<compute_test_metric>
print("Null values in color column of train data: "+str(train_data['Color'].isnull().sum()))
print("Null values in color column of test data: "+str(test_data['Color'].isnull().sum()))
print(classification_report(y, np.argmax(pred_X,axis=1)) )<predict_on_test>
train_data['Transmission'].value_counts()
pred = model.predict(X_unseen, verbose = 2 )<create_dataframe>
test_data['Transmission'].value_counts()
results=pd.DataFrame({"ImageId":sample['ImageId'],
"Label":np.argmax(pred,axis=1)} )<count_values>
train_data['Transmission'].replace("Manual","MANUAL",inplace = True )
results['Label'].value_counts()<save_to_csv>
train_data['Transmission'].value_counts()
results.to_csv("submission.csv",index=False )<prepare_x_and_y>
train_data['Transmission'].fillna(value = 'Transmission_unk',inplace = True)
 test_data['Transmission'].fillna(value = 'Transmission_unk',inplace = True )
train_images = np.load('.. /input/kuzushiji/kmnist-train-imgs.npz')['arr_0']
test_images = np.load('.. /input/kuzushiji/kmnist-test-imgs.npz')['arr_0']
train_labels = np.load('.. /input/kuzushiji/kmnist-train-labels.npz')['arr_0']
test_labels = np.load('.. /input/kuzushiji/kmnist-test-labels.npz')['arr_0']
train_images = train_images / 255.0
test_images = test_images / 255.0
x_train = np.expand_dims(train_images, axis=-1)
 x_test = np.expand_dims(test_images, axis=-1)
 y_train = train_labels
y_test = test_labels<train_model>
test_data['Transmission'].isnull().sum()
hist = model.fit(x_train, y_train,
batch_size=50,
epochs=EPOCHS,
verbose=1,
validation_data=(x_test, y_test))<compute_test_metric>
train_data['WheelTypeID'].value_counts()
score = model.evaluate(x_test, y_test, verbose=0)
<predict_on_test>
train_data.drop('WheelTypeID',axis =1, inplace=True)
 test_data.drop('WheelTypeID',axis =1, inplace=True )
kpred_x_test = model.predict(x_test, verbose = 2 )<compute_test_metric>
train_data['WheelType'].value_counts()
print(classification_report(y_test, np.argmax(kpred_x_test,axis=1)) )<set_options>
train_data['WheelType'].fillna(value = 'WheelType_unk',inplace = True)
 test_data['WheelType'].fillna(value = 'WheelType_unk',inplace = True )
warnings.filterwarnings('ignore')
<load_from_csv>
train_data['Nationality'].value_counts()
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv' )<concatenate>
train_data['Nationality'].fillna(value = 'Nationality_unk',inplace = True)
 test_data['Nationality'].fillna(value = 'Nationality_unk',inplace = True )
full = train.append(test,ignore_index=True)
 print('合并后的数据集：',full.shape )<set_options>
train_data['Size'].value_counts()
%matplotlib inline<create_dataframe>
train_data['Size'].fillna(value = 'Size_unk',inplace = True)
 test_data['Size'].fillna(value = 'Size_unk',inplace = True )
df_num = pd.DataFrame(train,columns=['Age','Fare','SibSp','Parch','Survived'])
 df_num.hist(figsize=(25,20),bins=20,xlabelsize=15,ylabelsize=15 )<data_type_conversions>
train_data['TopThreeAmericanName'].value_counts()
df_Cabin = train['Cabin'].astype(str ).map(lambda x:x[0])
 df_Cabin.head()<count_values>
train_data['TopThreeAmericanName'].fillna(value = 'TopThreeAmericanName_unk',inplace = True)
 test_data['TopThreeAmericanName'].fillna(value = 'TopThreeAmericanName_unk',inplace = True )
train['Ticket'].value_counts()<count_values>
train_data['PRIMEUNIT'].value_counts()
def getTicket(ticket):
ticket = ticket.replace('.',' ')
 ticket = ticket.replace('/',' ')
 ticket = ticket.split(' ')
 ticket = map(lambda a:a.strip() ,ticket)
 ticket = list(filter(lambda x:not x.isdigit() ,ticket))
if len(ticket)>0:
return ticket[0]
else:
return 'Unknown'
df_tickets = train['Ticket'].map(getTicket)
 df_tickets.value_counts()<feature_engineering>
train_data['PRIMEUNIT'].fillna(value = 'Prime_unk',inplace = True)
 test_data['PRIMEUNIT'].fillna(value = 'Prime_unk',inplace = True )
def get_agePeriod(age):
if not np.bool(age):
return age
x = np.linspace(0,80,9)
 for i in range(0,len(x)-1):
if(age>x[i] and age<=x[i+1]):
return '(%.0f,%.0f]' %(x[i],x[i+1])
 df_num['Age'] = df_num['Age'].map(get_agePeriod)
 df_num['Age'].head()<count_values>
train_data['AUCGUART'].value_counts()
train['Survived'].value_counts()<feature_engineering>
test_data['AUCGUART'].value_counts()
full['Age'] = full['Age'].fillna(full['Age'].median())
 full['Fare'] = full['Fare'].fillna(full['Age'].median() )<count_values>
train_data['AUCGUART'].replace('AGREEN','GREEN',inplace=True)
 test_data['AUCGUART'].replace('ARED','RED',inplace=True )
pd.value_counts(full['Embarked'] )<feature_engineering>
train_data['AUCGUART'].fillna(value = 'AUC_unk',inplace = True)
 test_data['AUCGUART'].fillna(value = 'AUC_unk',inplace = True )
full['Embarked'] = full['Embarked'].fillna('S' )<import_modules>
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 )
import xgboost as xgb<import_modules>
train_data.drop('PurchDate',inplace=True,axis=1)
 test_data.drop('PurchDate',inplace=True,axis=1 )
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler<normalization>
not_categorical = train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']
standard_scaler = StandardScaler()
full['Age']= pd.DataFrame(standard_scaler.fit_transform(full['Age'].values.reshape(-1,1)) )<normalization>
for i in not_categorical:
maximum = np.max(train_data[i])
 train_data[i] = train_data[i]/maximum
maximum_test = np.max(test_data[i])
 test_data[i] = test_data[i]/maximum_test
min_max_scaler = MinMaxScaler()
full['Fare']= pd.DataFrame(min_max_scaler.fit_transform(full['Fare'].values.reshape(-1,1)))
 full.head()<feature_engineering>
categorical = train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
familyDf['Family_Single'] = familyDf['FamilySize'].map(lambda s:1 if s==1 else 0)
 familyDf['Family_Small'] = familyDf['FamilySize'].map(lambda s:1 if s>=2 and s<=4 else 0)
 familyDf['Family_Large'] = familyDf['FamilySize'].map(lambda s:1 if s>=5 else 0)
 familyDf.head()<concatenate>
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,inplace=True,axis=1)
 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,inplace=True,axis=1 )
full = pd.concat([full,familyDf],axis=1)
 full.drop(['SibSp','Parch'],axis=1,inplace=True)
 full.head()<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))
embarkedDf = pd.DataFrame()
embarkedDf = pd.get_dummies(full['Embarked'],prefix='Embarked')
 embarkedDf.head()<categorify>
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))
full = pd.concat([full,embarkedDf],axis=1)
 full.drop(['Embarked'],axis=1,inplace=True)
 full.head()<categorify>
test_data = test_data[train_data.drop('IsBadBuy',axis=1 ).columns]
pclassDf = pd.DataFrame()
pclassDf = pd.get_dummies(full['Pclass'],prefix='Pclass')
 pclassDf.head()<categorify>
X = train_data.drop(['RefId','IsBadBuy'],axis=1)
 y = train_data['IsBadBuy']
full = pd.concat([full,pclassDf],axis=1)
 full.drop(['Pclass'],axis=1,inplace=True)
 full.head()<prepare_output>
X_train ,X_test,y_train,y_test = train_test_split(X,y,random_state = 32 )
titleDf = pd.DataFrame()
titleDf['Title'] = full['Name'].map(getTitle)
 titleDf.head()<count_values>
KNN_model = KNeighborsClassifier(n_neighbors = 11)
 KNN_model.fit(X_train,y_train )
titleDf['Title'].value_counts()<define_variables>
KNN_model.score(X_test,y_test )
title_mapDict = {'Capt':'Officer',
'Col':'Officer',
'Major':'Officer',
'Jonkheer':'Royalty',
'Don':'Royalty',
'Sir':'Royalty',
'the Countess':'Royalty',
'Dona':'Royalty',
'Dr':'Officer',
'Rev':'Officer',
'Lady':'Royalty',
'Mr':'Mr',
'Miss':'Miss',
'Mrs':'Mrs',
'Master':'Master',
'Mlle':'Miss',
'Mme':'Mrs',
'Ms':'Mrs'
}<categorify>
predict=KNN_model.predict(test_data.drop('RefId',axis=1))
titleDf['Title'] = titleDf['Title'].map(title_mapDict)
 titleDf = pd.get_dummies(titleDf['Title'])
 titleDf.head()<categorify>
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()
full = pd.concat([full,titleDf],axis=1)
 full.drop(['Name'],axis=1,inplace=True)
 full.head()<categorify>
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True )
full = pd.concat([full,cabinDf],axis=1)
 full.drop(['Cabin'],axis=1,inplace=True)
 full.head()<categorify>
Submission.head()
Submission.to_csv('Submission.csv' )