Изменение размера numpy массивов для использования функции sklearn train_test_split? - PullRequest
0 голосов
/ 28 мая 2020

Я пытаюсь разделить свои данные с помощью test_train_split из sklearn. Мои данные состоят из numpy .ndarray для изображений и точек лица. Однако я понял, что они имеют разные формы с изображениями ((2811, 250, 250, 3) и точками лица (2811, 68, 2)). Я не уверен, как go изменить масштаб, чтобы они были одного размера. Какие-либо предложения?

reg= linear_model.LinearRegression()
x_train, x_test, y_train, y_test = train_test_split(img, points, test_size=0.2)
reg.fit(x_train,y_train)

Ниже я получил сообщение об ошибке

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-ae4bf08b45bb> in <module>()
----> 1 reg.fit(x_train,y_train)

2 frames
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/_base.py in fit(self, X, y, sample_weight)
    490         n_jobs_ = self.n_jobs
    491         X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 492                          y_numeric=True, multi_output=True)
    493 
    494         if sample_weight is not None:

/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    753                     ensure_min_features=ensure_min_features,
    754                     warn_on_dtype=warn_on_dtype,
--> 755                     estimator=estimator)
    756     if multi_output:
    757         y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,

/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    572         if not allow_nd and array.ndim >= 3:
    573             raise ValueError("Found array with dim %d. %s expected <= 2."
--> 574                              % (array.ndim, estimator_name))
    575 
    576         if force_all_finite:

ValueError: Found array with dim 4. Estimator expected <= 2.


1 Ответ

1 голос
/ 28 мая 2020

Вы можете изменить форму ваших элементов и надписей на <= 2D. </p>

N = img.shape[0]
img = np.reshape(img, (N, -1)) # flattens the image to a vector of appropriate dimension
points = np.reshape(points, (N, -1)) # flattens the target

x_train, x_test, y_train, y_test = train_test_split(img, points, test_size=0.2)
reg.fit(x_train,y_train)
...