<jupyter_start><jupyter_text>分類法 decision tree<jupyter_code># 引用 sklearn
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.model_selection import train_test_split	#用來分割訓練/測試資料
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier

# 讀入鳶尾花資料
iris = load_iris()
iris_X = iris.data
iris_y = iris.target
# 切分訓練與測試資料
train_X, test_X, train_y, test_y = train_test_split(iris_X, iris_y, test_size = 0.3)
#https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html

# 建立分類器
clf = tree.DecisionTreeClassifier()
iris_clf = clf.fit(train_X, train_y)

# 預測
test_y_predicted = iris_clf.predict(test_X)
print(test_y_predicted)

# 標準答案
print(test_y)
# 績效
accuracy = accuracy_score(test_y, test_y_predicted)
print(accuracy)<jupyter_output>0.9555555555555556<jupyter_text>decision tree 分類器是建立一顆決策樹 用來預測時就從樹根開始跑  比較test_size大小<jupyter_code>for i in range(0,2):
    if i == 1 :
        size = 0.4
    else:
        size = 0.6
    train_X, test_X, train_y, test_y = train_test_split(iris_X, iris_y, test_size = size)
    clf = tree.DecisionTreeClassifier()
    iris_clf = clf.fit(train_X, train_y)
    test_y_predicted = iris_clf.predict(test_X)
    accuracy = accuracy_score(test_y, test_y_predicted)
    print(accuracy)<jupyter_output>0.9444444444444444
0.9333333333333333<jupyter_text>KNN (k nearest neighbor)<jupyter_code>#分割資料
train_X, test_X, train_y, test_y = train_test_split(iris_X, iris_y, test_size = 0.3)
#利用KNN演算法建立分類器
clf = KNeighborsClassifier(n_neighbors = 5) #k = 5
iris_clf = clf.fit(train_X, train_y)
#利用分類器預測test_x的結果
test_y_predicted = iris_clf.predict(test_X)
#比較結果與實際的答案差多少
accuracy = accuracy_score(test_y, test_y_predicted)
print(accuracy)<jupyter_output>0.9555555555555556
{'fraction_non_alphanumeric': 0.09371492704826039, 'fraction_numerical': 0.045454545454545456, 'mean_word_length': 6.337448559670782, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '280980', 'license': 'MIT', 'n_tokens_mistral': 894, 'n_tokens_neox': 875, 'n_words': 123, 'path': 'data_mining/classification.ipynb', 'repo_name': 'leon123858/python_exercise', 'size': '1782'}