Printing Numpy arrays without brackets

Viewed 4919
predictions  = [x6,x5,x4,x3,x2,x1]
predictions

Calling the above list yields the following arrays:

[array([782.36739152]),
 array([783.31415872]),
 array([726.90474426]),
 array([772.08910103]),
 array([728.79734162]),
 array([753.67887657])]

Yet I would like to print or call just the numbers inside, no array or brackets around the numbers.

Using the function below cleanly saves just the numbers to CSV, but I DON'T want to save the numbers, I want to call them inside iPython:

np.savetxt("P:/Earnest/Old/R/OutputPython.csv", predictions, delimiter=",")

How can I achieve this?

4 Answers

If you change predictions to numpy array then, you can use print(*predictions.flatten(), sep=', ').

You can try as following:

import numpy as np

predictions = np.array([np.array([782.36739152]),
                        np.array([783.31415872]),
                        np.array([726.90474426]),
                        np.array([772.08910103]),
                        np.array([728.79734162]),
                        np.array([753.67887657])])


print(*predictions.flatten(), sep=', ')

Output:

782.36739152, 783.31415872, 726.90474426, 772.08910103, 728.79734162, 753.67887657

The result would be a string.

>>> ' '.join(str(x[0]) for x in predicitons)
'782.36739152 783.31415872 726.90474426 772.08910103 728.79734162 753.67887657'

You could also round the result, e.g. str(round(x[0], 2)).

Try this:

print(' '.join(str(i.tolist()[0]) for i in arr))

Numpy arrays almost always come with the brackets. If you do not want brackets around each number, but good with them being around the whole array, the following code might help:

', '.join([str(lst[0]) for lst in predictions])

The delimiter could be modified to suit your purpose.

Hope it helps.

Related