моя модель линейной регрессии не подходит для нужд двумерного массива - PullRequest
0 голосов
/ 15 января 2019

Итак, у меня есть два 1D-массива, которые мне нужно построить, и я также хочу построить их линейную регрессионную модель, чтобы узнать их тренд, поэтому у меня есть следующий код:

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.

Может кто-нибудь помочь мне понять?

1 Ответ

0 голосов
/ 19 января 2019

Как упоминалось в ошибке, вы решаете ее с помощью

import numpy as np
rates=np.array(rates).reshape(-1, 1) 
reg = LinearRegression()
reg.fit(rates, R)
reg.coef_

P.S. Я предполагаю, что у вас есть только одна особенность в качестве предиктора для регрессионной модели.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...