1

I've been trying to create a 6 by 6 array in python using the numpy module with all elements set as 0.0:

import numpy as np
RawScores = np.zeros((6,6), dtype=np.float)
print RawScores

This outputs:

[[ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]]

Why are there no commas? Is it still an array without them?

2
  • 3
    That's just the way numpy displays it - try type(RawScores) to check that it is what you think it is. Commented Jun 5, 2014 at 15:02
  • print type(RawScores) resulted in an output of <type 'numpy.ndarray'> , so I assume this means it must be an array, so it's ok there are no commas! Thank you :) Commented Jun 5, 2014 at 15:56

2 Answers 2

3

It is because the print function calls the __str__ method, instead of the __repr__ method. See the following example. Please see here for a detailed explanation of the difference between these two methods.

# Python 2D List
>>> [[0]*6]*6
[[0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0]]

>>> import numpy as np
>>> np.zeros((6,6))
array([[ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.]])

# calls __repr__
>>> a = np.zeros((6,6))
>>> a
array([[ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.]])

# calls __str__
>>> print a
[[ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]]
Sign up to request clarification or add additional context in comments.

Comments

0

If you really really care about the commas you can always redefine the way numpy prints out arrays, VERY dirty example:

import numpy as np
RawScores = np.zeros((6,6), dtype=np.float)
def add_comma(num):
  return "%g, " % num

np.set_printoptions(formatter={"all":add_comma})
print RawScores
[[0,  0,  0,  0,  0,  0, ]    
 [0,  0,  0,  0,  0,  0, ]
 [0,  0,  0,  0,  0,  0, ]
 [0,  0,  0,  0,  0,  0, ]
 [0,  0,  0,  0,  0,  0, ]
 [0,  0,  0,  0,  0,  0, ]]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.