【问题标题】:How to save result obtained via various fuctions into csv or excel file如何将通过各种功能获得的结果保存到 csv 或 excel 文件中
【发布时间】:2021-04-20 21:11:40
【问题描述】:

我正在通过 for 循环对机器学习算法进行超调优。 但我不知道如何将其保存到 csv 或 excel 文件中,因为它显示在终端输出中...帮帮我..在这里我分享代码以供参考。

def random_search():
options = create_opts()

# kernel
kernel_opts = np.array(["rbf",  "poly", "linear"])  # 3

# C
C_opts = np.array([0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000]) # 15
# C_linear_opts = np.array([0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100]) # 10

# epsilon
epsilon_opts = np.array([.01, .02, .03, .04, .05, .06, .07, .08, .09, 0.1])  # 10

# gamma
gamma_opts = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])  # 9
# n_iter = len(kernel_opts)*len(C_opts)*len(epsilon_opts)*len(gamma_opts)
for kernel in kernel_opts:
    for C in C_opts:
        for epsilon in epsilon_opts:
            for gamma in gamma_opts:
                run_svr(options.random_state, options.poly_degree, kernel=kernel, C=C, epsilon=epsilon, gamma=gamma)

【问题讨论】:

  • 您可以将标准输出重定向到文件,请参阅stackoverflow.com/questions/7152762/…
  • run_svr() 函数的结果是什么类型的输出?我的意思是 run_svr 函数的返回类型是什么?是清单吗?一个整数?
  • @ Soroosh Noorzad k-fold mean: [ 0.79958758 9.56594248 7.53763915 29.17127736] k-fold standard deviation: [0.02541773 0.54930184 0.53217536 2.34891345] Finished running SVR for compressive data with random_state=0, poly_degree=1, kernel =rbf, C=0.1, epsilon=0.04, gamma=0.2
  • 创建一个数据框然后使用 df.to_csv

标签: python machine-learning optimization


【解决方案1】:

作为示例,使用它。有点乱,希望对你有帮助。

import pandas as pd
import numpy as np

# Creating
csv_format = {'col_1': [], 'col_2': [], 'col_3': [], 'col_4': []}
_db = pd.DataFrame(csv_format)
_db = seeds_db.fillna(0)  # with 0s rather than NaNs
_db.to_csv("file.csv", index=False)

# Updating
_db = pd.read_csv("file.csv", delimiter=',')
new_rec = np.array([['val_1', 'val_2', 'val_3', 'val_4']])
_db = _db.append(pd.DataFrame(new_rec, columns=['col_1', 'col_2', 'col_3', 'col_4']))
_db.to_csv("file.csv", index=False)

【讨论】:

    猜你喜欢
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2021-11-21
    • 1970-01-01
    • 2019-03-25
    • 2019-06-19
    相关资源
    最近更新 更多