Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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 使用Scikit学习按顺序编码数据时出错_Python_Numpy_Machine Learning_Keras_Scikit Learn - Fatal编程技术网

Python 使用Scikit学习按顺序编码数据时出错

Python 使用Scikit学习按顺序编码数据时出错,python,numpy,machine-learning,keras,scikit-learn,Python,Numpy,Machine Learning,Keras,Scikit Learn,我在数据帧的各行中循环,并尝试使用编码器对每一行数据进行编码 for index, row in self.data.iterrows(): data = self._encoder.transform(row) try: print(row.shape) results["classes"].append((self._model.predict(data) > 0.5).astype("int32"

我在数据帧的各行中循环,并尝试使用编码器对每一行数据进行编码

for index, row in self.data.iterrows():
    data = self._encoder.transform(row)
    try:
        print(row.shape)
        results["classes"].append((self._model.predict(data) > 0.5).astype("int32"))
        results["probability"].append((self._model.predict(data)))
        results["rows"].append(index)
    except Exception as e:
        print(e)
        results["rows"].append(index)
        results["classes"].append("ERROR")
        results["probability"].append("ERROR")
然后用我的模型预测。编码器和模型均使用Scikit learn和Keras制作,模型使用Keras的内置保存功能保存,编码器导出到joblib文件。如果我对整个数据帧进行编码,一切都会按预期进行

我正试图按顺序执行此操作,以避免编码器在数据出错时可能导致程序崩溃,特别是当一个新值出现在一个列中时,我是一个热编码,编码器以前从未见过这个值

我尝试过使用
iterrows()
,当我尝试对每一行进行编码时,会出现以下错误。
索引器错误:元组索引超出范围

我还尝试将每一行转换为它自己的数据帧,但当我尝试编码
ValueError时,我得到了以下结果:输入的特征数量必须等于或大于安装的变压器的特征数量。变压器n_特性为67,输入n_特性为1。

在我的数据中循环并按顺序编码和预测每一行数据的最佳方法是什么

第二个错误的完整跟踪

Traceback (most recent call last):
  File "/home/build/x-predictive-model/main.py", line 18, in <module>
    network.predictSequentially()
  File "/home/build/x-predictive-model/myai.py", line 191, in predictSequentially
    encoded = self._encoded_data = self._encoder.transform(pd.DataFrame(row))
  File "/home/user1/anaconda3/envs/x-model-lib/lib/python3.7/site-packages/sklearn/compose/_column_transformer.py", line 571, in transform
    .format(self._n_features, X.shape[1]))
ValueError: Number of features of the input must be equal to or greater than that of the fitted transformer. Transformer n_features is 67 and input n_features is 1.
回溯(最近一次呼叫最后一次):
文件“/home/build/x-predictive-model/main.py”,第18行,在
network.predicts()
文件“/home/build/x-predictive-model/myai.py”,第191行,按顺序排列
encoded=self.\u encoded\u data=self.\u encoder.transform(pd.DataFrame(行))
文件“/home/user1/anaconda3/envs/x-model-lib/lib/python3.7/site packages/sklearn/compose/_column_transformer.py”,第571行,在transform中
.format(self.\n\u特征,X.shape[1]))
ValueError:输入的特征数量必须等于或大于安装的变压器的特征数量。变压器n_特性为67,输入n_特性为1。

可以使用索引,使用语法
self.data[index:index+1]
循环并预测数据。

请使用反引号或缩进设置代码格式,但不能同时使用这两种格式(已编辑)。你能添加数据的形状和第二个错误的完整回溯吗?@ML_Engine数据的形状是(4722,67),我已在帖子正文中发布了完整的回溯