python - avoid duplicated column headers pandas dataframe creation
问题描述
Im a beginner trying to create dataframe that is storing some model perfomances(R², RMSE, training time, predic time...etc, below an example. but the result is a dataframe with repeated column headers. could you please help me to avoid this? the objective is to have all the df with one header only... The issue must come from the 'for loop' but I'm not sure how to fix it. Thanks
models = [LinearRegression(), Ridge(), Lasso(),ElasticNet()]
for model in models:
start = time.time()
model.fit(X_train, y_train)
stop = time.time()
start1 = time.time()
predictions = model.predict(X_train)
stop1 = time.time()
results={'Model':type(model).__name__, 'R²_score':r2_score(y_train, predictions),'RMSE':
mean_squared_error(y_train, predictions),'AB_Av_ERR':mean_absolute_error(y_train, predictions),'Training_time':stop-start,'Pred_time':stop1-
start1}
df_res = pd.DataFrame(results,index=[0])
print(df_res)
here's the output:
> Model R²_score RMSE AB_Av_ERR Training_time Pred_time 0
> LinearRegression 0.01 1736.28 21.28 0.86
> 0.07 Model R²_score RMSE AB_Av_ERR Training_time Pred_time 0 Ridge 0.01 1736.28 21.28 0.32 0.08 Model
> R²_score RMSE AB_Av_ERR Training_time Pred_time 0 Lasso
> 0.01 1740.02 21.26 0.99 0.08
> Model R²_score RMSE AB_Av_ERR Training_time Pred_time 0 ElasticNet 0.01 1740.14 21.28 0.89 0.08
解决方案
You can create an empty dataframe first outside the loop:
df = pd.DataFrame(columns=['Model', 'R²_score', 'RMSE', 'AB_Av_ERR', 'Training_time', 'Pred_time']
Then append values inside the loop like so:
df = df.append(results, ignore_index=True)
Try this:
models = [LinearRegression(), Ridge(), Lasso(),ElasticNet()]
df_res = pd.DataFrame(columns=['Model', 'R²_score', 'RMSE', 'AB_Av_ERR', 'Training_time', 'Pred_time']
for model in models:
start = time.time()
model.fit(X_train, y_train)
stop = time.time()
start1 = time.time()
predictions = model.predict(X_train)
stop1 = time.time()
results={'Model':type(model).__name__, 'R²_score':r2_score(y_train, predictions),'RMSE':
mean_squared_error(y_train, predictions),'AB_Av_ERR':mean_absolute_error(y_train, predictions),'Training_time':stop-start,'Pred_time':stop1-
start1}
df_res = df_res.append(results, ignore_index=True)
print(df_res)
推荐阅读
- node.js - 为什么 npm i -g 模块与 npm i -g --only=dev 不同。?
- python - 如何在 Flask 中从 JSON 中删除括号
- javascript - 使用 vh 或 % 显示和隐藏滚动内容?
- java - 如何找出 System.out.print() 的类型
- scala - Gatling 如何创建列表并将其传递给会话?
- ffmpeg - 是否可以在 SDP 上调整 ffmpeg 的超时?
- python-3.x - 我怎样才能将所有这些听写合二为一?
- ios - SceneKit 着色器修改器未修改几何体的位置
- python - 从大型 dfs2 文件中提取 dfs0
- linux - 如何修复无法为虚拟机 kali-linux-2020.3-vbox-amd34 打开会话