mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
61f3119467
* 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
32 lines
699 B
Python
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]}"
|
|
)
|