热线电话:13121318867

登录
2019-02-26 阅读量: 893
我的线性回归模型不适合2D阵列

所以我需要绘制两个一维数组,我想绘制线性回归模型以了解它们的趋势,所以我有以下代码:

from brian2 import *

%matplotlib inline

from sklearn.linear_model import LinearRegression

start_scope()

reg = LinearRegression()

reg.fit(rates, R)

reg.coef_

#rates and R were issued beforehand

figure(figsize=(36,12))

subplot(121)

plot(rates,R,'o','b',reg,'r' )

xlabel('Time (ms)')

ylabel('V (mV)')

title("M.I. - firing rate evolution")

ylim((0,35))

然后我得到这个错误:

ValueError Traceback (most recent call last)

<ipython-input-40-a93af061bea5> in <module>

1 reg = LinearRegression()

----> 2 reg.fit(rates, R)

3 reg.coef_

4

5 figure(figsize=(36,12))

~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/base.py in fit(self, X, y, sample_weight)

456 n_jobs_ = self.n_jobs

457 X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],

--> 458 y_numeric=True, multi_output=True)

459

460 if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:

~/anaconda3/lib/python3.6/site-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)

745 ensure_min_features=ensure_min_features,

746 warn_on_dtype=warn_on_dtype,

--> 747 estimator=estimator)

748 if multi_output:

749 y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,

~/anaconda3/lib/python3.6/site-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)

545 "Reshape your data either using array.reshape(-1, 1) if "

546 "your data has a single feature or array.reshape(1, -1) "

--> 547 "if it contains a single sample.".format(array))

548

549 # in the future np.flexible dtypes will be handled like object dtypes

ValueError: Expected 2D array, got 1D array instead:

array=[17.51666667 17.11666667 16.06666667 14.53333333 12.75 9.98333333].

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.resha

pe(1, -1) if it contains a single sample.

解决办法:如错误中所述,可以使用它来解决它

import numpy as np

rates=np.array(rates).reshape(-1, 1)

reg = LinearRegression()

reg.fit(rates, R)

reg.coef_

PS我猜你只有一个特征作为回归模型的预测器。

0.0000
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子