ValueError: at least one array or dtype is required

11,012

This error will occur cause of these reasons:

  1. You don't have target column in your csv. There for check twice your csv.
  2. If you have target column, there are white space or spaces in your targer column. It may be exist like these
< target>
<target >
< target >
<target   >...etc.

There for copy that column name with that white spaces. After that run this code

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

Updated:

If your data frame look like this

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

When you use 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

In your case you have used df_x = data.iloc[:, 1:]. Correct it as df_x = data.iloc[:, 1]. Understand how iloc works

Share:
11,012
TYS
Author by

TYS

Updated on June 04, 2022

Comments

  • TYS
    TYS almost 2 years

    My code:

    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)
    
    

    I am getting the error as shown in the title from .fit() method, and didn't understand much from the documentation as I am new to ML.

    Full error:

    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'
    

    Update -:
    I'v since removed and updated this as this was to test a solution found in the documentation. I have updated the error

    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
    

    Process finished with exit code 1