forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creates an example of KNN algorithm using sklearn library.
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from sklearn.model_selection import train_test_split | ||
from sklearn.datasets import load_iris | ||
from sklearn.neighbors import KNeighborsClassifier | ||
|
||
#Load iris file | ||
iris = load_iris() | ||
iris.keys() | ||
|
||
|
||
print('Target names: \n {} '.format(iris.target_names)) | ||
print('\n Features: \n {}'.format(iris.feature_names)) | ||
|
||
#Train set e Test set | ||
X_train, X_test, y_train, y_test = train_test_split(iris['data'],iris['target'], random_state=4) | ||
|
||
#KNN | ||
|
||
knn = KNeighborsClassifier (n_neighbors = 1) | ||
knn.fit(X_train, y_train) | ||
|
||
#new array to test | ||
X_new = [[1,2,1,4], | ||
[2,3,4,5]] | ||
|
||
prediction = knn.predict(X_new) | ||
|
||
print('\nNew array: \n {}' | ||
'\n\nTarget Names Prediction: \n {}'.format(X_new, iris['target_names'][prediction])) |