0

I was hyper-tuning the machine learning algorithms via for loop. but I do not know how to keep it saving into csv or excel file as it is showing in terminal output...Help me..Here I am sharing the code for reference.

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)
  • you could redirect your standard output to a file, see https://stackoverflow.com/questions/7152762/how-to-redirect-print-output-to-a-file – SpaceBurger Apr 20 '21 at 12:48
  • What sort of output is the result of the run_svr() function? I mean what is the return type of the run_svr function? Is it a list? An int? – Soroosh Noorzad Apr 20 '21 at 12:55
  • @ 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 – Rupesh Singh Apr 20 '21 at 13:12
  • create a dataframe then use df.to_csv – Golden Lion Apr 20 '21 at 14:48

1 Answers1

0

As a sample, use this. It's a little messed up, but I hope it helps you.

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)
Soroosh Noorzad
  • 503
  • 1
  • 6
  • 18