[英]Scikit-Learn (TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32'))
I created a small MLPRegressor code for the Iris dataset that I had downloaded as a csv.我为下载为 csv 的 Iris 数据集创建了一个小的 MLPRegressor 代码。 Although I can use the load_iris() module for my code, my uni wants me to do it the old fashioned way.虽然我可以在我的代码中使用 load_iris() 模块,但我的大学希望我用老式的方式来做。
from sklearn.neural_network import MLPRegressor
import pandas as pd
data = pd.read_csv("iris.csv", header=None)
# data = load_iris()
attributes = data.iloc[:,:-1].values.tolist()
outputs = data.iloc[:,-1].values.tolist()
clf = MLPRegressor(
# solver="sgd",
# activation="logistic",
# learning_rate_init=0.01,
# max_iter=10000,
# verbose=True,
hidden_layer_sizes=[4]
)
clf.fit(attributes, outputs)
print(clf.predict([[5.4,3.9,1.3,0.4],[6.5,3.2,5.1,2.0]]))
I get an error like this when I try to run:当我尝试运行时出现这样的错误:
Traceback (most recent call last):
File "prog4_scikit_learn.py", line 28, in <module>
clf.fit(attributes, outputs)
File "/usr/local/lib/python3.6/dist-packages/sklearn/neural_network/multilayer_perceptron.py", line 620, in fit
return self._fit(X, y, incremental=False)
File "/usr/local/lib/python3.6/dist-packages/sklearn/neural_network/multilayer_perceptron.py", line 378, in _fit
intercept_grads, layer_units, incremental)
File "/usr/local/lib/python3.6/dist-packages/sklearn/neural_network/multilayer_perceptron.py", line 516, in _fit_stochastic
coef_grads, intercept_grads)
File "/usr/local/lib/python3.6/dist-packages/sklearn/neural_network/multilayer_perceptron.py", line 230, in _backprop
loss = LOSS_FUNCTIONS[loss_func_name](y, activations[-1])
File "/usr/local/lib/python3.6/dist-packages/sklearn/neural_network/_base.py", line 194, in squared_loss
return ((y_true - y_pred) ** 2).mean() / 2
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
Although I created a code using MLPClassifier for the same dataset and it works like a charm虽然我使用 MLPClassifier 为相同的数据集创建了一个代码,但它的工作原理很吸引人
from sklearn.neural_network import MLPRegressor
import pandas as pd
data = pd.read_csv("iris.csv", header=None)
attributes = data.iloc[:,:-1].values.tolist()
outputs = data.iloc[:,-1].values.tolist()
clf = MLPClassifier(solver="sgd",
activation="logistic",
learning_rate_init=0.01,
max_iter=10000,
# verbose=True,
hidden_layer_sizes=[4])
clf.fit(attributes, outputs)
print(clf.predict([[5.4,3.9,1.3,0.4],[6.5,3.2,5.1,2.0]]))
Can someone help me out有人可以帮我吗
Check your y
.检查您的y
。 In this case: 'outputs'
在这种情况下: 'outputs'
In a typical 'iris.csv'
file, the last column denotes the species of the samples and will have values as strings在典型的'iris.csv'
文件中,最后一列表示样本的种类,并将值作为字符串
In the classifier, that is a valid input, so it works correctly.在分类器中,这是一个有效的输入,因此它可以正常工作。
But for a regressor, its not.但对于回归器,它不是。 Regression is for finding a continuous numerical value as target, which you dont have in this case.回归是为了找到一个连续的数值作为目标,在这种情况下你没有。 Agreed that the error message should be more clear, but hope that its clear to you now.同意错误消息应该更清楚,但希望您现在清楚。
There is no need to call .tolist()
on the arrays as they can be handled directly by the scikit-learn estimators, and in any case if you use tolist()
, they will be again converted to numpy arrays internally.不需要在数组上调用.tolist()
,因为它们可以由 scikit-learn 估计器直接处理,并且在任何情况下,如果您使用tolist()
,它们将再次在内部转换为 numpy 数组。
I think if you have not used tolist()
then the error might be different and easier to understand.我想如果你没有使用过tolist()
那么错误可能会有所不同并且更容易理解。
Now coming to load_iris()
.现在来到load_iris()
。 load_data().target
contains numerical values 0,1,2 as representation of the classes. load_data().target
包含数值 0,1,2 作为类的表示。 Since it is numbers, its working in regression as well as regression.由于它是数字,因此它在回归和回归中都起作用。 Hope this makes sense.希望这是有道理的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.