Python ML - 분류베이즈_KNN.

🛟 Dive.·2024년 2월 24일
0

Python

목록 보기
3/22

분류베이즈_KNN

분류베이즈_KNN 

from sklearn.datasets import load_iris
iris = load_iris()
print(iris.data)

# 4개의 특징 이름을 출력.
print(iris.feature_names)

# 정수는 꽃의 종류를 나타낸다. 0 = setosa, 1 = versicolor, 2 =virginica
print(iris.target)

from sklearn.model_selection import train_test_split

X = iris.data
Y = iris.target

X_train,X_test,y_train,y_test = train_test_split(X, Y, test_size=0.2, random_state=4)

print(X_train.shape)
print(X_test.shape)

from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics

knn = KNeighborsClassifier(n_neighbors=6)
knn.fit(X_train, y_train)

y_pred = knn.predict(X_test)
scores = metrics.accuracy_score(y_test, y_pred)

print(scores)

# 0 = setosa, 1 = versicolor, 2 = virginica
classes = {0:'setosa', 1:'versicolor',2:'virginica'}

# 아직 보지 못한 새로운 데이터 제시.
x_new = [[3,4,5,2],
        [5,4,2,2]]

y_predict = knn.predict(x_new)

print(classes[y_predict[0]])
print(classes[y_predict[1]])

profile
Data Science. DevOps.

0개의 댓글