Russell Christopher
Russell Christopher

Reputation: 1707

Creating a 2d NumPy array (Python)

NumPy newb.

I created a simple 2d array in np_2d, below. Works great.

Of course, I'm generally going to need to create N-d arrays by appending and/or concatenating existing arrays, so I'm trying that next.

The np.append method (with or without the axis parameter) doesn't seem to do anything.

My attempts to use .concantenate() and/or simply replace raw lists with np arrays also fail.

I'm sure this is trivial to do...just not trivial for me ATM. Can someone push me in the right direction? TY.

import numpy as np

# NumPy 2d array:
np_2d = np.array([[1.73, 1.68, 1.71, 1.89, 1.79], [65.4, 59.2, 63.6, 88.4, 68.7]])

print (np_2d) 

# [[ 1.73  1.68  1.71  1.89  1.79]
# [65.4  59.2  63.6  88.4  68.7 ]]

print (np_2d[1]) # second list

# [65.4 59.2 63.6 88.4 68.7]

np_2d_again = np.array([1.1, 2.2, 3.3])

np.append(np_2d_again, [4.4, 5.5, 6.6])
print(np_2d_again)

# wrong: [1.1 2.2 3.3], expect [1.1 2.2 3.3], [4.4, 5.5, 6.6]
# or MAYBE [1.1 2.2 3.3, 4.4, 5.5, 6.6]


np_2d_again = np.array([[1.1, 2.2, 3.3]])
np.concatenate(np_2d_again, np.array([4.4, 5.5, 6.6]))

# Nope: TypeError: only integer scalar arrays can be converted to a scalar index

print(np_2d_again)

np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79])
np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7])

np2_2d_again = np.array(np_height, np_weight)

# Nope: TypeError: data type not understood

height = [1.73, 1.68, 1.71, 1.89, 1.79]
weight = [65.4, 59.2, 63.6, 88.4, 68.7]

np2_2d_again = np.array(height, weight)

# Nope: TypeError: data type not understood

Upvotes: 1

Views: 7199

Answers (1)

hpaulj
hpaulj

Reputation: 231335

For questions like these, the docs can be really useful. Check them out here:

Using these you'll find:

In [2]: np_2d = np.array([[1.73, 1.68, 1.71, 1.89, 1.79], [65.4, 59.2, 63.6, 88.4, 68.7]])
   ...: 
In [2]: np_2d
Out[2]: 
array([[ 1.73,  1.68,  1.71,  1.89,  1.79],
       [65.4 , 59.2 , 63.6 , 88.4 , 68.7 ]])

Pay attention to the input to np.array. It is one list, containing 2 lists of equal length.

In [3]: np_2d_again = np.array([1.1, 2.2, 3.3])
In [4]: np.append(np_2d_again, [4.4, 5.5, 6.6])
Out[4]: array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])

Look at the np.append docs. See what says about raveling? It is joining one (3,) array to another, the result is (6,).

np.append is poorly named and often misused. It is not a drop in substitute for list append. For one thing it does not operate inplace.

In your np.concatenate(np_2d_again, np.array([4.4, 5.5, 6.6])), you get error because it expects an axis number as the 2nd argument. Reread the docs. You need to give a list of the arrays you want to join. np.append may have misled.

The correct way to use concatenate:

In [6]: np.concatenate([np_2d_again, np.array([4.4, 5.5, 6.6])])
Out[6]: array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])

But since both inputs are (3,), they can only be joined on the 0 axis, making a (6,) shape.

np2_2d_again = np.array(np_height, np_weight) has a similar problem. The 2nd argument is supposed to be a dtype, not another array. You used np.array correctly the first time.

In [7]: np.array([np_2d_again, np.array([4.4, 5.5, 6.6])])
Out[7]: 
array([[1.1, 2.2, 3.3],
       [4.4, 5.5, 6.6]])

np.array joins the components along a new axis. It's treating the list of arrays in basically the same as your original list of lists.

np.stack is a useful frontend for concatenate, which behaves like np.array (with a little more flexibility in the use of axis):

In [8]: np.stack([np_2d_again, np.array([4.4, 5.5, 6.6])])
Out[8]: 
array([[1.1, 2.2, 3.3],
       [4.4, 5.5, 6.6]])

Upvotes: 3

Related Questions