First, I’m not good at coding and English.(Sorry!, and this is my first question post.)
I wanted to make KNN Classifier in Python, google colab, not using Ai modules.
So I coded it for three hours fighting MANY Errors.
This is my code.
dlist={'index_id' :[],'distance':[]}
def find_near_target(df,k):
#print('sort values and head cut_4.1')
id_lst = df.sort_values('distance').head(k+1)[1:]['index_id']
#print('#print id list',id_lst)
#print('done_4.1')
#print('make nlist_4.2')
nlst=[]
for i_d in id_lst.values:
nlst.append(y_train.iloc[i_d])
#print('#print n list',nlst)
#print('done_4.2')
#print('reshape_4.3')
nplst = np.array(nlst).reshape(-1,1)
#print('done_4.3')
#print('return')
return np.argmax(np.bincount(nplst[0]))
def knn_algorithm(feature,target):
dlist={'index_id' :[],'distance':[]}
#print('knn algoritm..._0')
#print('destance calculate_1')
for i in range(X_train.shape[0]//13):
dlist['index_id'].append(i*13)
dlist['distance'].append(math.sqrt(sum((X_train.iloc[i*13]-feature)**2)))
#print('done_1')
#print('reshape_2')
#print('done_2')
#print('reshape to dataframe_3')
dflist = pd.DataFrame(dlist)
#print('done_3')
#print('find near target_4')
pre_target = find_near_target(dflist,7)
#print('done_4')
#print('compare target_5')
if (pre_target == target):
#print('done_5')
#print('true')
return 0
else :
#print('done_5')
#print('false')
return 1
count=0
for x in range(X_test.shape[0]//27):
count += knn_algorithm(X_test.iloc[x*27],y_test.iloc[x*27])
print(1-count/(X_test.shape[0]//27))
I used Mushroom Dataset (Binary Classification), in Kaggle.(https://www.kaggle.com/datasets/prishasawhney/mushroom-dataset)
I wonder why my code takes too much time to perform and why much less time in imported AI model.
I really want to know difference between mine and others.
Thankyou for reading, special thanks to respondent.
My handmade KNN code is too slow and I want to know why.
greenlemp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.