2019-07-05 08:47:18 +00:00
|
|
|
from sklearn.datasets import load_iris
|
2020-07-06 07:44:19 +00:00
|
|
|
from sklearn.model_selection import train_test_split
|
2019-07-05 08:47:18 +00:00
|
|
|
from sklearn.neighbors import KNeighborsClassifier
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# Load iris file
|
2019-07-05 08:47:18 +00:00
|
|
|
iris = load_iris()
|
|
|
|
iris.keys()
|
|
|
|
|
|
|
|
|
2019-12-07 05:39:59 +00:00
|
|
|
print(f"Target names: \n {iris.target_names} ")
|
|
|
|
print(f"\n Features: \n {iris.feature_names}")
|
2019-07-05 08:47:18 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# Train set e Test set
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(
|
|
|
|
iris["data"], iris["target"], random_state=4
|
|
|
|
)
|
2019-07-05 08:47:18 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# KNN
|
2019-07-05 08:47:18 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
knn = KNeighborsClassifier(n_neighbors=1)
|
2019-07-05 08:47:18 +00:00
|
|
|
knn.fit(X_train, y_train)
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# new array to test
|
|
|
|
X_new = [[1, 2, 1, 4], [2, 3, 4, 5]]
|
2019-07-05 08:47:18 +00:00
|
|
|
|
|
|
|
prediction = knn.predict(X_new)
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
print(
|
|
|
|
"\nNew array: \n {}"
|
|
|
|
"\n\nTarget Names Prediction: \n {}".format(X_new, iris["target_names"][prediction])
|
|
|
|
)
|