我试图创建一个使用线性回归的预测模型,数据集有157,673个条目。
数据(在csv文件中)采用以下格式:
Timestamp,Signal_1,Signal_2,Signal_3,Signal_4,Signal_5
2021-04-13 11:03:13+02:00,3,3,3,12,12
我现在的代码是:
filename = 'test.csv'
df = pd.read_csv(filename , parse_dates=['Timestamp'], header=0)
df['Timestamp'] = pd.to_numeric(pd.to_datetime(df['Timestamp']))
u, v, w, x, y, z = df.values.T
X = np.asarray([v, w, x, y, z])
Y = np.asarray([u, u, u, u, u])
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, shuffle= True)
lineReg = LinearRegression()
lineReg.fit(X_train, y_train)
print('Score: ', lineReg.score(X_test, y_test))
print('Weights: ', lineReg.coef_)
当打印出X和Y的形状时,它是(5, 157673)
(在Y数组中放置u
4次之后,否则会产生错误ValueError: Found input variables with inconsistent numbers of samples: [5, 1]
)。
但是,现在我遇到了错误MemoryError: Unable to allocate 185. GiB for an array with shape (157673, 157673) and data type float64
。
为什么会这样呢?在某个地方一定有一个错误,如果不是,为什么它突然变成了(157673, 157673)
而不是(6, 157673)
?
发布于 2021-05-20 09:05:25
快速修正就是改变数据格式--我看不出你的数据是什么样子的,所以我的建议在没有例子的情况下仍然停留在理论上
pd.read_csv
函数中使用dtypes
参数。或者在稍后使用astype
时转换它们。发布于 2022-02-19 18:57:15
您正在使用的科学工具包-学习的LinearRegression,优化与OLS (普通最小二乘)。OLS需要大量的内存。
您应该切换到scikit学习的SGDRegressor,它使用随机梯度下降(SGD)来优化。SGD使用的内存比OLS少得多。
https://datascience.stackexchange.com/questions/94556
复制相似问题