4.3訓練數(shù)據(jù)集、測試數(shù)據(jù)集

4.3訓練數(shù)據(jù)集、測試數(shù)據(jù)集

1.判斷機器學習算法的性能

image.png
測試我們的算法
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets

iris = datasets.load_iris()

x = iris.data
y = iris.target
train_test_split

將原始數(shù)據(jù)集拆分成兩部分,一部分是訓練數(shù)據(jù)集,一部分是測試數(shù)據(jù)集。

# 先對原始數(shù)據(jù)進行隨機化,但是因為x.y對應的關(guān)系,所以隨機化處理是對應關(guān)系應該保持一致。
# 隨機化方法:可以先把X.y合成一個矩陣然后隨機取出一部分數(shù)據(jù);另一種方法是先對y進行隨機化
# 形成15個索引的隨機序列
shuffle_indexes = np.random.permutation(len(X)
# 查看序列
shuffle_indexes
# 指定選取測試數(shù)據(jù)集的比例
test_ratio = 0.2
test_size = int(len(X) * test_ratio)
# 獲得測試數(shù)據(jù)集索引
test_indexes = shuffle_indexes[:test_size]
# 獲得訓練數(shù)據(jù)集索引
train_indexes = shuffle_indexes[test_size:]

# 獲取測試和訓練數(shù)據(jù)
X_train = X[train_indexes]
y_train = y[train_indexes]

X_test = X[test_indexes]
y_test = y[test_indexes]

創(chuàng)建model_selection.py文件

# 分割原始數(shù)據(jù)集為測試數(shù)據(jù)集和訓練數(shù)據(jù)集

import numpy as np

def train_test_split(X, y, test_ratio=0.2, seed=None):
    assert X.shape[0] == y.shape[0], \
        "the size of X must be equal to the size of y"
    assert 0.0 <= test_ratio <= 1.0, \
        "test_ratio must be valid"

    if seed:
        np.random.seed(seed)

    shuffled_indexes = np.random.permutation(len(X))

    test_size = int(len(X * test_ratio))
    test_indexes = shuffled_indexes[:test_size]
    train_indexes = shuffled_indexes[test_size:]

    X_train = X[train_indexes]
    y_train = y[train_indexes]

    X_test = X[test_indexes]
    y_test = y[test_indexes]

    return X_train, y_train, X_test, y_test
測試使用我們的算法
from playML.model_selection import train_test_split
X_train, y_train, X_test, y_test = train_test_split(X, y)

from playML.kNN2 import KNNClassifier
my_knn_clf = KNNClassifier(k=3)
my_knn_clf.fit(X_train, y_train)
y_predict = my_knn_clf.predict(X_test)
# 得出預測結(jié)果
y_predict
# 檢驗預測結(jié)果和實際結(jié)果
sum(y_predict == y_test)
# 計算預測準確率
sum(y_predict == y_test)/len(y_test)
sklearn中的train_test_split
from sklearn.model_selection import train_test_split
# random_state設置隨機種子
X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.2, random_state=666)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容