Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x LabelEncoder实例尚未安装_Python 3.x_Tensorflow_Keras_Nlp - Fatal编程技术网

Python 3.x LabelEncoder实例尚未安装

Python 3.x LabelEncoder实例尚未安装,python-3.x,tensorflow,keras,nlp,Python 3.x,Tensorflow,Keras,Nlp,我有一个代码,用于预测句子分类任务中看不见的数据 代码是 from sklearn.preprocessing import LabelEncoder maxlen = 1152 ### PREDICT NEW UNSEEN DATA ### tokenizer = Tokenizer() label_enc = LabelEncoder() X_test = ['this is boring', 'wow i like this you did a great job'] X_test

我有一个代码,用于预测句子分类任务中看不见的数据

代码是

from sklearn.preprocessing import LabelEncoder

maxlen = 1152

### PREDICT NEW UNSEEN DATA ###
tokenizer = Tokenizer()
label_enc = LabelEncoder()
X_test = ['this is boring', 'wow i like this you did a great job']

X_test = tokenizer.texts_to_sequences(X_test)
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)

a = (model.predict(X_test)>0.5).astype(int).ravel()
print(a)

reverse_pred = label_enc.inverse_transform(a.ravel())
print(reverse_pred)
但我得到了这个错误

[1 1]
---------------------------------------------------------------------------
NotFittedError                            Traceback (most recent call last)
<ipython-input-33-7e12dbe8aec1> in <module>()
     39 print(a)
     40 
---> 41 reverse_pred = label_enc.inverse_transform(a.ravel())
     42 print(reverse_pred)

1 frames
/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
    965 
    966     if not attrs:
--> 967         raise NotFittedError(msg % {'name': type(estimator).__name__})
    968 
    969 

NotFittedError: This LabelEncoder instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
[1]
---------------------------------------------------------------------------
NotFittedError回溯(最近一次呼叫最后一次)
在()
39印刷品(a)
40
--->41反向pred=标签加密反向变换(a.ravel())
42打印(反向打印)
1帧
/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_已安装(估计器、属性、消息、所有或任何)
965
966如果不是属性:
-->967 raise NOTFITTEDEError(msg%{'name':类型(估计器)。\uuuuuu name\uuuuu})
968
969
NotFitteError:此LabelEncoder实例尚未安装。在使用此估计器之前,使用适当的参数调用“fit”。
我使用了顺序模型,在培训部分,
model.fit
被写为
history=model.fit()
。为什么会出现此错误?

在sklearn和报告之后,您只需在进行逆变换之前安装编码器即可

y = ['positive','negative','positive','negative','positive','negative']
label_enc = LabelEncoder()
label_enc.fit(y)

model_predictions = np.random.uniform(0,1, 3)
model_predictions = (model_predictions>0.5).astype(int).ravel()
model_predictions = label_enc.inverse_transform(model_predictions)

你必须给编码器配上一些标签,否则你可以应用反向转换。你能提供一段代码让我更好地理解它吗?只是疑问,
y
会包含我在培训部分使用的所有标签吗。比如,如果我的培训部分有1000个句子和对应的1000个句子的目标标签,那么我应该将所有1000个标签都放在
y
中吗?您可以传递所有1000个,或者只需为每种类型传递一个标签。我建议你全部通过,剩下的由标签编码器完成