3
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?

ZJAY
  • 2,517
  • 9
  • 32
  • 51
  • 1
    May be `results = [i[0] for i in predictions] ` – niraj Sep 19 '18 at 00:37
  • thanks @student - but writing a for loop just to print a list of numbers seems excessive to after all...just print a list of numbers. – ZJAY Sep 19 '18 at 00:38

4 Answers4

5

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
niraj
  • 17,498
  • 4
  • 33
  • 48
1

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)).

Alexander
  • 105,104
  • 32
  • 201
  • 196
  • thanks @Alexander - this is indeed correct. I'm just a bit surprised that Python/numpy doesn't have an easier way to output a list of numbers in a console. – ZJAY Sep 19 '18 at 00:41
  • See also this: https://stackoverflow.com/questions/37084812/how-to-remove-decimal-points-in-pandas/37084889#37084889 – Alexander Sep 19 '18 at 00:42
0

Try this:

print(' '.join(str(i.tolist()[0]) for i in arr))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

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.

YC_
  • 329
  • 2
  • 9
  • @U9-Forward I may concede about the inefficiency, because there must be better ways to achieve the same result. But I tested it locally with the sample data provided and it worked on my machine. Could you please help make clear why it doesn't work? – YC_ Sep 20 '18 at 14:16
  • Ok accepted answers output is: `782.36739152, 783.31415872, 726.90474426, 772.08910103, 728.79734162, 753.67887657` so means that not right output for yours – U13-Forward Sep 20 '18 at 23:17
  • @U9-Forward Thank you very much for pointing this out! I will try to modify my solution so as it can really help to some extent :-) – YC_ Sep 21 '18 at 02:57
  • Ok edit, is better, sorry i can't undownvote, because i didn't downvote – U13-Forward Sep 21 '18 at 03:05
  • @U9-Forward No big deal, it is nice to learn from your comments :-) – YC_ Sep 21 '18 at 03:41