Python 使用sklearn digits数据集预测数字-错误

Python 使用sklearn digits数据集预测数字-错误,python,machine-learning,scikit-learn,digits,Python,Machine Learning,Scikit Learn,Digits,我想建立一个简单的数字预测模型 因此,我: 加载到sklearn数据集中 使用DecisionTreeClassifier()命令 符合数据 预测新形象 我做错了什么 ValueError Traceback (most recent call last) <ipython-input-9-b58a2a08d39b> in <module>() ----> 1 clf.predict(digits.dat

我想建立一个简单的数字预测模型

因此,我:

  • 加载到sklearn数据集中
  • 使用DecisionTreeClassifier()命令
  • 符合数据
  • 预测新形象
  • 我做错了什么

    ValueError                                Traceback (most recent call last)
    <ipython-input-9-b58a2a08d39b> in <module>()
    ----> 1 clf.predict(digits.data[-1])
    
    ValueError回溯(最近一次调用)
    在()
    ---->1 clf.预测(数字.数据[-1])
    
    您的问题是,当模型请求2D数组时,您正在传递1D数组

    这应该能奏效

    from sklearn import datasets
    from sklearn import tree
    from sklearn.model_selection import StratifiedKFold
    
    # load the digits dataset
    digits = datasets.load_digits()
    
    # separate features and labels
    X_digits = digits.data
    y_digits = digits.target
    
    # split data into training and testing sets
    k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)
    for train_index, test_index in k_fold.split(X_digits, y_digits):
            train_features, test_features = X_digits[train_index], X_digits[test_index]
            train_labels, test_labels = y_digits[train_index], y_digits[test_index]
    
    # fit to model
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(train_features, train_labels)
    
    # predict on the testing features
    print(clf.predict(test_features))
    

    还有,看一看。它可能会为您提供更多信息。

    您的问题是,当模型请求二维阵列时,您正在传递一维阵列

    这应该能奏效

    from sklearn import datasets
    from sklearn import tree
    from sklearn.model_selection import StratifiedKFold
    
    # load the digits dataset
    digits = datasets.load_digits()
    
    # separate features and labels
    X_digits = digits.data
    y_digits = digits.target
    
    # split data into training and testing sets
    k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)
    for train_index, test_index in k_fold.split(X_digits, y_digits):
            train_features, test_features = X_digits[train_index], X_digits[test_index]
            train_labels, test_labels = y_digits[train_index], y_digits[test_index]
    
    # fit to model
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(train_features, train_labels)
    
    # predict on the testing features
    print(clf.predict(test_features))
    

    还有,看一看。它可能会为您提供进一步的信息。

    因为这似乎是您使用sklearn的开始,所以我想从功能和示例方面向您介绍一下这些东西是如何工作的

    要训练模型,需要使用两个属性来拟合分类器,即特征向量和每个向量的标签。特征向量由
    2dn*M
    数组表示,其中每行对应一个新样本,每列对应一个特定特征。标签必须是大小为N的
    1D
    数组,即每个样品一个标签。(每个样本甚至可以有多个标签,但现在让我们重点关注每个样本一个标签)

    现在使用上述概念,您可以找出预测函数中的错误

    digits.data
    是一个
    2D
    数组和数字,其中每行对应一个样本。现在
    数字。数据[-1]
    是一个
    1D
    数组,它没有任何列,但您知道该
    1D
    数组中的每个数字实际上是一个特征(即一列)。因此,您需要将该
    1D
    数组重塑为一个包含1行和N列的
    2D
    数组。在这里,numpy的重塑方法可以派上用场。只需执行
    数字。数据[-1]。重塑(1,-1)
    即可达到预期效果并应用于预测方法。然后,您的代码将成为

    clf.predict(digits.data[-1].reshape(1,-1))
    

    请考虑阅读numpy.reforme的教程,了解
    .reforme()
    方法正在做什么以及如何做的更多细节。

    由于这似乎是您使用sklearn的开始,我想从功能和示例方面为您介绍一些基本的工作原理

    要训练模型,需要使用两个属性来拟合分类器,即特征向量和每个向量的标签。特征向量由
    2dn*M
    数组表示,其中每行对应一个新样本,每列对应一个特定特征。标签必须是大小为N的
    1D
    数组,即每个样品一个标签。(每个样本甚至可以有多个标签,但现在让我们重点关注每个样本一个标签)

    现在使用上述概念,您可以找出预测函数中的错误

    digits.data
    是一个
    2D
    数组和数字,其中每行对应一个样本。现在
    数字。数据[-1]
    是一个
    1D
    数组,它没有任何列,但您知道该
    1D
    数组中的每个数字实际上是一个特征(即一列)。因此,您需要将该
    1D
    数组重塑为一个包含1行和N列的
    2D
    数组。在这里,numpy的重塑方法可以派上用场。只需执行
    数字。数据[-1]。重塑(1,-1)
    即可达到预期效果并应用于预测方法。然后,您的代码将成为

    clf.predict(digits.data[-1].reshape(1,-1))
    

    请考虑查看numpy.reforme的详细信息,了解
    .reforme()
    方法的作用和方式。

    这应该是您的错误:

    ValueError:应为2D数组,而应为1D数组: 数组=[0.0.10.14.8.1.0.0.0.2.16.14.6.1.0.0.0.0。 15. 15. 8. 15. 0. 0. 0. 0. 5. 16. 16. 10. 0. 0. 0. 0. 12. 15. 15. 12. 0. 0. 0. 4. 16. 6. 4. 16. 6. 0. 0. 8. 16. 10. 8. 16. 8. 0. 0. 1. 8. 12. 14. 12. 1. 0.]. 使用数组重塑数据。如果数据具有单个特征或数组,则重塑(-1,1)。如果数据包含单个样本,则重塑(1,-1)

    除了最后一排,你把所有的都淘汰了。这就是你想做的吗

    import numpy as np
    arr = np.array([[11,52], [46, 49], [35, 26]])
    arr[-1]
    
    你可以和我一起去

    digits.data[-1].reshape(-1, 1)  
    

    但只有当您仅使用一维数组(如错误所示)训练您的模型时。

    这应该是您的错误:

    ValueError:应为2D数组,而应为1D数组: 数组=[0.0.10.14.8.1.0.0.0.2.16.14.6.1.0.0.0.0。 15. 15. 8. 15. 0. 0. 0. 0. 5. 16. 16. 10. 0. 0. 0. 0. 12. 15. 15. 12. 0. 0. 0. 4. 16. 6. 4. 16. 6. 0. 0. 8. 16. 10. 8. 16. 8. 0. 0. 1. 8. 12. 14. 12. 1. 0.]. 使用数组重塑数据。如果数据具有单个特征或数组,则重塑(-1,1)。如果数据包含单个样本,则重塑(1,-1)

    除了最后一排,你把所有的都淘汰了。这就是你想做的吗

    import numpy as np
    arr = np.array([[11,52], [46, 49], [35, 26]])
    arr[-1]
    
    你可以和我一起去

    digits.data[-1].reshape(-1, 1)  
    

    但是,如果你只使用一维数组来训练你的模型,就像错误所说的那样。

    非常感谢你的回答。你们都对这一点作出了回应。现在,我明白了错误。实际上,我从和中学习了这个例子

    它主要是python版本,在python predict函数的前一个版本中接受1D数组,但现在它已被弃用,因此通过在1D数组周围使用大括号(如[1D array])将使其成为2D数组。现在,下面的代码工作得很好

    import matplotlib.pyplot as plt
    
    from sklearn import datasets
    
    from sklearn import tree
    
    digits = datasets.load_digits()
    
    clf = tree.DecisionTreeClassifier()
    
    clf = clf.fit(digits.data[:-1], digits.target[:-1])
    
    clf.predict(digits.data[[-1]])
    
    输出为数组([8])


    再次感谢您的帮助

    非常感谢您的回答。你们都对这一点作出了回应。现在,我明白了错误。实际上,我从和中学习了这个例子

    它主要是python版本,在python predict函数的前一个版本中接受1D数组,但现在它已被弃用,因此在1D数组周围使用大括号,如[1D arr]