sklearn.svm.SVC
sklearn.svm.SVR
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
data1=pd.read_csv('breast-cancer-wisconsin.csv', encoding='utf-8')
X=data1[data1.columns[1:10]]
y=data1[["Class"]]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test=train_test_split(X, y, stratify=y, random_state=42)
from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()
scaler.fit(X_train)
X_scaled_train=scaler.transform(X_train)
X_scaled_test=scaler.transform(X_test)
from sklearn.svm import SVC
model=SVC()
model.fit(X_scaled_train, y_train)
pred_train=model.predict(X_scaled_train)
model.score(X_scaled_train, y_train)
0.984375
from sklearn.metrics import confusion_matrix
confusion_train=confusion_matrix(y_train, pred_train)
print("훈련데이터 오차행렬:\n", confusion_train)
훈련데이터 오차행렬:
[[329 4]
[ 4 175]]
from sklearn.metrics import classification_report
cfreport_train=classification_report(y_train, pred_train)
print("분류예측 레포트:\n", cfreport_train)
분류예측 레포트:
precision recall f1-score support
0 0.99 0.99 0.99 333
1 0.98 0.98 0.98 179
accuracy 0.98 512
macro avg 0.98 0.98 0.98 512
weighted avg 0.98 0.98 0.98 512
pred_test=model.predict(X_scaled_test)
model.score(X_scaled_test, y_test)
0.9649122807017544
confusion_test=confusion_matrix(y_test, pred_test)
print("테스트데이터 오차행렬:\n", confusion_test)
테스트데이터 오차행렬:
[[106 5]
[ 1 59]]
from sklearn.metrics import classification_report
cfreport_test=classification_report(y_test, pred_test)
print("분류예측 레포트:\n", cfreport_test)
분류예측 레포트:
precision recall f1-score support
0 0.99 0.95 0.97 111
1 0.92 0.98 0.95 60
accuracy 0.96 171
macro avg 0.96 0.97 0.96 171
weighted avg 0.97 0.96 0.97 171
param_grid=[{'kernel': ['rbf'], 'C': [0.001, 0.01, 0.1, 1, 10, 100],
'gamma': [0.001, 0.01, 0.1, 1, 10, 100]},
{'kernel': ['linear'], 'C': [0.001, 0.01, 0.1, 1, 10, 100],
'gamma': [0.001, 0.01, 0.1, 1, 10, 100]}]
from sklearn.model_selection import GridSearchCV
grid_search=GridSearchCV(SVC(), param_grid, cv=5)
grid_search.fit(X_scaled_train, y_train)
GridSearchCV(cv=5, estimator=SVC(),
param_grid=[{'C': [0.001, 0.01, 0.1, 1, 10, 100],
'gamma': [0.001, 0.01, 0.1, 1, 10, 100],
'kernel': ['rbf']},
{'C': [0.001, 0.01, 0.1, 1, 10, 100],
'gamma': [0.001, 0.01, 0.1, 1, 10, 100],
'kernel': ['linear']}])
print("Best Parameter: {}".format(grid_search.best_params_))
print("Best Score: {:.4f}".format(grid_search.best_score_))
print("TestSet Score: {:.4f}".format(grid_search.score(X_scaled_test, y_test)))
Best Parameter: {'C': 100, 'gamma': 0.01, 'kernel': 'rbf'}
Best Score: 0.9746
TestSet Score: 0.9591
from scipy.stats import randint
param_distribs={'kernel': ['rbf'], 'C': randint(low=0.001, high=100),
'gamma': randint(low=0.001, high=100)}
from sklearn.model_selection import RandomizedSearchCV
random_search=RandomizedSearchCV(SVC(),
param_distributions=param_distribs, n_iter=100, cv=5)
random_search.fit(X_scaled_train, y_train)
RandomizedSearchCV(cv=5, estimator=SVC(), n_iter=100,
param_distributions={'C': <scipy.stats._distn_infrastructure.rv_frozen object at 0x000001E558FB7B20>,
'gamma': <scipy.stats._distn_infrastructure.rv_frozen object at 0x000001E558FB79D0>,
'kernel': ['rbf']})
print("Best Parameter: {}".format(random_search.best_params_))
print("Best Score: {:.4f}".format(random_search.best_score_))
print("TestSet Score: {:.4f}".format(random_search.score(X_scaled_test, y_test)))
Best Parameter: {'C': 65, 'gamma': 5, 'kernel': 'rbf'}
Best Score: 0.9648
TestSet Score: 0.9649
data2=pd.read_csv('house_price.csv', encoding='utf-8')
X=data2[data2.columns[1:5]]
y=data2[["house_value"]]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test=train_test_split(X, y, random_state=42)
from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler()
scaler.fit(X_train)
X_scaled_train=scaler.transform(X_train)
X_scaled_test=scaler.transform(X_test)
from sklearn.svm import SVR
model=SVR(kernel='poly')
model.fit(X_scaled_train, y_train)
pred_train=model.predict(X_scaled_train)
model.score(X_scaled_train, y_train)
0.4517702565282381
pred_test=model.predict(X_scaled_test)
model.score(X_scaled_test, y_test)
0.4699770809619134
# RMSE (Root Mean Squared Error)
import numpy as np
from sklearn.metrics import mean_squared_error
MSE_train = mean_squared_error(y_train, pred_train)
MSE_test = mean_squared_error(y_test, pred_test)
print("훈련 데이터 RMSE:", np.sqrt(MSE_train))
print("테스트 데이터 RMSE:", np.sqrt(MSE_test))
훈련 데이터 RMSE: 70669.55244251262
테스트 데이터 RMSE: 69600.0895993858
param_grid={'kernel': ['poly'], 'C': [0.01, 0.1, 1, 10],
'gamma': [0.01, 0.1, 1, 10]}
from sklearn.model_selection import GridSearchCV
grid_search=GridSearchCV(SVR(kernel='poly'), param_grid, cv=5)
grid_search.fit(X_scaled_train, y_train)
GridSearchCV(cv=5, estimator=SVR(kernel='poly'),
param_grid={'C': [0.01, 0.1, 1, 10], 'gamma': [0.01, 0.1, 1, 10],
'kernel': ['poly']})
print("Best Parameter: {}".format(grid_search.best_params_))
print("Best Score: {:.4f}".format(grid_search.best_score_))
print("TestSet Score: {:.4f}".format(grid_search.score(X_scaled_test, y_test)))
Best Parameter: {'C': 10, 'gamma': 10, 'kernel': 'poly'}
Best Score: 0.4888
TestSet Score: 0.5092
param_distribs={'kernel': ['poly'], 'C': randint(low=0.01, high=10),
'gamma': randint(low=0.01, high=10)}
from sklearn.model_selection import RandomizedSearchCV
random_search=RandomizedSearchCV(SVR(kernel='poly'),
param_distributions=param_distribs, n_iter=20, cv=5)
random_search.fit(X_scaled_train, y_train)
RandomizedSearchCV(cv=5, estimator=SVR(kernel='poly'), n_iter=20,
param_distributions={'C': <scipy.stats._distn_infrastructure.rv_frozen object at 0x000001E558FCC940>,
'gamma': <scipy.stats._distn_infrastructure.rv_frozen object at 0x000001E558FCCB50>,
'kernel': ['poly']})
print("Best Parameter: {}".format(random_search.best_params_))
print("Best Score: {:.4f}".format(random_search.best_score_))
print("TestSet Score: {:.4f}".format(random_search.score(X_scaled_test, y_test)))
Best Parameter: {'C': 7, 'gamma': 9, 'kernel': 'poly'}
Best Score: 0.4682
TestSet Score: 0.4922