python - TypeError:单例数组数组(0.5)不能被视为有效集合
问题描述
我有以下错误,但我真的不明白它为什么会发生以及如何解决它。
这是我的代码:
from sklearn import naive_bayes, tree, metrics
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import numpy as np
我首先加载了虹膜数据,然后获取了其对应的标签。
# get all data
iris = load_iris()
# get raw data and label
labeled_data = list(zip(iris_data, iris_labels))
X, y = np.arange(10).reshape((5, 2)), range(5)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=25)
然后我运行一个分类器。
# 运行指定的分类器 def run_classifier(classifier, training, testing): classifier.fit(*training)
# classifier.fit(X_train, y_train, batch_size=10, epochs=100)
expect = testing[1]
predict = classifier.predict(testing[0])
return expect, predict
我使用朴素贝叶斯和决策树进行分类。
# 收集关于训练大小高原的数据 def simulation(): # 在测试数据大小范围内取得进展 nb_acc = [] tree_acc = [] training_fracs = [x/1000 for x in range(500, 850, 25)]
for i in training_fracs:
nb = naive_bayes.CategoricalNB()
dt = tree.DecisionTreeClassifier()
training, testing = train_test_split(i)
nb_expect, nb_predict = run_classifier(nb, training, testing)
dt_expect, dt_predict = run_classifier(dt, training, testing)
nb_acc.append(metrics.accuracy_score(nb_expect, nb_predict))
tree_acc.append(metrics.accuracy_score(dt_expect, dt_predict))
return nb_acc, tree_acc, training_fracs
nb_acc, tree_acc, training_fracs = simulate()
print(f"Naive Bayes accuracy @ 50% training: {nb_acc[0]}")
print(f"Decision Tree accuracy @ 50% training: {tree_acc[0]}")
> ---------------------------------------------------------------------------
> TypeError Traceback (most recent call last)
> <ipython-input-541-1b565a23fd50> in <module>
> 28 return nb_acc, tree_acc, training_fracs
> 29
> ---> 30 nb_acc, tree_acc, training_fracs = simulate()
> 31
> 32 print(f"Naive Bayes accuracy @ 50% training: {nb_acc[0]}")
>
> <ipython-input-541-1b565a23fd50> in simulate()
> 18 nb = naive_bayes.CategoricalNB()
> 19 dt = tree.DecisionTreeClassifier()
> ---> 20 training, testing = train_test_split(i)
> 21
> 22 nb_expect, nb_predict = run_classifier(nb, training, testing)
>
> ~\anaconda3\lib\site-packages\sklearn\model_selection\_split.py in train_test_split(test_size, train_size, random_state, shuffle,
> stratify, *arrays)
> 2170 raise ValueError("At least one array required as input")
> 2171
> -> 2172 arrays = indexable(*arrays)
> 2173
> 2174 n_samples = _num_samples(arrays[0])
>
> ~\anaconda3\lib\site-packages\sklearn\utils\validation.py in indexable(*iterables)
> 297 """
> 298 result = [_make_indexable(X) for X in iterables]
> --> 299 check_consistent_length(*result)
> 300 return result
> 301
>
> ~\anaconda3\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays)
> 257 """
> 258
> --> 259 lengths = [_num_samples(X) for X in arrays if X is not None]
> 260 uniques = np.unique(lengths)
> 261 if len(uniques) > 1:
>
> ~\anaconda3\lib\site-packages\sklearn\utils\validation.py in <listcomp>(.0)
> 257 """
> 258
> --> 259 lengths = [_num_samples(X) for X in arrays if X is not None]
> 260 uniques = np.unique(lengths)
> 261 if len(uniques) > 1:
>
> ~\anaconda3\lib\site-packages\sklearn\utils\validation.py in _num_samples(x)
> 200 if hasattr(x, 'shape') and x.shape is not None:
> 201 if len(x.shape) == 0:
> --> 202 raise TypeError("Singleton array %r cannot be considered"
> 203 " a valid collection." % x)
> 204 # Check that shape is returning an integer or default to len
>
> TypeError: Singleton array array(0.5) cannot be considered a valid collection.
解决方案
推荐阅读
- kubernetes - 使用 Kubernetes 调度构建
- mysql - 在 SQL 查询中使用 Promise?
- git - 是否可以通过 git&github 访问本地 pc 上某人的文件?
- java - Java Vector - 数组索引超出范围
- c# - 如何在 .Net 核心控制台应用程序中创建树视图和树节点
- azure-ad-b2c - 管理员能否获取 Azure B2C 中特定使用者的访问令牌?
- php - 创建响应时出现错误“SOAP-ERROR: Encoding: object has no 'error' property”
- ansible - Ansible:为“with_items”中包含的每个项目增加一个变量
- python - 如何在 Tkinter Python 中动态调整图像大小
- if-statement - 通过 Selenium IDE 测试状态OK