Code submission format

Hi
1)Following is my code it’s correct according to me. So what changes should I make in my code to submit it as a csv file because in my code we need to manually provide test cases and check with it’s corresponding label.

  1. And file named ‘sample_submission’ is label file for test cases i.e 0 and 1 label for test cases ?? because I have used this as label for test cases.

Following is my code:
import numpy as np,pandas as pd, matplotlib.pyplot as plt

#data preparation
dfx=pd.read_csv(‘Diabetes_XTrain.csv’)
dfy=pd.read_csv(‘Diabetes_YTrain.csv’) #labels
df_test=pd.read_csv(‘Diabetes_Xtest.csv’) #test cases
df_label=pd.read_csv(‘sample_submission.csv’) #test labels

X=dfx.values
Y=dfy.values.reshape((-1,)) #labels

X_test=df_test.values
X_label=df_label.values.reshape((-1,))

#knn
def dist(x1,x2):
return np.sqrt(sum((x1-x2)**2))

def knn(x,y,queryPoint,k=7):
vals=[]
m=x.shape[0]

for i in range(m):
    d=dist(queryPoint,x[i])
    vals.append((d,y[i]))
    

vals=sorted(vals)
vals=vals[:k]
vals=np.array(vals)

new_vals=np.unique(vals[:,1],return_counts=True)
index=new_vals[1].argmax()
pred=new_vals[0][index]

return int(pred)

print(knn(X,Y,X_test[0])) #manually I have given test case
print(X_label[0])

Hey @pasta, instead of this
print(knn(X,Y,X_test[0])) #manually I have given test case

you need to use a for loop and iterate over all df_test row pass each row in each iteration, make prediction, and than append in ans list. Than finally convert this array into the the csv.

For this go through the .to_csv function of pandas.

Hope this resolved your doubt.
Plz mark the doubt as resolved in my doubts section. :blush: