阅读背景:

1.5 KNN算法学习——KNN算法分类模型的实现与分类准确度评估

来源:互联网 
训练集分割成训练集与测试集,代码封装
import numpy as np

def train_test_split(X,y,test_ratio=0.2,seed=None):
    """将数据X与y按照test_ratio分割成X_train,X_test,y_train,y_test"""
    assert X.shape[0] == y.shape[0], "the size of X must equal to the sise of y"
    assert 0.0 <=test_ratio <= 1.0,"test_ratio must be valid"

    if seed:
        np.random.seed(seed)

    shuffled_index=np.random.permutation(len(X))
    test_size=int(len(X)*test_ratio)
    test_index=shuffled_index[:test_size]
    train_index=shuffled_index[test_size:]

    X_train=X[train_index]
    y_train=y[train_index]

    X_test=X[test_index]
    y_test=y[test_index]

    return X_train,X_test,y_train,y_testimport numpy as np

def tr



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: