所以我有两个一维数组需要绘制,我还想绘制它们的线性回归模型以了解它们的趋势,所以我有以下代码:
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.reshape(1, -1) if it contains a single sample.
有人可以帮助我理解吗?
答案 0 :(得分:0)
如错误所述,您可以使用
来解决import numpy as np
rates=np.array(rates).reshape(-1, 1)
reg = LinearRegression()
reg.fit(rates, R)
reg.coef_
P.S。我猜想您只有一个功能作为回归模型的预测器。