from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import StratifiedKFold,KFold
sad26.png
workclass = df['workclass'].unique()
workclass
123.png
np.argwhere(workclass=='State-gov')[0][0]
>>>0
def conver(x):
return np.argwhere(workclass==x)[0,0]
X['workclass'] = X['workclass'].map(conver)
234.png
X.columns
qwe17.png
clos = ['marital_status', 'occupation',
'relationship', 'race', 'sex', 'native_country']
for clo in clos:
u = X[clo].unique()
def conver(x):
return np.argwhere(u==x)[0,0]
X[clo] = X[clo].map(conver)
12313.png
sKfold = StratifiedKFold(n_splits=20)
knn = KNeighborsClassifier(n_neighbors=5)
scores = []
for train,test in sKfold.split(X,y):
# print(train.shape)
knn.fit(X.iloc[train],y[train])
s = knn.score(X.iloc[test],y[test])
scores.append(s)
np.mean(scores)
>>>0.800651505347638