Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct a ndarray from a numpy array? python

I can't seem to convert it into an ndarray in numpy, i've read http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html but it didn't show me how i can convert my input data as shown below into an ndarray.

How to construct a ndarray from a numpy array or a list of integer lists? *What's the difference between ndarray and array?* I could just use an array type right?

I have a list of integer counts like this

[[1, 2, 4, 1, 5],
 [6, 0, 0, 0, 2],
 [0, 0, 0, 1, 0]]

And i manage to use this code to create a np.array as shown in http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy.array

import numpy as np
x = [[1, 2, 4, 1, 5],
 [6, 0, 0, 0, 2],
 [0, 0, 0, 1, 0]]
print np.array(x)

[out]:

[[1 2 4 1 5]
 [6 0 0 0 2]
 [0 0 0 1 0]]

But I can't change it into a np.ndarray with this code:

import numpy as np
x = [[1, 2, 4, 1, 5],
[6, 0, 0, 0, 2],
[0, 0, 0, 1, 0]]
print np.ndarray(x)

I got an error:

Traceback (most recent call last):
  File "/home/alvas/workspace/sklearntut/test.py", line 7, in <module>
    print np.ndarray(x)
TypeError: an integer is required

How do I create a np.ndarray with the list of integer counts i've got? What integer is the TypeError talking about?

like image 356
alvas Avatar asked Jan 13 '14 09:01

alvas


People also ask

How do I create a NumPy Ndarray in Python?

Create a NumPy ndarray Object The array object in NumPy is called ndarray . We can create a NumPy ndarray object by using the array() function.

Is Ndarray same as NumPy array?

The main data structure in NumPy is the ndarray, which is a shorthand name for N-dimensional array. When working with NumPy, data in an ndarray is simply referred to as an array. It is a fixed-sized array in memory that contains data of the same type, such as integers or floating point values.

How do you define NumPy Ndarray?

The most important object defined in NumPy is an N-dimensional array type called ndarray. It describes the collection of items of the same type. Items in the collection can be accessed using a zero-based index. Every item in an ndarray takes the same size of block in the memory.


2 Answers

Though the accepted response is correct, that didn't help me actually create a 1-dimensional array of arrays.

As this thread is the first answer at Google, I post my work around, even if it isn't elegant solution (please don't hesitate to point one out to me):

import numpy as np

# Create example array
initial_array = np.ones(shape = (2,2))

# Create array of arrays
array_of_arrays = np.ndarray(shape = (1,), dtype = "object")
array_of_arrays[0] = initial_array

Be aware that array_of_arrays is in this case mutable, i.e. changing initial_array automatically changes array_of_arrays .

like image 60
Andarin Avatar answered Oct 09 '22 10:10

Andarin


An ndarray is a NumPy array.

>>> x = np.array([1, 2, 3])
>>> type(x)
<type 'numpy.ndarray'>

The difference between np.ndarray and np.array is that the former is the actual type, while the latter is a flexible shorthand function for constructing arrays from data in other formats. The TypeError comes your use of np.array arguments to np.ndarray, which takes completely different arguments (see docstrings).

like image 46
Fred Foo Avatar answered Oct 09 '22 10:10

Fred Foo