Numpy lets us create arrays in multiple ways, most of the time in consonancy with core Python and other libraries like Pandas.

Creating ndarrays

We can get Numpy vector and a matrix rapidly from a Python list:

We can quickly create matrices of ones and zeros:

Or any other value:

We can create ndarrays initialized with values:

Or generate ndarrays of random values:

We can observe some attributes from an ndarray, with the following operations, for an array defined as np.array([(2,4,6)]):

  • itemsize: Will return 4, that is the number of bytes used by the array.
  • dtype: int32 is the result, that is the type of the elements in the array. (we can have np.int32, np.float16, np.float32, or np.float64)
  • ndim: Number if dimensions of de array, 1 for vector, 2 for matrix etc..

These are the data types we can use in numpy:

Data type Description
bool_ Boolean Stored as a byte
int_ Default integer type either int64 or int32
intc C int Either int32 or int64
intp C ssize_t Either int32 or int64
int8 Byte -128 to 127
int16 Integer -32768 to 32767
int32 Integer -2147483648 to 2147483647
int64 Integer -9223372036854775808 to 9223372036854775807
uint8 Unsigned integer 0 to 255
uint16 Unsigned integer 0 to 65535
uint32 Unsigned integer 0 to 4294967295
uint64 Unsigned integer 0 to 18446744073709551615
float_ float64 Shorthand for float64.
float16 Half precision float Sign bit, 5 bits exponent, 10 bits mantissa
float32 Single precision float Sign bit, 8 bits exponent, 23 bits mantissa
float64 Double precision float Sign bit, 11 bits exponent, 52 bits mantissa
complex_ complex128 Shorthand for complex128.
complex64 Complex number Represented by two 32-bit floats
complex128 Complex number Represented by two 64-bit floats

Structured arrays

We mentioned that numpy and ndarrays are fast and compact because they have homogeneus data on them, but htey might be cases when we need heterogeneous data on them, we can do this using Panda’s Dataframes, but can we do it using just Numpy?

Yes, we have structured arrays that can include multipe data types:

  • U10 means: Unicode string of maximum length 10 .
  • i4 means: 4-byte (i.e., 32 bit) integer
  • f8 means: 8-byte (i.e., 64 bit) float.

Slice and dice

Let´s create a 5x5 simple matrix and start slicing it!

The logic can be seen in the following highlighted image:

Stacking and tiling

Sometimes we’ll need to copy horitontally or vertically our matrix or vector, numpy can do this for us:


When there is no ambiguity the smaller array will be expanded to match the shape of the bigger array.