import pandas as pd

# Loading Data of iris
iris_data = pd.read_csv('./data/iris2.csv', encoding='utf-8')

# Spliting Data to Input data and Label
y = iris_data.loc[:, "Name"]
x = iris_data.loc[:, ["SepalLength", "SepalWidth", "PetalLength", "PetalWidth"]]

# Spliting Data to Train and Test set
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, train_size=0.8, shuffle=True)

# Parameter
parameters = [
    {"n_estimators": [1, 10, 100, 1000]}
]

# K-Fold
from sklearn.model_selection import KFold
kfold_cv = KFold(n_splits=5, shuffle=True)

# GridSearch
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
model = GridSearchCV(RandomForestClassifier(), parameters, cv=kfold_cv)
model.fit(x_train, y_train)
print("Optimal parameter: ", model.best_estimator_)

# Evaluating in optimal parameter
y_pred = model.predict(x_test)

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