Search code examples
python-3.xnumpyscikit-learngrid-search

AttributeError: 'numpy.ndarray' object has no attribute 'target


I get the error an "AttributeError: 'numpy.ndarray' object has no attribute 'target'" when executing the grid.fit() command. I am not sure what this means and how to fix it. Can anybody advise?

#Grid Search Parameter Tuning
import numpy as np
from sklearn import datasets
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV

# datasets
dataset = np.random.rand(1000,2)

#Ridge regression 
alphas = np.array([1,0.1,0.01,0.001,0.0001,0])
model = Ridge()
grid = GridSearchCV(estimator=model, param_grid=dict(alpha=alphas))
grid.fit(dataset.data, dataset.target)

Solution

  • You have given created random array as your dataset, you are passing that data set in fit but that data does not have target, you need to define another array which define labels.

    x = np.random.rand(1000,2)
    y3 = np.concatenate((np.zeros(500),np.ones(500)))
    random = np.random.permutation(1000)
    y = y3[random]