Kaggle competition: Kannada MNIST
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))<drop_column>
test_data=test_data[train_data.drop("IsBadBuy",axis=1 ).columns]<prepare_x_and_y>
from fastai.vision import *
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']<split>
path = Path('.. /input/Kannada-MNIST')
 train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 train_other = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv' )
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )<import_modules>
data,labels =(train.iloc[:,1:],train.iloc[:,0] )
from sklearn.neighbors import KNeighborsClassifier<import_modules>
data_other,labels_other =(train_other.iloc[:,1:],train_other.iloc[:,0] )
from sklearn.neighbors import KNeighborsClassifier<import_modules>
data_train,labels_train =(pd.concat([data, data_other]),pd.concat([labels, labels_other]))
from sklearn.neighbors import KNeighborsClassifier<train_model>
data_train, data_valid, labels_train, labels_valid = train_test_split(data_train, labels_train, test_size=0.25, random_state=42,stratify=labels_train )
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )<compute_test_metric>
def save_img_to_folder(path:Path,data,labels):
path.mkdir(parents=True,exist_ok=True)
 for i in range(len(data)) :
test = path
temp_path = test/(str(labels[i]))
if os.path.isdir(temp_path):
imageio.imwrite(str(temp_path/(str(i)+'.jpg')) , data[i])
 else:
temp_path.mkdir(parents=True,exist_ok=True)
 imageio.imwrite(str(temp_path/(str(i)+'.jpg')) , data[i] )
KNN.score(X_test,y_test )<predict_on_test>
data_arr = np.array(data_valid ).reshape(-1,28,28)
 labels_arr = np.array(labels_valid )
predict=KNN.predict(test_data.drop('RefId',axis=1))<prepare_output>
save_img_to_folder(Path('valid'),data_arr,labels_arr )
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()<prepare_output>
data_arr1 = np.array(data_train ).reshape(-1,28,28)
 labels_arr1 = np.array(labels_train )
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True )<save_to_csv>
save_img_to_folder(Path('train'),data_arr1,labels_arr1 )
Submission.head()
Submission.to_csv('Submission.csv' )<load_from_csv>
path = Path('/kaggle/working')
 train_path = path/'train'
valid_path = path/'valid'
path.ls()
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()<load_from_csv>
from fastai.metrics import error_rate
from fastai.vision import *
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()<count_missing_values>
np.random.seed(42)
 tfms = get_transforms(do_flip=False )
train_data.isnull().sum()<count_values>
src =(ImageList.from_folder(path)
.split_by_folder(train='train', valid='valid')
.label_from_folder() )
train_data['Model'].value_counts()
train_data.drop('Model',axis=1,inplace=True)
 test_data.drop("Model",axis=1,inplace=True )<drop_column>
data =(src.transform(tfms,size=64)
.databunch(bs=64)
.normalize(mnist_stats))
train_data.drop('Trim',inplace=True,axis=1)
 test_data.drop('Trim',inplace=True,axis=1)
<drop_column>
data.train_ds
train_data.drop('SubModel',inplace=True,axis=1)
 test_data.drop('SubModel',inplace=True,axis=1)
<data_type_conversions>
data.show_batch(rows=4,figsize=(7,8))
train_data['Color'].fillna(value='Color_Unknown',inplace=True)
 test_data['Color'].fillna(value='Color_Unknown',inplace=True )<rename_columns>
learner = cnn_learner(data,models.resnet152, metrics=[error_rate, accuracy] )
train_data['Transmission'].replace("Manual","MANUAL",inplace=True )<data_type_conversions>
learner.fit_one_cycle(12 )
train_data['Transmission'].fillna(value="Transmission_unk",inplace=True)
 test_data['Transmission'].fillna(value="Transmission_unk",inplace=True )<drop_column>
learner.save('kannada-stage1' )
train_data.drop('WheelTypeID',inplace=True,axis=1)
 test_data.drop('WheelTypeID',inplace=True,axis=1)
<data_type_conversions>
interp = ClassificationInterpretation.from_learner(learner )
train_data['WheelType'].fillna(value='WheelType_unk',inplace=True)
 test_data['WheelType'].fillna(value='WheelType_unk',inplace=True )<data_type_conversions>
losses,idx = interp.top_losses()
train_data['Nationality'].fillna(value='Nationality_unk',inplace=True)
 test_data['Nationality'].fillna(value='Nationality_unk',inplace=True )<data_type_conversions>
len(data.valid_ds)==len(losses)==len(idx )
train_data['Size'].fillna(value='Size_unk',inplace=True)
 test_data['Size'].fillna(value="Size_unk",inplace=True )<data_type_conversions>
learner.lr_find()
train_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True )<data_type_conversions>
learner.unfreeze()
learner.fit_one_cycle(10, max_lr=slice(1e-6,1e-4))
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 )<drop_column>
learner.save('kannada-stage2' )
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'<feature_engineering>
learner.load('kannada-stage2' )
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()<drop_column>
img = learner.data.valid_ds[0][0]
categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
categorical<categorify>
learner.predict(img )
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()<feature_engineering>
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))<split>
test_csv = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 test_csv.drop('id',axis = 'columns',inplace = True)
 sub_df = pd.DataFrame(columns=['id','label'])
 test_data = np.array(test_csv )
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 )<train_model>
def get_img(data):
t1 = data.reshape(28,28)/255
t1 = np.stack([t1]*3,axis=0)
 img = Image(FloatTensor(t1))
return img
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )<compute_test_metric>
from fastprogress import progress_bar
KNN.score(X_test,y_test )<predict_on_test>
def decr(ido):
return ido-1
sub_df['id'] = sub_df['id'].map(decr )
predict=KNN.predict(test_data.drop('RefId',axis=1))
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head() 
<save_to_csv>
sub_df.to_csv("submission.csv", index=False )