본문 바로가기
AI

Day64_Ai(2)_Am

by roaring90s 2025. 10. 28.

from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(
 perch_length, perch_weight, random_state=42
)
#테스트에 사이즈가 0.25로 디폴트 설정되어있음

print(x_train.shape)

x_train = x_train.reshape(-1,1)  #-1은 자동으로 만들어라, 열은 하나로 설정할테니
x_test = x_test.reshape(-1,1)
print(x_train.shape)

knr = KNeighborsRegressor(n_neighbors=3)
knr.fit(x_train,y_train)
print(knr.predict([[100]]))

knn의 단점으로 linear를 사용한다.

from sklearn.linear_model import LinearRegression

lr = LinearRegression()
lr.fit(x_train, y_train)
print(lr.predict([[100]]))
#[3192.69585141]
print(lr.coef_, lr.intercept_)
# weight(w) 값[39.01714496] 바이어스(b)값-709.0186449535474

distances, indexes = knr.kneighbors([[100]])
print(distances, indexes)
plt.scatter(x_train,y_train)
plt.scatter(x_train[indexes], y_train[indexes], marker='D')
plt.scatter(100, 1033, marker='^')
plt.scatter(100, 3092, marker='o')
plt.show()

train_poly = np.column_stack((x_train **2, x_train))
#쌍으로 값을 가져오고 싶을때 column() 을 쓴다.
test_poly = np.column_stack((x_test**2, x_test))
print(train_poly)

lr2 = LinearRegression()
lr2.fit(train_poly, y_train)

print(lr2.predict([[100**2, 100]]))
print(lr2.coef_, lr2.intercept_)
#[  1.01433211 -21.55792498] 116.0502107827827
#[  1.01433211 -21.55792498] : x값 / 116.0502107827827 : b값

point = np.arange(15, 100)
plt.scatter(x_train, y_train)
plt.plot(point, 1.01433211*point**2 -21.55792498*point +116.0502107827827)
plt.scatter(100, 8103)
plt.show()

 


길이 = 1 짜리 언더피팅

길이 = 9 짜리는 오버피팅 

 

뎁스를 깊게 쓴다? 무조건 좋은게 아니다

 

구분과 값을 구할때 쓸 수 있다 = 결정트리 (decision tree)

 

decisionTreeEx1

from sklearn.datasets import load_iris

iris = load_iris()
print(iris.keys())

print(iris['DESCR'])
'''
:Attribute Information:
    - sepal length in cm
    - sepal width in cm
    - petal length in cm
    - petal width in cm
    - class:
            - Iris-Setosa
            - Iris-Versicolour
            - Iris-Virginica
'''

print(iris.target)
'''
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2]
'''
print(iris.target_names)
#['setosa' 'versicolor' 'virginica']
print(iris.feature_names)
#['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']

 

x = iris.data[:, 2:]
y = iris.target

from sklearn.tree import DecisionTreeClassifier
tree_clf = DecisionTreeClassifier(max_depth=2, random_state=42) # 뎁스설정 = max_depth
tree_clf.fit(x,y)

plt.figure(figsize = (8,4))
plot_decision_boundary(tree_clf,x , y)
plt.show()

x = iris.data[:, 2:]
y = iris.target

from sklearn.tree import DecisionTreeClassifier
tree_clf = DecisionTreeClassifier(max_depth=3, random_state=42) # 뎁스설정 = max_depth
tree_clf.fit(x,y)

plt.figure(figsize = (8,4))
plot_decision_boundary(tree_clf,x , y)
plt.show()

max_depth = 3으로 변경시

 


dtree_clf1 = DecisionTreeClassifier(random_state=42)
dtree_clf2 = DecisionTreeClassifier(min_samples_leaf=4, random_state=42)
#leaf 안에 들어가있는 최소 sample 갯수, 너무 작으면 오버피팅이 될 수있다.

dtree_clf1.fit(xm, ym)
dtree_clf2.fit(xm, ym)

fig, axes = plt.subplots(ncols=2,figsize=(12,6))   #ncols = 2개로 나눔
plt.sca(axes[0])
plot_decision_boundary(dtree_clf1, xm, ym, axes=[-1.5, 2.4,-1, 1.5], iris=False) #바운더리 사용할때 axes=[]
plt.title('no restrictions', fontsize=16)

plt.sca(axes[1])
plot_decision_boundary(dtree_clf2, xm, ym, axes=[-1.5, 2.4,-1, 1.5], iris=False) #바운더리 사용할때 axes=[]
plt.title(f'min_sample_leaf={dtree_clf2.min_samples_leaf}', fontsize=16)
plt.show()

 

decisionTreeEx2

import numpy as np
import matplotlib.pyplot as plt
from numpy.ma.core import reshape
from sklearn.tree import DecisionTreeRegressor

np.random.seed(42)
m=200
x = np.random.rand(m, 1)
y = 4* (x -0.5) **2
y = y + np.random.randn(m,1) / 10
print(x)
print(y)

tree_reg1 = DecisionTreeRegressor(random_state=42)
tree_reg2 = DecisionTreeRegressor(random_state=42, min_samples_leaf=10)

tree_reg1.fit(x,y)
tree_reg2.fit(x,y)

def plot_regression_predictions(tree_reg, x, y, axes=[0, 1 ,-0.2, 1], ylabel='y'):
    x1 = np.linspace(axes[0], axes[1], 500).reshape(-1,1)
    y_pred = tree_reg.predict(x1)
    plt.axis(axes)
    plt.xlabel('x', fontsize=14)
    if ylabel:
        plt.ylabel(ylabel, fontsize=14, rotation=0)
    plt.plot(x, y, 'b.')
    plt.plot(x1, y_pred, 'r-', linewidth=2)

fig, axes = plt.subplots(ncols=2, figsize=(12, 6))
plt.sca(axes[0])
plot_regression_predictions(tree_reg1, x, y)
plt.title('no restrictions', fontsize=15)

plt.sca(axes[1])
plot_regression_predictions(tree_reg2, x, y)
plt.title(f'min_sample_leaf={tree_reg2.min_samples_leaf}', fontsize=15)
plt.show()

 


모델과 모델을 연결한다.

배깅과 부스팅 이외에 많은 모델들이 많다.

결정트리보다 랜덤포레스트가 더 기준이다

원본데이터셋을 랜덤하게 가져온다 그치만 중복해서 가져오기도 하니 그것을 학습시키는것 / 데이터 샘플링

 

가장 쉬운 앙상블은 a, b, c, d, e의 모델들에게 data를 학습시킨다. 학습을 시킨다음 예측치를 뽑아 낸다.

이 예측한 값을 가지고 voting 을 한다.

다중의법칙이 적용된다.

 

#보팅시스템 만들기

emsembleEx1

from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

x,y = make_moons(n_samples=500, noise=0.3, random_state=42)
x_train, x_test, y_train,y_test = train_test_split(x,y, random_state=42)

#수업 당시 배우지 않은것
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier

log_clf = LogisticRegression(random_state=42)
rnd_clf = RandomForestClassifier(n_estimators=100, random_state=42)
svm_clf = SVC(random_state=42)

from sklearn.ensemble import VotingClassifier
voting_clf = VotingClassifier(
    estimators=[('lr', log_clf), ('rf', rnd_clf),('svc', svm_clf)],
    voting='hard'
)

for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    clf.fit(x_train, y_train)
    y_pred = clf.predict(x_test)
    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))

 

 

log_clf = LogisticRegression(random_state=42)
rnd_clf = RandomForestClassifier(n_estimators=100, random_state=42)
svm_clf = SVC(random_state=42, probability=True)    # soft 쓰기위해 probability=True 를 써야한다.


voting_clf = VotingClassifier(
    estimators=[('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
    voting='soft'       # soft 방식은 확률의 평균값을 가져 온다. 오류는 svc 확률값을 그냥은 못가져옴
)

for clf in (log_clf, rnd_clf, svm_clf, voting_clf):
    clf.fit(x_train, y_train)
    y_pred = clf.predict(x_test)
    print(clf.__class__.__name__, accuracy_score(y_test, y_pred))

 


import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.ensemble import VotingClassifier
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split


mnist = load_digits()
print(mnist.DESCR)
'''
:Number of Instances: 1797
:Number of Attributes: 64
:Attribute Information: 8x8 image of integer pixels in the range 0..16.
'''

print(mnist.data.shape)
#(1797, 64) => 8*8 을 직렬로 만든것
print(mnist.target[0:20])
#[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]

# DecisionTreeClassifier
# KNeighborsClassifier
# SVC



x_train, x_test, y_train, y_test = train_test_split(
    mnist.data, mnist.target, test_size=0.2, random_state=42)

dtree = DecisionTreeClassifier(max_depth=8, random_state=42)
dtree = dtree.fit(x_train, y_train)
dtree_predicted = dtree.predict(x_test)

knn = KNeighborsClassifier(n_neighbors=299)
knn = knn.fit(x_train, y_train)
knn_predicted = knn.predict(x_test)

svm = SVC(C=0.1, gamma=0.003, probability=True, random_state=35)
svm = svm.fit(x_train, y_train)
svm_predicted = svm.predict(x_test)
print('accuracy')
print('dtree : ', accuracy_score(y_test, dtree_predicted))
print('knn : ', accuracy_score(y_test, knn_predicted))
print('svm : ', accuracy_score(y_test, svm_predicted))

hard_voting_clf = VotingClassifier(
    estimators=[('dt',dtree), ('knn', knn), ('svm', svm)],
    voting='hard'
)
hard_voting_predicted = hard_voting_clf.fit(x_train, y_train).predict(x_test)
print('voting(hard) : ', accuracy_score(y_test, hard_voting_predicted))

soft_voting_clf = VotingClassifier(
    estimators=[('dt',dtree), ('knn', knn), ('svm', svm)],
    voting='soft'
)
soft_voting_predicted = soft_voting_clf.fit(x_train, y_train).predict(x_test)
print('voting(soft) : ', accuracy_score(y_test, soft_voting_predicted))

'''
(1797, 64)
[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
accuracy
dtree :  0.8361111111111111
knn :  0.8472222222222222
svm :  0.8972222222222223
voting(hard) :  0.9138888888888889
voting(soft) :  0.9055555555555556
'''

 


 

'AI' 카테고리의 다른 글

Day65_Ai(3)_Pm  (0) 2025.10.29
Day65_Ai(3)_Am  (0) 2025.10.29
Day64_Ai(2)_Pm  (0) 2025.10.28
Day63_Ai(1)_Pm  (0) 2025.10.27
Day63_Ai(1)_Am / 머신러닝 가기전 최소한의 수학  (0) 2025.10.27