Python 尝试获取回归摘要时出错
我试图进行山脊回归,就像这样:Python 尝试获取回归摘要时出错,python,scikit-learn,regression,Python,Scikit Learn,Regression,我试图进行山脊回归,就像这样: from sklearn.linear_model import LinearRegression, RidgeCV, Ridge from regressors import stats alphas = np.linspace(.00001, 100, 1000) rr_scaled = RidgeCV(alphas= alphas, cv=5, normalize=True) rr_scaled.fit(X_train, y_train) 它工作得很
from sklearn.linear_model import LinearRegression, RidgeCV, Ridge
from regressors import stats
alphas = np.linspace(.00001, 100, 1000)
rr_scaled = RidgeCV(alphas= alphas, cv=5, normalize=True)
rr_scaled.fit(X_train, y_train)
它工作得很好,所以我去获取摘要:
stats.summary(rr_scaled, X_train, y_train)
但我一直在犯这样的错误:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 1 and the array at index 1 has size 10
那是什么?语法有问题吗
我发现了这篇帖子:但它和我当时做的一模一样。并且在岗位上工作 问题似乎是希望您的数据处于特定的形状。特别是,它似乎期望您的目标变量是一个数组,而不是一个矩阵 考虑以下基于代码的示例:
import numpy as np
import pandas as pd
from regressors import stats
from sklearn.linear_model import LinearRegression, RidgeCV, Ridge
n_features = 3
n_samples = 10
X_train = np.random.normal(0, 1, size=(n_samples, n_features))
y_train = np.random.randn(n_samples)
alphas = np.linspace(.00001, 100, 1000)
rr_scaled = RidgeCV(alphas=alphas, cv=5, normalize=True)
rr_scaled.fit(X_train, y_train)
stats.summary(rr_scaled, X_train, y_train)
如果我运行它,它将执行良好并输出
Residuals:
Min 1Q Median 3Q Max
-2.5431 -0.8815 -0.0059 0.69 2.2218
Coefficients:
Estimate Std. Error t value p value
_intercept 0.213519 0.463767 0.4604 0.656149
x1 0.001617 0.761174 0.0021 0.998351
x2 0.006398 0.895701 0.0071 0.994457
x3 -0.003119 0.518982 -0.0060 0.995335
---
R-squared: 0.00267, Adjusted R-squared: -0.49599
F-statistic: 0.01 on 3 features
现在,如果我将目标更改为“矩阵”形状:
我得到了与您相同的错误:
Traceback (most recent call last):
File "a.py", line 16, in <module>
stats.summary(rr_scaled, X_train, y_train)
File "lib/python3.8/site-packages/regressors/stats.py", line 252, in summary
coef_df['Estimate'] = np.concatenate(
File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 1 and the array at index 1 has size 3
您可以展示一些数据示例吗?首先想到的是,您的训练数组的结构对于
stats.summary
无效。我也检查过了,它说明函数的第一个参数应该是线性模型
。我不确定它是否与岭回归模型相同,请注意它。@Tino好吧,那么我提到的帖子是错的?@griggy这是机密。。。
Traceback (most recent call last):
File "a.py", line 16, in <module>
stats.summary(rr_scaled, X_train, y_train)
File "lib/python3.8/site-packages/regressors/stats.py", line 252, in summary
coef_df['Estimate'] = np.concatenate(
File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 1 and the array at index 1 has size 3
y_train = y_train.reshape((-1,))