首页 > 解决方案 > 我正在尝试加载和拆分我的数据,但是我得到 TypeError:'只有整数标量数组可以转换为标量索引'

问题描述

进口

import numpy as np
from sklearn.linear_model import LogisticRegression # Architecture model
from sklearn.datasets import _olivetti_faces # Dataset
from sklearn.model_selection import train_test_split

### Loading and splitting the data
data = np.load("/content/Face_Recognition/olivetti_faces.npy")
Xtrain, Xtest, Ytrain, Ytest = train_test_split(data.data, test_size=0.3, random_state=0)
#Error to show if x and y rows mismatch
print(X.shape)
print(y.shape)
if X.shape[0] != y.shape[0]:

  print("X and y rows are mismatched, check dataset again")

标签: pythonscikit-learnjupyter-notebook

解决方案


似乎您正在使用数据集的过时版本。您可以在此处看到更新的链接。您拥有的代码将不起作用,因为您要求拆分数据和目标,而您只提供目标。

像这样的东西应该可以工作(在 sklearn 0.24.1 上):

from sklearn.datasets import fetch_olivetti_faces

data = fetch_olivetti_faces()
Xtrain, Xtest, Ytrain, Ytest = train_test_split(data.data, data.target, test_size=0.3, random_state=0)

推荐阅读