<jupyter_start><jupyter_text>KNN<jupyter_code>import pickle
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import  classification_report, confusion_matrix
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedShuffleSplit

#資料劃分
x_train, x_test, y_train, y_test = train_test_split(feature, answer, test_size=0.3, random_state=9)
#參數區間
param_grid = {'n_neighbors':[1, 2, 3, 4, 5]}
#交叉驗證 
cv = StratifiedShuffleSplit(n_splits=2, test_size=0.3, random_state=12)
grid = GridSearchCV(KNeighborsClassifier(n_neighbors = 5), param_grid, cv=cv, verbose=10, n_jobs=-1) #n_jobs為平行運算的數量
grid.fit(x_train, y_train)
grid_predictions = grid.predict(x_test)

#儲存
file = open('model_KNN.pickle', 'wb')
pickle.dump(grid, file)
file.close()

#最佳參數
print(grid.best_params_)
#預測結果
#print(grid_predictions)
#混淆矩陣
print(confusion_matrix(y_test, grid_predictions))
#分類結果
print(classification_report(y_test, grid_predictions))<jupyter_output>{'n_neighbors': 5}
[[6164   75  373  345    0]
 [  71 6402  356  352    0]
 [ 351  360 8975  105    0]
 [ 368  397  123 8683    0]
 [   5    5    7   25    1]]
              precision    recall  f1-score   support

           0       0.89      0.89      0.89      6957
           1       0.88      0.89      0.89      7181
           2       0.91      0.92      0.91      9791
           3       0.91      0.91      0.91      9571
           4       1.00      0.02      0.05        43

    accuracy                           0.90     33543
   macro avg       0.92      0.72      0.73     33543
weighted avg       0.90      0.90      0.90     33543<jupyter_text>MLPClassifier<jupyter_code>from sklearn.neural_network import MLPClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(feature, answer, test_size=0.3, random_state = 7)
MLPC = MLPClassifier(random_state=1, max_iter=300).fit(x_train, y_train)

MLPC.predict_proba(x_test)
MLPC.predict(x_test)
MLPC.score(x_test, y_test)

file = open("model_MLPC.pickle", "wb")
pickle.dump(MLPC, file)
file.close()
print("Accuracy score (validation): {0:.3f}".format(MLPC.score(x_test, y_test)))
print("Confusion Matrix for Raandom Forests:")
print(confusion_matrix(y_test, MLPC.predict(x_test)))
print()
print("Classification Report for Random Forests")
print(classification_report(y_test, MLPC.predict(x_test)))<jupyter_output>Accuracy score (validation): 0.603
Confusion Matrix for Raandom Forests:
[[ 1575     0     0     0]
 [    0  1009    44   456]
 [    0   849 14806  6395]
 [    0   449 10651 11176]]

Classification Report for Random Forests
              precision    recall  f1-score   support

           0       1.00      1.00      1.00      1575
           1       0.44      0.67      0.53      1509
           2       0.58      0.67      0.62     22050
           3       0.62      0.50      0.55     22276

    accuracy                           0.60     47410
   macro avg       0.66      0.71      0.68     47410
weighted avg       0.61      0.60      0.60     47410<jupyter_text>HistGradientBoostingClassifier (會delay不要用)<jupyter_code>from sklearn.experimental import enable_hist_gradient_boosting  # noqa
from sklearn.ensemble import HistGradientBoostingClassifier

x_train, x_test, y_train, y_test = train_test_split(feature, answer, test_size=0.3, random_state = 7)
HGBC = HistGradientBoostingClassifier(random_state=1, max_iter=300).fit(x_train, y_train)

HGBC.predict_proba(x_test)
HGBC.predict(x_test)
HGBC.score(x_test, y_test)

file = open("model_HGBC.pickle", "wb")
pickle.dump(HGBC, file)
file.close()
print("Accuracy score (validation): {0:.3f}".format(HGBC.score(x_test, y_test)))
print("Confusion Matrix for Raandom Forests:")
print(confusion_matrix(y_test, HGBC.predict(x_test)))
print()
print("Classification Report for Random Forests")
print(classification_report(y_test, HGBC.predict(x_test)))<jupyter_output>Accuracy score (validation): 0.988
Confusion Matrix for Raandom Forests:
[[6812   20   11   14    6]
 [  17 7053    3   19    5]
 [  79   66 9764    2    5]
 [  71   70    5 9476    7]
 [   4    2    2    8   22]]

Classification Report for Random Forests
              precision    recall  f1-score   support

           0       0.98      0.99      0.98      6863
           1       0.98      0.99      0.99      7097
           2       1.00      0.98      0.99      9916
           3       1.00      0.98      0.99      9629
           4       0.49      0.58      0.53        38

    accuracy                           0.99     33543
   macro avg       0.89      0.91      0.90     33543
weighted avg       0.99      0.99      0.99     33543<jupyter_text>DecisionTree<jupyter_code>from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(feature, answer, test_size=0.3, random_state = 7)
DT = DecisionTreeClassifier(random_state=0).fit(x_train, y_train)

DT.predict_proba(x_test)
DT.predict(x_test)
DT.score(x_test, y_test)

file = open("model_DT.pickle", "wb")
pickle.dump(DT, file)
file.close()
print("Accuracy score (validation): {0:.3f}".format(DT.score(x_test, y_test)))
print("Confusion Matrix for Raandom Forests:")
print(confusion_matrix(y_test, DT.predict(x_test)))
print()
print("Classification Report for Random Forests")
print(classification_report(y_test, DT.predict(x_test)))<jupyter_output>Accuracy score (validation): 0.989
Confusion Matrix for Raandom Forests:
[[6789   46   15    6    7]
 [  52 7022    6   11    6]
 [  40   29 9826   14    7]
 [  43   33   16 9534    3]
 [   4    2    5    8   19]]

Classification Report for Random Forests
              precision    recall  f1-score   support

           0       0.98      0.99      0.98      6863
           1       0.98      0.99      0.99      7097
           2       1.00      0.99      0.99      9916
           3       1.00      0.99      0.99      9629
           4       0.45      0.50      0.48        38

    accuracy                           0.99     33543
   macro avg       0.88      0.89      0.89     33543
weighted avg       0.99      0.99      0.99     33543<jupyter_text>Extra tree<jupyter_code>from sklearn.model_selection import cross_val_score
from sklearn.ensemble import BaggingClassifier
from sklearn.model_selection import train_test_split
from sklearn.tree import ExtraTreeClassifier

x_train, x_test, y_train, y_test = train_test_split(feature, answer, test_size=0.3, random_state = 7)
cls = ExtraTreeClassifier(random_state=0)
ET = BaggingClassifier(cls, random_state=0).fit(x_train, y_train)

ET.predict_proba(x_test)
ET.predict(x_test)
ET.score(x_test, y_test)

file = open("model_ET.pickle", "wb")
pickle.dump(ET, file)
file.close()
print("Accuracy score (validation): {0:.3f}".format(ET.score(x_test, y_test)))
print("Confusion Matrix for Raandom Forests:")
print(confusion_matrix(y_test, ET.predict(x_test)))
print()
print("Classification Report for Random Forests")
print(classification_report(y_test, ET.predict(x_test)))<jupyter_output>Accuracy score (validation): 0.980
Confusion Matrix for Raandom Forests:
[[6729   32   30   72    0]
 [  30 6962   51   54    0]
 [  76   80 9748   12    0]
 [  78   91   22 9438    0]
 [   4   11    6   12    5]]

Classification Report for Random Forests
              precision    recall  f1-score   support

           0       0.97      0.98      0.98      6863
           1       0.97      0.98      0.98      7097
           2       0.99      0.98      0.99      9916
           3       0.98      0.98      0.98      9629
           4       1.00      0.13      0.23        38

    accuracy                           0.98     33543
   macro avg       0.98      0.81      0.83     33543
weighted avg       0.98      0.98      0.98     33543<jupyter_text>SVM.svc(train很久不要用)<jupyter_code>import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC

SVC = make_pipeline(StandardScaler(), SVC(gamma='auto',probability=True))
SVC.fit(x_train, y_train)

SVC.predict_proba(x_test)
SVC.predict(x_test)
SVC.score(x_test, y_test)

file = open("model_SVC.pickle", "wb")
pickle.dump(SVC, file)
file.close()
print("Accuracy score (validation): {0:.3f}".format(SVC.score(x_test, y_test)))
print("Confusion Matrix for Raandom Forests:")
print(confusion_matrix(y_test, SVC.predict(x_test)))
print()
print("Classification Report for Random Forests")
print(classification_report(y_test, SVC.predict(x_test)))<jupyter_output>Accuracy score (validation): 0.981
Confusion Matrix for Raandom Forests:
[[6650   10   56  147    0]
 [   5 6980   48   64    0]
 [  64   59 9792    1    0]
 [  77   62    1 9489    0]
 [   4   12    6   12    4]]

Classification Report for Random Forests
              precision    recall  f1-score   support

           0       0.98      0.97      0.97      6863
           1       0.98      0.98      0.98      7097
           2       0.99      0.99      0.99      9916
           3       0.98      0.99      0.98      9629
           4       1.00      0.11      0.19        38

    accuracy                           0.98     33543
   macro avg       0.98      0.81      0.82     33543
weighted avg       0.98      0.98      0.98     33543
{'fraction_non_alphanumeric': 0.0903062835491001, 'fraction_numerical': 0.10419955794126934, 'mean_word_length': 2.2374787052810903, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 29, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '404520', 'license': 'MIT', 'n_tokens_mistral': 3853, 'n_tokens_neox': 3388, 'n_words': 814, 'path': 'MLGame/games/snake/ml/others/train.py_knn.ipynb', 'repo_name': 'Liuian/1092_INTRODUCTION-TO-MACHINE-LEARNING-AND-ITS-APPLICATION-TO-GAMING', 'size': '9501'}