AttributeError:“MLPClassifier”对象没有属性“_label_binarizer”
Posted
技术标签:
【中文标题】AttributeError:“MLPClassifier”对象没有属性“_label_binarizer”【英文标题】:AttributeError: 'MLPClassifier' object has no attribute '_label_binarizer' 【发布时间】:2021-08-01 17:56:05 【问题描述】:我正在尝试使用 sklearn 的 MLPClassifier 利用 partial_fit() 函数来实现批量训练,但出现以下错误:
。
我已经咨询了一些与此相关的问题 (partial_fit Sklearn's MLPClassifier)。这是我用来重现错误的一段代码(来自所附参考):
from __future__ import division
import numpy as np
from sklearn.datasets import make_classification
from sklearn.neural_network import MLPClassifier
#Creating an imaginary dataset
input, output = make_classification(1000, 30, n_informative=10, n_classes=2)
input= input / input.max(axis=0)
N = input.shape[0]
train_input = input[0:500,:]
train_target = output[0:500]
test_input= input[500:N,:]
test_target = output[500:N]
#Creating and training the Neural Net
# 1. Disable verbose (verbose is annoying with partial_fit)
clf = MLPClassifier(activation='tanh', learning_rate='constant',
alpha=1e-4, hidden_layer_sizes=(15,), random_state=1, batch_size=1,verbose= False,
max_iter=1, warm_start=False)
# 2. Set what the classes are
clf.classes_ = [0,1]
for j in range(0,100):
for i in range(0,train_input.shape[0]):
input_inst = train_input[[i]]
target_inst= train_target[[i]]
clf=clf.partial_fit(input_inst,target_inst)
# 3. Monitor progress
print("Score on training set: %0.8f" % clf.score(train_input, train_target))
#Testing the Neural Net
y_pred = clf.predict(test_input)
print(y_pred)
# 4. Compute score on testing set
print(clf.score(test_input, test_target))
我还修改了第 895 行的 multilayer_perceptron.py
代码以替换它,如 here 所述:
self.label_binarizer_.fit(y)
有了这个:
if not incremental:
self.label_binarizer_.fit(y)
else:
self.label_binarizer_.fit(self.classes_)
而且还是不行。非常感谢任何帮助。
谢谢!
【问题讨论】:
【参考方案1】:这可行:
from __future__ import division
import numpy as np
from sklearn.datasets import make_classification
from sklearn.neural_network import MLPClassifier
#Creating an imaginary dataset
input, output = make_classification(1000, 30, n_informative=10, n_classes=2)
input= input / input.max(axis=0)
N = input.shape[0]
train_input = input[0:500,:]
train_target = output[0:500]
test_input= input[500:N,:]
test_target = output[500:N]
#Creating and training the Neural Net
# 1. Disable verbose (verbose is annoying with partial_fit)
clf = MLPClassifier(activation='tanh', learning_rate='constant',
alpha=1e-4, hidden_layer_sizes=(15,), random_state=1, batch_size=1,verbose= False,
max_iter=1, warm_start=False)
for j in range(0,100):
for i in range(0,train_input.shape[0]):
input_inst = train_input[[i]]
target_inst= train_target[[i]]
clf.partial_fit(input_inst,target_inst,[0,1])
# 3. Monitor progress
print("Score on training set: %0.8f" % clf.score(train_input, train_target))
#Testing the Neural Net
y_pred = clf.predict(test_input)
print(y_pred)
# 4. Compute score on testing set
print(clf.score(test_input, test_target))
此行导致错误:
# 2. Set what the classes are
clf.classes_ = [0,1]
而且你必须在这里通过课程:
clf.partial_fit(input_inst,target_inst,[0,1])
【讨论】:
以上是关于AttributeError:“MLPClassifier”对象没有属性“_label_binarizer”的主要内容,如果未能解决你的问题,请参考以下文章
AttributeError: 'RDD' 对象没有属性 'show'
AttributeError:“NumpyArrayIterator”对象没有属性“类”
AttributeError:模块 'dbus' 没有属性 'lowlevel'