pythonmachine-learningscikit-learnclassificationknn

AttributeError: 'Flags' object has no attribute 'c_contiguous'


I am following Hands On Machine Learning Book by Aurélien Géron and running in to the following error.

Code:

y_train_large = (y_train.astype("int") >= 7)
y_train_odd = (y_train.astype("int") % 2 == 1)
y_multilabel = np.c_[y_train_large, y_train_odd]

#model
knn_clf = KNeighborsClassifier()
knn_clf.fit(X_train, y_multilabel)

y_train_knn_pred = cross_val_predict(knn_clf, X_train, y_multilabel, cv=3)

The last line produces the following error:

{
AttributeError: 'Flags' object has no attribute 'c_contiguous'"
}

Since I am following the book, I expected this code to work. I have tried solutions from Google Bard and Claude AI chatbots but with no success.


Solution

  • There seems to be a bug report for this in Scikit-learn 1.3.0 (although it seems to have been fixed in the nightly builds). Try downgrading to version 1.2.2:

    pip uninstall scikit-learn
    pip install scikit-learn==1.2.2