How to match the stored name of np.savetxt to the number in the programming.

Asked 3 months ago, Updated 3 months ago, 5 views

In the programming below (sorry for the scribbling)
n1 = 1

when np.savetxt("data-1(value of n1).csv", data_del, delimiter=",")

n1 = 2

when np.savetxt("data-2.csv", data_del, delimiter=",")

...
I've repeatedly manually re-run it to n1=1-10, but I want to automate it.

Is there a way to make it possible?

import numpy as np
import pandas aspd
from pandas import Series, DataFrame
import matplotlib.pyplot asplt
%matplotlib inline

data_path = 'data.csv'


n0 = 64 
n1 = 1 ★★ Change Required 1-10 ★★
n2 = 1 
n3 = 14                                                                

# Loading Data
data_all=pd.read_csv(data_path, delimiter=",", skiprows=n1)
data_all = np.array(data_all)



sows1 = 2304*n1
data_all2 = np.zeros ([16*n2,n3])
for jin range (n2):
    for n in range (n3):
        for i in range (16):
            data_all2[i+16*j,n] = data_all[srows1*j+128*i:srows1*j+64+128*i,n].mean()


data_del=data_all2
for i in range (n2):
    data_del=np.delete(data_del, 14+15*i, 0)
for i in range (n2):
    data_del=np.delete(data_del, 12+14*i, 0)
for i in range (n2):
    data_del=np.delete(data_del, 4+13*i, 0)
for i in range (n2):
    data_del=np.delete(data_del, 1+12*i, 0)
    
    
# Save Data
data_del_t = data_del.T
np.savetxt("data-1.csv", data_del, delimiter=",")#1 corresponds to n and 1-10

python

2022-09-30 14:07

1 Answers

There are several ways to assemble a string containing variable values, so you can use whatever you want.

  • @payaneco's comment "data-{}.csv".format(n1)
  • Simple concatenation"data-"+str(n1)+".csv"
  • f stringf"data-{n1}.csv"

There are many other ways.

At the same time, if you've really rewritten n1 and the filename up to 1-10, you can put them in a for loop or function them all in one run.

The idea of a simple for loop is as follows:
range(1,11) specifies a range of numbers to loop in variables starting with 1 and up to one before 11 (=10).

for n1 in range (1,11):
    ## Describe repeat processing
    print(f"data-{n1}.csv")## For example:

np.savetxt with variables specified in the filename should also be included in the repeat process, so the following is true:

import numpy as np
import pandas aspd
from pandas import Series, DataFrame
import matplotlib.pyplot asplt
%matplotlib inline

data_path = 'data.csv'

n0 = 64 
# n1 = 1## Not required here ★★ Change Required 1-10 ★★
n2 = 1
n3 = 14

for n1 in range (1,11): Loop by changing the number of n1s to 1-10 in the process of ## for
    # Loading Data
    data_all=pd.read_csv(data_path, delimiter=",", skiprows=n1)
    data_all = np.array(data_all)

    sows1 = 2304*n1
    data_all2 = np.zeros ([16*n2,n3])
    for jin range (n2):
        for n in range (n3):
            for i in range (16):
                data_all2[i+16*j,n] = data_all[srows1*j+128*i:srows1*j+64+128*i,n].mean()

    data_del=data_all2
    for i in range (n2):
        data_del=np.delete(data_del, 14+15*i, 0)
    for i in range (n2):
        data_del=np.delete(data_del, 12+14*i, 0)
    for i in range (n2):
        data_del=np.delete(data_del, 4+13*i, 0)
    for i in range (n2):
        data_del=np.delete(data_del, 1+12*i, 0)

    # Save Data
    data_del_t = data_del.T
    np.savetxt(f"data-{n1}.csv", data_del, delimiter=",")##f string to incorporate variables #1 corresponds to n and 1-10


2022-09-30 14:07

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.