I'm not sure why you want it "this array to [look?] like a JavaScript array, so I am presuming (as I am at liberty to do in the absence of information to the contrary) that you wish to communicate the array to some unfortunate front-end process: almost four million elements is still a significant amount of data to squirt across network pipes. So, as always, some background to the problem would be helpful (and you can edit your question to provide it).
Assuming you want to serialize the data for transmission or storage then the simplest way to render it as a string comprehensible to JavaScript (I didn't rally know what "[look?] like" meant) is using the json
standard library. Since this can't natively encode anything but list and dicts of ints, floats, truth values and strings, you are still faced with the problem of how best to represent the matrix as a list of lists.
Small example, but you have to accept this is a random shot in the dark. First
let's create a manageable data set to work with:
a = np.random.randn(4, 5)
This cannot be directly represented in JSON:
import json
try:
json.dumps(a)
except Exception as e:
print "Exception", e
resulting in the rather verbose (it's probably just calling the object's repr
) but comprehensible and true message
Exception array([[ 1.24064541, 0.97989932, -0.8469167 , -0.27318908, 1.21954134],
[-1.30172725, 0.41261504, 1.39895842, 0.75260258, -1.34749298],
[-0.38415007, -0.56925321, -1.59202204, 1.29900292, 1.91357277],
[ 1.06254537, 2.75700739, -0.66371951, 1.36906192, -0.3973517 ]]) is not JSON serializable
If we ask the interpreter to convert the array to a list it does a half-hearted job, converting it into a list of array objects:
list(a)
shows as its result
[array([ 1.24064541, 0.97989932, -0.8469167 , -0.27318908, 1.21954134]),
array([-1.30172725, 0.41261504, 1.39895842, 0.75260258, -1.34749298]),
array([-0.38415007, -0.56925321, -1.59202204, 1.29900292, 1.91357277]),
array([ 1.06254537, 2.75700739, -0.66371951, 1.36906192, -0.3973517 ])]
Using the same function to convert those arrays into lists yields a usable list of lists:
list(list(r) for r in a)
evaluating to
[[1.2406454087805279,
0.97989932000522928,
-0.84691669720415574,
-0.27318907894171163,
1.219541337120247],
[-1.3017272505660062,
0.41261503624079976,
1.3989584188044133,
0.75260257672408482,
-1.3474929807527067],
[-0.38415007296182629,
-0.56925320938196644,
-1.5920220380072485,
1.2990029230603588,
1.9135727724853433],
[1.0625453748520415,
2.7570073901625185,
-0.66371950666590918,
1.3690619178580901,
-0.39735169991907082]]
This is eminently convertible to JSON, which I do here by converting it into a string:
json.dumps(list(list(r) for r in a))
which gives the (string) result
'[[1.2406454087805279, 0.97989932000522928, -0.84691669720415574, -0.27318907894171163, 1.219541337120247], [-1.3017272505660062, 0.41261503624079976, 1.3989584188044133, 0.75260257672408482, -1.3474929807527067], [-0.38415007296182629, -0.56925320938196644, -1.5920220380072485, 1.2990029230603588, 1.9135727724853433], [1.0625453748520415, 2.7570073901625185, -0.66371950666590918, 1.3690619178580901, -0.39735169991907082]]'
You can check that the result is correct by reconstituting the list of lists and comparing it with the array (since one of the arguments is a numpy array, the comparison is done elementwise):
s = json.dumps(list(list(r) for r in a))
lofls = json.loads(s)
lofls == a
array([[ True, True, True, True, True],
[ True, True, True, True, True],
[ True, True, True, True, True],
[ True, True, True, True, True]], dtype=bool)
Did I understand your question correctly?