【发布时间】: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 tofit`应该是单个二维形状数组(num_records,num_features),而不是两个不同数组的列表 -
不过,作为更一般的建议,为了在堆栈溢出方面获得最佳结果,您应该发布一个最小的、可重现的示例,其中包括创建一些示例数据以运行您尝试调试的代码的代码.这里没有人有你的
file1.txt,所以没有人可以真正运行你的代码来迭代它,直到它真正起作用。有关如何使用示例数据编写良好的完整最小示例的示例,请参见此处:stackoverflow.com/a/43298736/1870832
标签: python python-3.x machine-learning scikit-learn