ValueError: Найдены входные переменные с непоследовательным количеством выборок: [249957, 8248581] - PullRequest
0 голосов
/ 19 января 2020

Я пытаюсь подобрать модель для тренировки и получить представление о том, как эти данные о физических нагрузках, полученные с датчиков на теле, влияют на колебания сердечного ритма

, но не могут продолжаться с этого момента поскольку я не понимаю, в чем причина,

Я также изменил значения x и y, чтобы они имели 2D-форму

import

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# generate random data-set
np.random.seed(0)
x = subject1["heart_rate"]
y = df

x= x.values.reshape(-1, 1)
y= y.values.reshape(-1, 1)

#x.shape[0] != y.shape[0]
# sckit-learn implementation

# Model initialization
regression_model = LinearRegression()
# Fit the data(train the model)
regression_model.fit(x, y)
# Predict
y_predicted = regression_model.predict(x)

# model evaluation
rmse = mean_squared_error(y, y_predicted)
r2 = r2_score(y, y_predicted)

# printing values
print('Slope:' ,regression_model.coef_)
print('Intercept:', regression_model.intercept_)
print('Root mean squared error: ', rmse)
print('R2 score: ', r2)

# plotting values

# data points
plt.scatter(x, y, s=10)
plt.xlabel('x')
plt.ylabel('y')

# predicted values
plt.plot(x, y_predicted, color='r')
plt.show()

df:

timestamp   activity_ID IMU_hand_temp   hand_acceleration_16_1  hand_acceleration_16_2  hand_acceleration_16_3  hand_gyroscope_rad_7    hand_gyroscope_rad_8    hand_gyroscope_rad_9    hand_magnetometer_μT_10 ... ankle_acceleration_16_1 ankle_acceleration_16_2 ankle_acceleration_16_3 ankle_gyroscope_rad_7   ankle_gyroscope_rad_8   ankle_gyroscope_rad_9   ankle_magnetometer_μT_10    ankle_magnetometer_μT_11    ankle_magnetometer_μT_12    Intensity
2928    37.66   0   30.3750 2.21530 8.27915 5.58753 -0.004750   0.037579    -0.011145   8.93200 ... 9.73855 -1.84761    0.095156    0.002908    -0.027714   0.001752    -61.1081    -36.8636    -58.3696    0
2929    37.67   0   30.3750 2.29196 7.67288 5.74467 -0.171710   0.025479    -0.009538   9.58300 ... 9.69762 -1.88438    -0.020804   0.020882    0.000945    0.006007    -60.8916    -36.3197    -58.3656    0
2930    37.68   0   30.3750 2.29090 7.14240 5.82342 -0.238241   0.011214    0.000831    9.05516 ... 9.69633 -1.92203    -0.059173   -0.035392   -0.052422   -0.004882   -60.3407    -35.7842    -58.6119    0
2931    37.69   0   30.3750 2.21800 7.14365 5.89930 -0.192912   0.019053    0.013374    9.92698 ... 9.66370 -1.84714    0.094385    -0.032514   -0.018844   0.026950    -60.7646    -37.1028    -57.8799    0
2932    37.70   0   30.3750 2.30106 7.25857 6.09259 -0.069961   -0.018328   0.004582    9.15626 ... 9.77578 -1.88582    0.095775    0.001351    -0.048878   -0.006328   -60.2040    -37.1225    -57.8847    0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
361795  3626.33 0   30.1875 2.51550 7.02650 5.78869 -0.469234   -0.065244   0.194218    -46.60210   ... 9.84361 1.84234 -0.852816   -0.018273   0.000864    0.018507    -56.3324    -29.6397    34.1311 0
361796  3626.34 0   30.1875 2.50643 6.30465 5.67552 -0.512432   -0.116526   0.151890    -45.83020   ... 9.76526 1.80581 -0.969161   -0.037873   -0.022418   -0.017999   -55.7786    -29.1093    34.2560 0
361797  3626.35 0   30.1875 2.54102 5.84908 5.67758 -0.458393   -0.041476   0.139977    -47.17750   ... 9.91807 1.76593 -0.851578   0.059283    -0.050002   0.005630    -55.5371    -29.5769    35.6106 0
361798  3626.36 0   30.1875 2.65866 5.88715 5.79468 -0.418556   -0.001280   0.116727    -46.84460   ... 10.06840    1.87889 -0.850738   0.049326    -0.016209   0.016162    -56.4328    -30.1943    34.6211 0
361799  3626.37 0   30.1875 2.51044 6.11629 5.83017 -0.366492   0.004535    0.052418    -46.51040   ... 9.94878 1.95778 -1.122580   0.051950    -0.011002   -0.017655   -56.3426    -29.1916    33.6409 0
249957 rows × 33 columns

Ошибка:

 ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-127-74da3aeee03e> in <module>()
     19 regression_model = LinearRegression()
     20 # Fit the data(train the model)
---> 21 regression_model.fit(x, y)
     22 # Predict
     23 y_predicted = regression_model.predict(x)

c:\python36\lib\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:

c:\python36\lib\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)
    764         y = y.astype(np.float64)
    765 
--> 766     check_consistent_length(X, y)
    767 
    768     return X, y

c:\python36\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays)
    233     if len(uniques) > 1:
    234         raise ValueError("Found input variables with inconsistent numbers of"
--> 235                          " samples: %r" % [int(l) for l in lengths])
    236 
    237 

ValueError: Found input variables with inconsistent numbers of samples: [249957, 8248581]

1 Ответ

0 голосов
/ 19 января 2020
  1. Не используйте изменение формы.
  2. Измените x на df и y на субъект1 [«heart_rate»].
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...