【问题标题】:Scikit-learn's DecisionTreeClassifier's fit method gives ValueError: Couldn't broadcast input array from shape (10,35) into shape (10)Scikit-learn 的 DecisionTreeClassifier 的 fit 方法给出 ValueError: Couldn't broadcast input array from shape (10,35) into shape (10)
【发布时间】:2020-05-18 19:34:39
【问题描述】:

所以我正在尝试制作决策树,我的目标是数组 [0, 1](二进制“否”或“是”),而我的输入 training_set 是三维数组,第一个元素都是“否”示例( 10) 并具有 35 个功能,每个功能都与“是”相同。但我不断收到此错误。

    file1 = open(file1.txt) # examples of 'No' class
    file2 = open(file2.txt) # examples of 'Yes' class
    x = vectorizer.fit_transform(file1)
    y = vectorizer.fit_transform(file2)    

    x_array = x.toarray()    
    y_array = y.toarray()    


    x_train, x_test, y_train, y_test = train_test_split(x_array, y_array, 
    test_size=0.2)    
    target = [0, 1] # 0 encoded as 'No' and 1 as 'Yes
    train = [x_train, y_train]

    decisiontree = DecisionTreeClassifier(random_state=0, max_depth=5)
    decisiontree = decisiontree.fit(train, target)    

感谢您的帮助。

编辑:我正在从 txt 文件加载数据,它是文本数据,我尝试打印数组的某些部分,这里是

[[0 0 0 ... 0 0 0]    
 [0 0 0 ... 0 0 0]     
 [0 0 0 ... 0 0 0]     
 [0 0 0 ... 0 0 0]]    

【问题讨论】:

  • 上传更多代码,显示您的训练和目标数组。也是您的数据集样本
  • 这可能不是唯一的问题,但decisiontree = decisiontree.fit(train, target) 应该是decisiontree = decisiontree.fit(x_train, y_train)
  • @Max Power x_train 和 y_train 是“否”和“是”类的示例。目标不应该是[0, 1],0编码为“否”,1编码为“是”吗?
  • 如果 target[0,1] 并且您将其作为第二个参数传递给 fit 方法,sklearn 会将其解释为“我们正在训练两个记录/行,第一个的ground-truth值为0,第二个的ground-truth值为1. But I'm pretty sure that's not what you want, since you seem to have more than two rows of data. Also the first param you pass to fit`应该是单个二维形状数组(num_records,num_features),而不是两个不同数组的列表
  • 不过,作为更一般的建议,为了在堆栈溢出方面获得最佳结果,您应该发布一个最小的、可重现的示例,其中包括创建一些示例数据以运行您尝试调试的代码的代码.这里没有人有你的file1.txt,所以没有人可以真正运行你的代码来迭代它,直到它真正起作用。有关如何使用示例数据编写良好的完整最小示例的示例,请参见此处:stackoverflow.com/a/43298736/1870832

标签: python python-3.x machine-learning scikit-learn


【解决方案1】:

我认为原因是您对 decisiontree.fit 中的 fit 方法感到困惑。

对于decisiontree.fit(X,Y),它期望X 是数据点,Y 是标签。也就是说,如果X 的形状为N x 32,那么Y 的形状应该为N(其中N 是数据点的数量)。

你应该将x_arrayy_array合并为整个数据集,拆分,并使用相应的标签执行fit

考虑以下几点:

# from sklearn.model_selection import train_test_split
# from sklearn.tree import DecisionTreeClassifier
import numpy as np

file1 = open(file1.txt)
file2 = open(file2.txt)
x = vectorizer.fit_transform(file1)
y = vectorizer.fit_transform(file2)    

x_array = x.toarray()    
y_array = y.toarray()

# ------------------------------------------------------------
# combine the positive and negative examples
data = np.concatenate([x_array, y_array], axis=0)
# create corresponding labels (based on the data's length)
labels = np.concatenate([np.zeros(x_array.shape[0]), 
                          np.ones(y_array.shape[0])], axis=0)

# split into train and test set
train_data, test_data, train_labels, test_labels = train_test_split(
    data, labels, test_size=0.2)

decisiontree = DecisionTreeClassifier(random_state=0, max_depth=5)
decisiontree = decisiontree.fit(train_data, train_labels)

# ------------------------------------------------------------
# this is how you can test model performance with the test set
correct_predictions = np.count_nonzero(
    decisiontree.predict(test_data) == test_labels
  )

print("Correct prediction in test set: {}/{}".format(correct_predictions,
                                                       test_labels.shape[0]))

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 2019-05-23
    • 2017-06-04
    • 2016-11-12
    • 2016-12-01
    • 2019-09-22
    • 2020-07-08
    • 2021-01-22
    • 2021-04-14
    相关资源
    最近更新 更多