Kaggle competition: Don't Get Kicked!
test_batch_size = 64
test_dataset = get_dataset(test_df, transform=val_test_transforms)
 test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=test_batch_size, shuffle=False)
 test_pred = prediciton(test_loader, model)
 test_pred_df = pd.DataFrame(np.c_[np.arange(1, len(test_dataset)+1), test_pred.numpy() ],
columns=['ImageId', 'Label'])
 print(test_pred_df.head() )<save_to_csv>
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()
def create_download_link(df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv(index=False)
 b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html)
 create_download_link(test_pred_df, filename="submission.csv" )<save_to_csv>
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()
test_pred_df.to_csv('submission.csv', index=False )<load_from_csv>
train_data.isnull().sum()
warnings.filterwarnings("ignore")
 train1=pd.read_csv(".. /input/digit-recognizer/train.csv")
 train=pd.read_csv(".. /input/new-digit-dataset/Digit_Train_Final.csv")
 test=pd.read_csv(".. /input/digit-recognizer/test.csv")
 train.head()<prepare_x_and_y>
train_data['Model'].value_counts()
train_data.drop('Model',axis=1,inplace=True)
 test_data.drop("Model",axis=1,inplace=True )
x=train.drop(labels=["label"],axis=1)
 y=train["label"]<drop_column>
train_data.drop('Trim',inplace=True,axis=1)
 test_data.drop('Trim',inplace=True,axis=1)
del train['Unnamed: 0']<prepare_x_and_y>
train_data.drop('SubModel',inplace=True,axis=1)
 test_data.drop('SubModel',inplace=True,axis=1)
x=train.drop(['label'],axis=1)
 y=train['label']<normalization>
train_data['Color'].fillna(value='Color_Unknown',inplace=True)
 test_data['Color'].fillna(value='Color_Unknown',inplace=True )
norm=MinMaxScaler()
x=norm.fit_transform(x)
 test=norm.fit_transform(test)
<split>
train_data['Transmission'].replace("Manual","MANUAL",inplace=True )
y=to_categorical(y,num_classes=10)
 x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.009,random_state=33 )<choose_model_class>
train_data['Transmission'].fillna(value="Transmission_unk",inplace=True)
 test_data['Transmission'].fillna(value="Transmission_unk",inplace=True )
model = Sequential()
model.add(Conv2D(filters = 128, kernel_size = 3,padding = 'Same',input_shape =(28,28,1)))
 model.add(Conv2D(filters = 128, kernel_size = 3,padding = 'Same',input_shape =(28,28,1)))
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(LeakyReLU(alpha=0.1))
model.add(Dropout(0.1))
model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same'))
model.add(Conv2D(filters = 64, kernel_size = 3,padding = 'Same'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(LeakyReLU(alpha=0.1))
model.add(Dropout(0.1))<choose_model_class>
train_data.drop('WheelTypeID',inplace=True,axis=1)
 test_data.drop('WheelTypeID',inplace=True,axis=1)
model.add(Flatten())
 model.add(Dense(256))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
 model.add(Dense(10, activation = "softmax"))
model.summary()<choose_model_class>
train_data['WheelType'].fillna(value='WheelType_unk',inplace=True)
 test_data['WheelType'].fillna(value='WheelType_unk',inplace=True )
optimizer=Adamax(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
<choose_model_class>
train_data['Nationality'].fillna(value='Nationality_unk',inplace=True)
 test_data['Nationality'].fillna(value='Nationality_unk',inplace=True )
model.compile(optimizer = optimizer , loss = "binary_crossentropy", metrics=["accuracy"] )<choose_model_class>
train_data['Size'].fillna(value='Size_unk',inplace=True)
 test_data['Size'].fillna(value="Size_unk",inplace=True )
lr_reduction = ReduceLROnPlateau(monitor='val_acc', patience=2, verbose=1, factor=0.1, min_lr=0.00001)
 b_l=BaseLogger(stateful_metrics=None)
 e_stop = EarlyStopping(monitor='val_acc', patience=4, verbose=1 )<define_variables>
train_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True )
callbacks=[lr_reduction,b_l,e_stop]
batch_size=89
epochs=20<train_model>
train_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True)
 test_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True)
 train_data['AUCGUART'].replace("AGREEN","GREEN",inplace=True)
 test_data['AUCGUART'].replace("ARED","RED",inplace=True)
 train_data['AUCGUART'].fillna(value="AUC_unk",inplace=True)
 test_data['AUCGUART'].fillna(value="AUC_unk",inplace=True)
 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)
 train_data.drop('PurchDate',axis=1,inplace=True)
 test_data.drop("PurchDate",axis=1,inplace=True )
datagen = ImageDataGenerator()
datagen.get_random_transform(img_shape=(28,28,1),seed=3)
 datagen.flow(x=x, y=y, batch_size=32, shuffle=True, sample_weight=None, seed=None, save_to_dir=None, save_prefix='', save_format='png', subset=None)
 datagen.fit(x_train )<train_model>
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'
information = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),epochs=epochs,validation_data =(x_test, y_test), steps_per_epoch=x_train.shape[0] // batch_size,callbacks=callbacks )<create_dataframe>
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']
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
train_data[not_categorical].head()
karsilastirma=pd.DataFrame()
karsilastirma["prediction"]=Y_head_class
karsilastirma["true"]=Y_true<count_values>
categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
categorical
karsilastirma['correct'] = karsilastirma.prediction == karsilastirma.true
karsilastirma["correct"].value_counts()<filter>
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)
 train_data.head()
karsilastirma[karsilastirma['correct']==False]<predict_on_test>
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))
predicted= model.predict(test )<create_dataframe>
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42)
 print(X_train.shape,y_train.shape)
 print(X_test.shape,y_test.shape )
predicted = np.argmax(predicted,axis = 1)
 predicted = pd.Series(predicted,name="Label" )<save_to_csv>
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )
result = pd.concat([pd.Series(range(1,len(test)+1),name = "ImageId"),predicted],axis = 1)
 result.to_csv("Digit_Recognizer_predicted.csv",index=False )<define_variables>
KNN.score(X_test,y_test )
DATA_PATH = '/kaggle/input/nlp-getting-started/'
SEED = 42
DROPOUT = 0.5
EPOCHS = 3
LEARN_RATE = 1e-4
SPLIT = 0.2
BATCH_SIZE = 32<load_from_csv>
predict=KNN.predict(test_data.drop('RefId',axis=1))
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()
train = pd.read_csv(os.path.join(DATA_PATH, 'train.csv'))
test = pd.read_csv(os.path.join(DATA_PATH, 'test.csv'))
sample_submission = pd.read_csv(os.path.join(DATA_PATH, 'sample_submission.csv'))
ds = ['train', 'test', 'sample submission']
print("Training set has {} rows and {} columns.".format(train.shape[0], train.shape[1]))
print("Test set has {} rows and {} columns.".format(test.shape[0], test.shape[1]))
print()
print(train.columns)
 print(test.columns )<count_values>
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True)
 Submission.head()
Submission.to_csv('Submission.csv' )