ValueError:至少需要一个数组或dtype

时间:2019-12-19 05:13:40

标签: python scikit-learn neural-network artificial-intelligence

我的代码:

import numpy as np
from pandas import read_csv
from matplotlib import pyplot as plt
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split

data = read_csv('data.csv', usecols=['col_1'])

df_x = data.iloc[:, 1:]
df_y = data.iloc[:, 0]

x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size=0.9, random_state=4)

nn = MLPClassifier(activation='logistic', solver='sgd', hidden_layer_sizes=(2,), random_state=1)
#nn.fit(x_train[x], y_train[x])

print(nn)

nn.fit(x_train, y_test)

pred = nn.predict(x_test)

我从.fit()方法的标题中看到了错误,并且由于我是ML新手,所以从文档中了解的不多。

完整错误:

File "C:/NNC/Main.py", line 14, in <module>
    data.target.array([])
  File "C:\NNC\venv\lib\site-packages\pandas\core\generic.py", line 5179, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'target'

更新-:
此后,我删除并更新了此内容,因为这是为了测试文档中找到的解决方案。我已更新错误

File "C:\Users\PycharmProjects\NNC\venv\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py", line 325, in _fit
    X, y = self._validate_input(X, y, incremental)
  File "C:\Users\PycharmProjects\NNC\venv\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py", line 932, in _validate_input
    multi_output=True)
  File "C:\Users\PycharmProjects\NNC\venv\lib\site-packages\sklearn\utils\validation.py", line 739, in check_X_y
    estimator=estimator)
  File "C:\Users\PycharmProjects\NNC\venv\lib\site-packages\sklearn\utils\validation.py", line 459, in check_array
    dtype_orig = np.result_type(*array.dtypes)
  File "<__array_function__ internals>", line 6, in result_type
ValueError: at least one array or dtype is required

以退出代码1完成的过程

1 个答案:

答案 0 :(得分:1)

由于这些原因,将发生此错误。

  1. 您的csv中没有target列。在那里检查两次您的csv。
  2. 如果您有target列,则targer列中有空格。 可能像这样
< target>
<target >
< target >
<target   >...etc.

使用该空格复制该列名称。之后,运行此代码

data = read_csv('data.csv', usecols=['col_1'])
data.columns = data.columns.str.strip()

已更新-:

如果您的数据框看起来像这样

       a         b
0      1         2
1      1         2
2      1         2
3      1         2
4      1         2

使用iloc时

df_y = data.iloc[:, 0]

output -:
       a         
0      1         
1      1         
2      1         
3      1         
4      1     
df_y = data.iloc[:, 1]   

output -:
       b
0      2
1      2
2      2
3      2
4      2

在您的情况下,您使用了df_x = data.iloc[:, 1:]。将其更正为df_x = data.iloc[:, 1]。了解iloc的工作原理