我正试图从一组数据中获得几个线性回归的系数。 我不知道为什么收到消息:“找到的数组具有0个样本(形状=(0,1)),而最少需要1个。” 我查了几个论坛,但仍然找不到解决我问题的方法...
代码: **
from sklearn.linear_model import LinearRegression
def regression_lineaire_cot_cr(df_entreprises):
lm_dict = {}
for industry in df_entreprises.keys():
df_entreprise = df_entreprises[industry]
df_cot = df_entreprise[(df_entreprise.PolluantNom == "Carbone organique total (COT)")| df_entreprise.RejFinal.notnull()]
df_cr = df_entreprise[(df_entreprise.PolluantNom == "Chrome et ses composés (Cr)")| df_entreprise.RejFinal.notnull()]
feature_cols = ['Annee']
X_cr = df_cr[feature_cols]
y_cr = df_cr.MasseEmiseRetenue
lm_cr = LinearRegression()
lm_cr.fit(X_cr, y_cr)
X_cot = df_cot[feature_cols]
y_cot = df_cot.MasseEmiseRetenue
lm_cot = LinearRegression()
lm_cot.fit(X_cot, y_cot)
lm_dict[industry] = {"cr": lm_cr, "cot": lm_cot}
return lm_dict
**
当我尝试这个时: 结果= gression_lineaire_cot_cr(df_entreprises)
这是错误消息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-179-d4efca86f9d9> in <module>
----> 1 results = regression_lineaire_cot_cr(df_entreprises)
<ipython-input-174-fc3bdd2132a2> in regression_lineaire_cot_cr(df_entreprises)
16
17 lm_cr = LinearRegression()
---> 18 lm_cr.fit(X_cr, y_cr)
19
20
~\Anaconda3\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:
~\Anaconda3\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)
754 ensure_min_features=ensure_min_features,
755 warn_on_dtype=warn_on_dtype,
--> 756 estimator=estimator)
757 if multi_output:
758 y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,
~\Anaconda3\lib\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)
580 " minimum of %d is required%s."
581 % (n_samples, shape_repr, ensure_min_samples,
--> 582 context))
583
584 if ensure_min_features > 0 and array.ndim == 2:
ValueError: Found array with 0 sample(s) (shape=(0, 1)) while a minimum of 1 is required.