Python/machine_learning/knn_sklearn.py
CarsonHam 61f3119467
Change occurrences of str.format to f-strings (#4118)
* f-string update rsa_cipher.py

* f-string update rsa_key_generator.py

* f-string update burrows_wheeler.py

* f-string update non_recursive_segment_tree.py

* f-string update red_black_tree.py

* f-string update deque_doubly.py

* f-string update climbing_stairs.py

* f-string update iterating_through_submasks.py

* f-string update knn_sklearn.py

* f-string update 3n_plus_1.py

* f-string update quadratic_equations_complex_numbers.py

* f-string update nth_fibonacci_using_matrix_exponentiation.py

* f-string update sherman_morrison.py

* f-string update levenshtein_distance.py

* fix lines that were too long
2021-02-23 11:23:49 +05:30

32 lines
699 B
Python

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
# Load iris file
iris = load_iris()
iris.keys()
print(f"Target names: \n {iris.target_names} ")
print(f"\n Features: \n {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(
f"\nNew array: \n {X_new}\n\nTarget Names Prediction: \n"
f" {iris['target_names'][prediction]}"
)