14

I'm trying to generate a 2d numpy array with the help of generators:

x = [[f(a) for a in g(b)] for b in c]

And if I try to do something like this:

x = np.array([np.array([f(a) for a in g(b)]) for b in c])

I, as expected, get a np.array of np.array. But I want not this, but ndarray, so I can get, for example, column in a way like this:

y = x[:, 1]

So, I'm curious whether there is a way to generate it in such a way.

Of course it is possible with creating npdarray of required size and filling it with required values, but I want a way to do so in a line of code.

Taygrim
  • 379
  • 2
  • 3
  • 14
  • And it has to be a generator expression? It can't be a `np.shape` or anything like that? – ljetibo Feb 20 '15 at 12:17
  • what do `f(a)` and `g(b)` do exactly? If they produce numbers, your code should work: that is the correct way to initialize a 2d numpy array (numpy is generally smart enough to cast an array of an array to a ndarray) – Marijn van Vliet Feb 20 '15 at 12:20
  • What is `data['Name']` like? It's best if you give us examples that we can plug and run. What is the shape of `np._names`? I don't see any generators in your code, just list comprehensions. – hpaulj Feb 20 '15 at 17:52

3 Answers3

17

This works:

a = [[1, 2, 3], [4, 5, 6]]
nd_a = np.array(a)

So this should work too:

nd_a = np.array([[x for x in y] for y in a])
Marijn van Vliet
  • 5,239
  • 2
  • 33
  • 45
7

To create a new array, it seems numpy.zeros is the way to go

import numpy as np
a = np.zeros(shape=(x, y))

You can also set a datatype to allocate it sensibly

>>> np.zeros(shape=(5,2), dtype=np.uint8)
array([[0, 0],
       [0, 0],
       [0, 0],
       [0, 0],
       [0, 0]], dtype=uint8)
>>> np.zeros(shape=(5,2), dtype="datetime64[ns]")
array([['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
       ['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
       ['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
       ['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
       ['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000']],
      dtype='datetime64[ns]')

See also

ti7
  • 16,375
  • 6
  • 40
  • 68
2

Its very simple, do like this

import numpy as np

arr=np.arange(50)
arr_2d=arr.reshape(10,5)    #Reshapes 1d array in to 2d, containing 10 rows and 5 columns.
print(arr_2d)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
thinkingmonster
  • 5,063
  • 8
  • 35
  • 57