【问题标题】:Machine Learning Using Scikit-Learn & SVM使用 Scikit-Learn 和 SVM 进行机器学习
【发布时间】:2020-12-10 18:17:37
【问题描述】:

sklearn.datasets 模块加载流行数字数据集并将其分配给可变数字。

digits.data 拆分为两组名称X_trainX_test。此外,将digits.target 分成两组Y_trainY_test

提示:使用sklearn.model_selection中的train_test_split()方法;将random_state 设置为 30;并进行分层抽样。 使用默认参数从X_train 集合和Y_train 标签构建一个 SVM 分类器。将模型命名为svm_clf

在测试数据集上评估模型准确性并打印其分数。 我使用了以下代码:

import sklearn.datasets as datasets
import sklearn.model_selection as ms
from sklearn.model_selection import train_test_split


digits = datasets.load_digits();
X = digits.data
y = digits.target

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=30)
    
print(X_train.shape)
print(X_test.shape)

from sklearn.svm import SVC
svm_clf = SVC().fit(X_train, y_train)
print(svm_clf.score(X_test,y_test))

我得到了以下输出。

(1347,64)
(450,64)
0.4088888888888889

但我无法通过测试。有人可以帮忙解决问题吗?

【问题讨论】:

    标签: python machine-learning scikit-learn svm


    【解决方案1】:

    您缺少分层抽样要求;修改您的拆分以包含它:

    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=30, stratify=y)
    

    检查documentation

    【讨论】:

      【解决方案2】:

      即使使用了这个,它也没有通过:

      X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=30, stratify=y)
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2017-08-22
      • 2019-04-16
      • 2020-10-26
      • 2013-06-17
      • 2019-02-08
      • 2016-06-18
      • 1970-01-01
      • 2020-04-17
      • 2020-04-24
      相关资源
      最近更新 更多