调整numpy数组大小以使用sklearn的train_test_split函数?

我正在尝试使用sklearn中的test_train_split来分割我的数据。我的数据包括用于图像和面部点的numpy.ndarray。然而,我发现它们具有不同的形状,图像的形状是((2811, 250, 250, 3)),面部点的形状是(2811, 68, 2)。我不知道如何重新调整它们的大小,使它们具有相同的大小。有什么建议吗?

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.

回答:

你可以将你的特征和标签重塑为二维或更低维度。

N = img.shape[0]img = np.reshape(img, (N, -1)) # 将图像展平为适当维度的向量points = np.reshape(points, (N, -1)) # 将目标展平x_train, x_test, y_train, y_test = train_test_split(img, points, test_size=0.2)reg.fit(x_train,y_train)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注