from icecream import ic
import warnings
import numpy as np
import pandas as pd
from time import time

from sklearn.datasets import load_breast_cancer

from sklearn.model_selection import KFold, cross_val_score, train_test_split, GridSearchCV
from sklearn.metrics import accuracy_score

# KFold, Cross_Validation

from sklearn.ensemble import RandomForestClassifier

warnings.filterwarnings('ignore')   # 오류구문 무시


dataset = pd.read_csv('../_data/winequality-white.csv', sep=';', index_col=None, header=0)
print(dataset)

print(dataset.shape)
print(dataset.info())
print(dataset.describe())

# wine의 quailty를 y로 잡음

y = dataset['quality'].to_numpy()
x = dataset.drop(columns='quality')

print(x.shape, y.shape)  # (150, 4) (150,)

x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8, shuffle=True, random_state=41)

# Model

kfold = KFold(n_splits=5, shuffle=True, random_state=41)

parameters = [
    {'n_estimators' : [100, 200],
    'max_depth' : [6, 8, 10, 12],
    'min_samples_leaf' : [3, 5, 7, 10],
    'min_samples_split' : [2, 3, 5, 10],
    'n_jobs' : [-1, 2, 4]}
]

'''
parameters = [
    {'n_estimators' : [100, 200],
    'max_depth' : [6, 8, 10, 12],
    'min_samples_leaf' : [3, 5, 7, 10],
    'min_samples_split' : [2, 3, 5, 10],
    'n_jobs' : [-1, 2, 4]}
]
-> 연산 수 -> 5 * {2 * 4 * 4 * 4 * 3} = 1920
-> 원판(7-5) 대비 연산량이 85 -> 1920으로 20배 이상 증가함, 엄청 오래 걸림
'''

# GridSearchCV로 감싸기

start_time = time()

model = GridSearchCV(RandomForestClassifier(), parameters, cv=kfold, verbose=1)


model.fit(x_train, y_train)

ic(model.best_estimator_)

ic(model.best_score_)

ic(model.score(x_test, y_test))

y_predict = model.predict(x_test)
print('정답률 : ', accuracy_score(y_test, y_predict))

elapsed_time = time() - start_time

ic(elapsed_time)

'''
ic| model.best_estimator_: RandomForestClassifier(n_jobs=2)
ic| model.best_score_: 0.670248195063466
ic| model.score(x_test, y_test): 0.6857142857142857
정답률 :  0.6857142857142857
'''