• 莫烦python教程学习笔记——利用交叉验证计算模型得分、选择模型参数


    # View more python learning tutorial on my Youtube and Youku channel!!!
    
    # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
    # Youku video tutorial: http://i.youku.com/pythontutorial
    
    """
    Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
    """
    from __future__ import print_function
    from sklearn.datasets import load_iris
    from sklearn.cross_validation import train_test_split
    from sklearn.neighbors import KNeighborsClassifier
    
    iris = load_iris()
    X = iris.data
    y = iris.target
    
    # test train split #
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=4)
    knn = KNeighborsClassifier(n_neighbors=5)
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    print(knn.score(X_test, y_test))
    
    # this is cross_val_score # 计算模型得分
    from sklearn.cross_validation import cross_val_score
    knn = KNeighborsClassifier(n_neighbors=5)
    scores = cross_val_score(knn, X, y, cv=5, scoring='accuracy')
    print(scores)
    
    # this is how to use cross_val_score to choose model and configs # 选择模型参数
    from sklearn.cross_validation import cross_val_score
    import matplotlib.pyplot as plt
    k_range = range(1, 31)
    k_scores = []
    for k in k_range:
        knn = KNeighborsClassifier(n_neighbors=k)
    ##    loss = -cross_val_score(knn, X, y, cv=10, scoring='mean_squared_error') # for regression
        scores = cross_val_score(knn, X, y, cv=10, scoring='accuracy') # for classification
        k_scores.append(scores.mean())
    
    plt.plot(k_range, k_scores)
    plt.xlabel('Value of K for KNN')
    plt.ylabel('Cross-Validated Accuracy')
    plt.show()
  • 相关阅读:
    python-使用pyecharts绘制各省份985学校数量图
    python-将多个表格的信息合并到一个表格中
    python-使用百度AipOcr实现表格文字图片识别
    python安装OCR识别库
    python-一种去掉前后缀获取子串的方法
    python-一种字符串排序方式
    How to write educational schema.
    RabbitMq related
    OPENId是什么, OAUTH 是什么
    使用abp的 redis cache
  • 原文地址:https://www.cnblogs.com/simpleDi/p/9964228.html
Copyright © 2020-2023  润新知