728x90
반응형
(문제)
dach_length = [75, 77, 83, 81, 73, 99, 72, 83]
dach_height = [24, 29, 19, 32, 21, 22, 19, 34]
samo_length = [76, 78, 82, 88, 76, 83, 81, 89]
samo_height = [55, 58, 53, 54, 61, 52, 57, 64]
malt_length = [35, 39, 38, 41, 30, 57, 41, 35]
malt_height = [23, 26, 19, 30, 21, 24, 28, 20]
1. 이 정보로 닥스훈트를 0, 사모예드를 1, 말티즈를2로 레이블링해서 데이터와 레이블을 생성하여라.
2.각 견종별 데이터를 2차원으로 배열로 만들어서 출력하여라
ex) 닥스훈트(0): [[75, 24], [77,29]...[83, 34]]
3.k값이 3일때, k-nn 분류기의 분류결과 목표값과 예측결과를 다음과같은 혼동향렬로 표시
array([[8, 0, 0], [0, 8, 0[, [1, 0, 7]])
라이브러리 사용
import numpy as np
from sklearn import metrics
import matplot lib.pyplot as plt
(방법)
import numpy as np
from sklearn import metrics
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
dach_length = [75, 77, 83, 81, 73, 99, 72, 83]
dach_height = [24, 29, 19, 32, 21, 22, 19, 34]
dach_label = [0,0,0,0,0,0,0,0]
samo_length = [76, 78, 82, 88, 76, 83, 81, 89]
samo_height = [55, 58, 53, 54, 61, 52, 57, 64]
samo_label = [1,1,1,1,1,1,1,1]
malt_length = [35, 39, 38, 41, 30, 57, 41, 35]
malt_height = [23, 26, 19, 30, 21, 24, 28, 20]
malt_label = [2,2,2,2,2,2,2,2]
d_L = np.array(dach_length).reshape(len(dach_length),1) # [[75],[77]..] 과 같이 만듬
d_H = np.array(dach_height).reshape(len(dach_height),1)
d_B = np.array(dach_label).reshape(len(dach_label),1)
dach = np.concatenate((d_L, d_H, d_B),axis=1) # [[75, 24, 0]..] 형태로 만듬
print('닥스훈트:\n',dach)
s_L = np.array(samo_length).reshape(len(samo_length),1)
s_H = np.array(samo_height).reshape(len(samo_height),1)
s_B = np.array(samo_label).reshape(len(samo_label),1)
samo = np.concatenate((s_L, s_H, s_B), axis=1)
print('사모예드:\n',samo)
m_L = np.array(malt_length).reshape(len(malt_length),1)
m_H = np.array(malt_height).reshape(len(malt_height),1)
m_B = np.array(malt_label).reshape(len(malt_label),1)
malt = np.concatenate((m_L, m_H, m_B), axis=1)
print('말티즈:\n',malt)
x = np.concatenate((dach, samo, malt)) # 합침
np.random.shuffle(x) # 순서를 임의로 섞음
y = x[:,-1] # label 값만 분리
x = x[:,:2] # length, height 로만 분리
classifier = KNeighborsClassifier(n_neighbors = 3) # k_NN 선언. k 값을 3으로 선언
classifier.fit(x, y) # 학습
pred = classifier.predict(x) # 예측
cf = metrics.confusion_matrix(y, pred) # 혼돈행렬(Confusion Matrix)
print('결과:\n',cf)
728x90
반응형
'프로그램' 카테고리의 다른 글
[파이썬] 문제 : 숫자 맞추기 게임(입력값 5초과 출력, 근접 메세지 출력) (0) | 2022.12.09 |
---|---|
[파이썬] 문제 : 두 숫자를 입력 받아 3번 반복해 누적합 계산 (0) | 2022.12.06 |
[파이썬] 문제 : bubble 정렬 코드 설명 (0) | 2022.12.06 |
[파이썬] 문제 : 최빈값 찾기(numpy) (0) | 2022.12.06 |
[파이썬] 문제 : 50미만 임의의 정수 100개 배열에서 3의 나머지가 1인 수 출력 (0) | 2022.12.06 |
댓글