Seems like a simply thing to do, but yet I fail. I want to write the following arrays into one line in a csv file, alongside with other strings (i.e. "\n"
). The values should be separated by semicolons.
I create the arrays from lists
auth_list = ['5,2.6,5.23515389636e-11,0.00444005161574,0.0644299145707,0.04,0.0037037037037', '1,5.0,5.65187364062e-12,0.0,0.0605326876513,0.0,0.000740740740741']
aff_list = [(1, 0.003105590062111801), (1, 0.001838235294117647)]
comm_list = ['', '4,17.5,0.00584085856718,0.0002890919969,0.278790504782,0.0140752484561,0.0029973357016,0.00044404973357', '0,0.0,0.000218693005322,3.33471210416e-07,0.232075228649,0.0,0.000222024866785,0.0', '0,0.0,0.00025235732634,6.73003774237e-07,0.233652374653,0.0428571428571,0.000555062166963,0.0']
I proceed turning them into arrays
import numpy
auth_array = numpy.genfromtxt(auth_list, delimiter=",")
aff_array = numpy.array(aff_list) # Here I don't have to use numpy.genfromtext, don't know why
auth_array = numpy.genfromtxt(aff_list, delimiter=",")
These arrays already have no commas. I calculate the means using
auth_mean = numpy.mean(auth_array, axis=0)
aff_mean = numpy.mean(aff_array, axis=0)
comm_mean = numpy.mean(comm_array, axis=0)
title = "Happy New Year"
Using print
, I see in the terminal
auth_mean = [ 3.00000000e+00 3.80000000e+00 2.90017063e-11 2.22002581e-03
6.24813011e-02 2.00000000e-02 2.22222222e-03]
aff_mean = [ 1. 0.00247191]
comm_mean = [ 1.33333333e+00 5.83333333e+00 2.10396963e-03 9.66994906e-05
2.48172703e-01 1.89774638e-02 1.25814091e-03 1.48016578e-04]
The arrays always have the same dimension.
output_text = title + str(auth_mean).strip('[]') + ";" + str(com_mean).strip('[]') + ";" + str(aff_mean).strip('[]') + "\n"
output_file = open(output_file_name, 'w')
output_file.write(output_text)
output_file.close()
yields
Happy New Year;3.00000000e+00 3.80000000e+00 2.90017063e-11 2.22002581e-03
6.24813011e-02 2.00000000e-02 2.22222222e-03; 1.33333333e+00 5.83333333e+00 2.10396963e-03 9.66994906e-05
2.48172703e-01 1.89774638e-02 1.25814091e-03 1.48016578e-04; 1. 0.00247191
How can I make a simple straight line like
Happy New Year;3.00000000e+00;3.80000000e+00;2.90017063e-11;2.22002581e-03;6.24813011e-02;2.00000000e-02;2.22222222e-03;1.33333333e+00;5.83333333e+00;2.10396963e-03;9.66994906e-05;2.48172703e-01;1.89774638e-02;1.25814091e-03;1.48016578e-04;1.;0.00247191
numpy.mean(.. , axis=0)
from arrays that I create from lists usingnumpy.genfromtxt(.., delimiter=",")
The lists of course are comma-separated, but the arrays aren't. – MERose Jan 3 at 9:58numpy
tag to your question so that numpy experts will notice it. (I Am Not A Numpy Expert). – PM 2Ring Jan 3 at 10:00