数据分类器

自己实现

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 be equal to the size of y"
    assert 0.0 <= test_ratio <= 1.0, 
        "test_ration 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, X_test, y_train, y_test

sklearn带的分类器‘

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,random_state=666)

利用KNN算法测试

from sklearn.neighbors import KNeighborsClassifier
KNN_classifier = KNeighborsClassifier(n_neighbors=3)
KNN_classifier.fit(X_train,y_train)
Y_predict = KNN_classifier.predict(X_test)
Y_predict

判断准确率

sum(Y_predict==y_test)/len(y_test)
原文地址:https://www.cnblogs.com/Erick-L/p/9008347.html