본문 바로가기
AI

Day64_Ai(2)_Pm

by roaring90s 2025. 10. 28.

배깅은 같은 데이터에 같은 모델들인데 일부분만 발췌를 하지만 중복도 가지고 옴

paseting  = 같은모델, 페이스팅은 중복되지 않은 데이터를 학습함

 

ensembleEx3

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

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.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

bag_clf = BaggingClassifier(
    DecisionTreeClassifier(), n_estimators=500, max_samples=100,
    bootstrap=True, random_state=42
) #bootstrap= True 는 중복된 데이터를 가지고 오는것 false는 페이스팅 모델로

bag_clf.fit(x_train, y_train)
y_pred = bag_clf.predict(x_test)
print('bagging accuracy: ', accuracy_score(y_test, y_pred))

bag_clf2 = BaggingClassifier(
    DecisionTreeClassifier(), n_estimators=500, max_samples=100,
    bootstrap=False, random_state=42
)
bag_clf2.fit(x_train, y_train)
y_pred2 = bag_clf2.predict(x_test)
print('pasting accuracy: ', accuracy_score(y_test, y_pred2))

#bagging accuracy:  0.904
#pasting accuracy:  0.92

 

 

ensembleEx4

from sklearn.datasets import load_breast_cancer

cancer = load_breast_cancer()
print(cancer.DESCR)

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(cancer.data,
                                                    cancer.target,
                                                    random_state=42)
rf_clf = RandomForestClassifier(n_estimators=500, max_leaf_nodes=16, n_jobs=-1 )
#스레드 = 일꾼이 여러개 쓸수 있는것. = n_jobs / -1은 너가 쓸수 있는거 다 써
rf_clf.fit(x_train, y_train)
y_pred = rf_clf.predict(x_test)

from sklearn.metrics import accuracy_score
print('accuracy : ', accuracy_score(y_test, y_pred))

 

 

 

클래스를 나누는 결정 경계를 찾는게 서포트 벡터 머신

경계선에서 가장 가까운 것을 서포트 벡터라고 한다. 경계선과 데이터 사이의 간격을 margin 이라고 함

감마값을 크게 하면 그래프가 줄어들고 감마값을 작게하면 그래프가 넓어진다.

감마값이 크면클수록 좁아진다. 작을수록 영양력이 크다. 

svm 은 스케일작업을 해줘야함 스케일러는 붙이기 위한 작업

SVMEEx3

from scipy.odr import polynomial
from sklearn.datasets import make_moons
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.svm import LinearSVC
import matplotlib.pyplot as plt
import numpy as np

X, y = make_moons(n_samples=100, noise=0.15, random_state=42)

def plot_dataset(X, y, axes):
    plt.plot(X[:, 0][y==0], X[:, 1][y==0], "bs")
    plt.plot(X[:, 0][y==1], X[:, 1][y==1], "g^")
    plt.axis(axes)
    plt.grid(True, which='both')
    plt.xlabel(r"$x_1$", fontsize=20)
    plt.ylabel(r"$x_2$", fontsize=20, rotation=0)

plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
# plt.show()

from scipy.odr import polynomial
from sklearn.datasets import make_moons
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.svm import LinearSVC
import matplotlib.pyplot as plt
import numpy as np

X, y = make_moons(n_samples=100, noise=0.15, random_state=42)

def plot_dataset(X, y, axes):
    plt.plot(X[:, 0][y==0], X[:, 1][y==0], "bs")
    plt.plot(X[:, 0][y==1], X[:, 1][y==1], "g^")
    plt.axis(axes)
    plt.grid(True, which='both')
    plt.xlabel(r"$x_1$", fontsize=20)
    plt.ylabel(r"$x_2$", fontsize=20, rotation=0)

plot_dataset(X, y, [-1.5, 2.5, -1, 1.5])
# plt.show()

하이퍼파라미터 라고 한다 감마를

 

gridSearchEX1

import pandas as pd

wine = pd.read_csv('http://bit.ly/wine-date')
# print(wine)
# wine.info()
print()

data = wine[['alcohol', 'sugar', 'pH']].to_numpy()
print(data)
target = wine['class'].to_numpy()
print(target)

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(data,
                                                    target,
                                                    test_size=0.2,
                                                    random_state=42)
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import GridSearchCV

params = {'min_impurity_decrease': [0.0001, 0.0002, 0.0003, 0.0004, 0.0005]}
gs = GridSearchCV(DecisionTreeClassifier(random_state=42), params,
                  n_jobs=-1) #n_jobs = 스레드설정
#불순물을 최소한으로 줄이는 것
gs.fit(x_train, y_train)
dt = gs.best_estimator_
print(dt.score(x_test, y_test))
print(gs.best_params_)
print(gs.cv_results_['mean_test_score'])

'''
[[ 9.4   1.9   3.51]
 [ 9.8   2.6   3.2 ]
 [ 9.8   2.3   3.26]
 ...
 [ 9.4   1.2   2.99]
 [12.8   1.1   3.34]
 [11.8   0.8   3.26]]
[0. 0. 0. ... 1. 1. 1.]
0.8653846153846154
{'min_impurity_decrease': 0.0001}
[0.86819297 0.86453617 0.86492226 0.86780891 0.86761605]
'''

 

import numpy as np

params = {'min_impurity_decrease': np.arange(0.0001, 0.001, 0.0001),
          'max_depth':range(5, 20, 1),
          'min_samples_split': range(2, 100, 10)}
print(params)
'''
{'min_impurity_decrease': array([0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008,
       0.0009]), 'max_depth': range(5, 20), 'min_sample_split': range(2, 100, 10)}
'''

gs = GridSearchCV(DecisionTreeClassifier(random_state=42),
                  params,
                  n_jobs=-1)
gs.fit(x_train, y_train)
print(gs.best_params_)
print(gs.cv_results_['mean_test_score'])
print(np.max(gs.cv_results_['mean_test_score']))
'''
{'max_depth': 14, 'min_impurity_decrease': np.float64(0.0004), 'min_samples_split': 12}
[0.85780355 0.85799604 0.85799604 ... 0.86126601 0.86165063 0.86357629]
0.8683865773302731

'''

 

import pandas as pd
import numpy as np
from sklearn.model_selection import GridSearchCV

train = pd.read_csv('https://raw.githubusercontent.com/wikibook/machine-learning/2.0/data/csv/basketball_train.csv')
test = pd.read_csv('https://raw.githubusercontent.com/wikibook/machine-learning/2.0/data/csv/basketball_test.csv')

# train.info()
# print(train.head())
# print()
# test.info()

# svc > grid search

x_train = train[['3P', 'BLK']]
y_train = train['Pos']

x_test = test[['3P','BLK']]
y_test = test['Pos']

#앞으로 데이터가 있으면 찍어보고 해당데이터가 뭔지 확인을 꼭 해봐야 한다 수치화는 string 이면 또 안된다.
print(y_train)
print(y_test)
print(type(y_train))
print(x_train)  #dataFrame 형식


from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.model_selection import GridSearchCV

def svc_param_selection(x,y):
    svm_parameters = [
        {'kernel':['rbf'],
         'gamma':[0.00001, 0.0001, 0.001, 0.1, 1],
         'C':[0.01, 0.1, 1, 10, 100]}
    ]
    clf = GridSearchCV(SVC(), svm_parameters, n_jobs=-1)
    clf.fit(x,y.values)
    print(clf.best_params_)
    return clf

clf = svc_param_selection(x_train, y_train)
y_pred = clf.predict(x_test)
print('accuracy:', accuracy_score(y_test, y_pred))
print()
'''
[80 rows x 2 columns]
{'C': 0.1, 'gamma': 1, 'kernel': 'rbf'}
accuracy: 1.0
'''

comparison = pd.DataFrame({'prediction':y_pred,
                           'truth':y_test.values})

print(comparison)
'''
prediction truth
0           C     C
1          SG    SG
2           C     C
3          SG    SG
4           C     C
5           C     C
6           C     C
7          SG    SG
8          SG    SG
9           C     C
10         SG    SG
11          C     C
12         SG    SG
13          C     C
14          C     C
15         SG    SG
16         SG    SG
17          C     C
18         SG    SG
19          C     C
'''

'AI' 카테고리의 다른 글

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