使用train_test_split()返回的值列表作为训练数据

我正在尝试对一些数据进行线性回归。这就是数据的外观。

X = df['vectors'] 看起来像这样:

0      [-1.86135, 1.3202, 0.023501, -2.9511, 1.62135,...1      [0.5487195, 0.27389452, 0.49712706, 0.6853927,...2      [-1.3525691, -0.8444542, 2.8269022, -1.4456564...3      [1.0730275, -0.14970247, -1.1424525, -1.953272...4      [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...

当我在其上运行线性回归模型时:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=101)lm = LinearRegression()lm.fit(X_train, y_train)

我得到了这个错误:

TypeError                                 Traceback (most recent call last)TypeError: only size-1 arrays can be converted to Python scalarsThe above exception was the direct cause of the following exception:

我如何将X中的值转换为标量?我在考虑获取向量的平均值,但不确定具体该怎么做。


回答:

从外观上看,X 是一个 pandas.Series 对象。

由于X中每一行的列表长度都相同,你可以将X重塑为一个ndarray,其行数与X相同,每个列表中的元素数量作为列数。

# 导入numpyimport numpy as np# 重塑X = np.array(X.explode()).reshape(len(X), -1)# 像之前一样进行X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=101)lm = LinearRegression()lm.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中创建了一个多类分类项目。该项目可以对…

发表回复

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