alphas = np.logspace(-10,10,20) coef = pd.DataFrame() for alpha in alphas: ridge_clf = Ridge(alpha=alpha) ridge_clf.fit(X_train[features_without_ones],y_train) df = pd.DataFrame([ridge_clf.coef_],columns=X_train[features_without_ones].columns) df['alpha']=alpha coef = pd.concat([coef, df], ignore_index=True) coef.round(decimals=2) alphas = np.logspace(-10,10,20) coef = pd.DataFrame() for alpha in alphas: ridge_clf = Ridge(alpha=alpha) ridge_clf.fit(X_train[features_without_ones],y_train) df = pd.DataFrame([ridge_clf.coef_],columns=X_train[features_without_ones].columns) df['alpha']=alpha coef = pd.concat([coef, df], ignore_index=True) coef.round(decimals=2)
时间: 2023-12-06 10:41:02 浏览: 132
DataFrame 数据合并实现(merge,join,concat)
5星 · 资源好评率100%
这段代码是在利用岭回归(Ridge Regression)来进行特征选择。其中,np.logspace(-10,10,20)是在生成20个对数尺度的alpha值,ridge_clf = Ridge(alpha=alpha)是定义岭回归模型,ridge_clf.fit(X_train[features_without_ones],y_train)是利用训练集进行模型训练,df = pd.DataFrame([ridge_clf.coef_],columns=X_train[features_without_ones].columns)是将岭回归模型的系数转换为数据框格式,df['alpha']=alpha是为每个alpha值添加一列,coef = pd.concat([coef, df], ignore_index=True)是将所有alpha值对应的系数数据框进行合并,最后用coef.round(decimals=2)将结果保留两位小数。
阅读全文