1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| import numpy as np from sklearn.preprocessing import LabelBinarizer from sklearn.preprocessing import LabelBinarizer from sklearn.neighbors import KNeighborsClassifier from sklearn.preprocessing import StandardScaler
X_train = np.array([ [158, 64], [170, 86], [183, 84], [191, 80], [155, 49], [163, 59], [180, 67], [158, 54], [170, 67] ])
y_train = ['male', 'male', 'male', 'male', 'female', 'female', 'female','female', 'female']
X_test = np.array([[155, 70],[160, 50]])
ss = StandardScaler() X_train_scaled = ss.fit_transform(X_train) X_test_scaled = ss.transform(X_test)
lb = LabelBinarizer() y_train_binarized = lb.fit_transform(y_train) print(y_train_binarized)
""" the output is : [[1] [1] [0] [0]] """
k = 3 clf = KNeighborsClassifier(n_neighbors=k) clf.fit(X_train,y_train_binarized.reshape(1,-1)[0])
prediction_binarized = clf.predict(X_test)
prediction_binarized = prediction_binarized.reshape(1,-1)[0] y_test = lb.inverse_transform(prediction_binarized) print(y_test)
|