'Mean function output not separated by commas

That's my code:

array = np.array([[0,1,2],[3,4,5],[6,7,8]])
axis1_mean = np.mean(array,axis = 0)
print(axis1_mean)

That's the output:

[3. 4. 5.]

I need the output separated by commas, is there an easy way to do so? I found in the documentation of Numpy that the output should be separated by commas. Does anyone see what I'm missing?



Solution 1:[1]

You need to separate the CONTENT of your data from the REPRESENTATION of your data. Your data is a list of three floating point numbers. There are no commas, no brackets, and no decimal points in your data.

If you need to PRESENT your data in a specific way, then it is up to you to do that. What you're seeing is the way numpy presents data by default. If you want commas, you have to add them:

print( "[" + (",".join(str(f) for f in axis1_mean)) + "]")

Solution 2:[2]

I am not sure if that will help but they are indeed separated by commas (just like the documentation) if you look at them in a jupyter notebook or execute them. But the print() method prints it differently (without the commas). And it has nothing to do with the mean function itself.

Like this:

>>> np.array([5, 5, 5])

Output:

array([5, 5, 5])

But with print:

>>> print(np.array([5, 5, 5]))

Output:

[5 5 5]

If what you need is to "print" them seperated by commas then @Tim's answer will do the job

Solution 3:[3]

If you need to print them with the commas and the brackets.

You can use:

print(str(list(axis1_mean)))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts
Solution 2
Solution 3 D.Manasreh