## The NumPy ndarray Object

While the array object of Python’s array module provides efficient storage of array-based data, NumPy’s ndarray object adds several more advantages:

• efficient operations on the data via dozens of specialized functions.
• works for multi-dimensional arrays.
• closer to hardware (faster and more efficient).
• designed for scientific computation (convenience).

Example

Creating a 1-D ndarray object.

1import numpy as np
2np.array([1,2,3,4,5])

array([1, 2, 3, 4, 5])

Note

We need to first import the NumPy package and assign an alias.

Recall that NumPy is constrained to creating arrays that contain elements of the same data type. If the types do not match, NumPy will upcast if possible (as in the following example where integers are upcast to floats):

Example

Integers upcast to floats.

1np.array([2.48, 4, 2, 3])

array([2.48, 4.  , 2.  , 3.  ])


## Setting the Data Type

NumPy arrays can only contain values of a single type, so it is important to understand the various possible data types and their limitations. The standard NumPy data types are listed in the following table in 5 broad categories:

• Booleans
• Integers
• Unsigned integers
• Floats
• Complex numbers.
Category Data Type Description
Strings str_ Unicode string
Boolean bool_ Boolean (True or False) stored as a byte
Integers int_ Default integer type (same as C long; normally either int64 or int32)
intc Identical to C int (normally int32 or int64)
intp Integer used for indexing; identical to C ssize_t (normally int32 or int64)
int8 Integer (–128 to 127)
int16 Integer (–32768 to 32767)
int32 Integer (–2147483648 to 2147483647)
int64 Integer (–9223372036854775808 to 9223372036854775807)
Unsigned Integers uint8 Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
Floats float_ 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 complex_ Shorthand for complex128
complex64 Complex number, represented by two 32-bit floats
complex128 Complex number, represented by two 64-bit floats

If we want to explicitly set the data type of the NumPy array, we need to use the dtype keyword with either a string or a NumPy object. For example, if we want to specify the float32 data type, we can do either one of the following:

Example

Specifying the data type using a string.

1np.array([1, 2, 3, 4], dtype='float32')

array([1., 2., 3., 4.], dtype=float32)


Example

Specifying the data type using a NumPy object.

1np.array([1, 2, 3, 4], dtype=np.float32)

array([1., 2., 3., 4.], dtype=float32)


### Using Data Type Codes

Alternatively, each built-in data-type also has a character code that uniquely identifies it.

Code Description Example Equivalent to
‘i’ Signed Integer np.dtype(‘i4’) np.int32
‘u’ Unsigned integer np.dtype(‘u1’) np.uint8
‘f’ Floating point np.dtype(‘f8’) np.int64
‘c’ Complex floating point np.dtype(‘c16’) np.complex128
‘S’ String np.dtype(‘S5’)
‘U’ Unicode string np.dtype(‘U’) np.str_
‘V’ Raw data np.dtype(‘V’) np.void
Note

1 byte is equivalent to 8 bits.

The first character specifies the kind of data and the remaining characters specify the number of bytes per item, except for Unicode, where it is interpreted as the number of characters. The item size must correspond to an existing type, or an error will be raised.

Example

Specifying an integer type using character code.

1np.dtype('i4')

dtype('int32')


The first character i indicates it is a signed integer and second character indicates that there are 4 bytes per item which means it is a 4*8=32 bit signed integer array.

Example

Specifying a floating-point number type using character code.

1np.dtype('f8')

dtype('float64')


The first character f indicates it is a floating point number and second character indicates that there are 8 bytes per item which means it is a 8*8=32 bit float array.

Example

Specifying a floating-point number type using character code.

1np.dtype('c16')

dtype('complex128')


The first character c indicates it is a complex floating point number and second character indicates that there are 16 bytes per item which means it is a 16*8=32 bit complex floating point array.

Example

Specifying a 25-character string type using character code.

1np.dtype('U25')

dtype('<U25')


The first character U indicates it is a unicode string and second character indicates that there are 25 characters per item. Since one character consumes 4 bytes, this means one item consumes 25*4=100 bytes. The total number of bytes will depend on the number of items within the array.

### Specifying the float64 Data Type

To specify the float64 data type, we can use either one of the following equivalent dtypes:

• 'float64'
• np.float64
• np.float_
• float
• 'f8'

Example

Specifying the float64 data type.

1print(np.array([1, 2, 3, 4], dtype='float64'))
2print(np.array([1, 2, 3, 4], dtype=np.float64))
3print(np.array([1, 2, 3, 4], dtype=np.float_))
4print(np.array([1, 2, 3, 4], dtype=float))
5print(np.array([1, 2, 3, 4], dtype='f8'))

[1. 2. 3. 4.]
[1. 2. 3. 4.]
[1. 2. 3. 4.]
[1. 2. 3. 4.]
[1. 2. 3. 4.]


### Specifying the int32 Data Type

To specify the int32 data type, we can use either one of the following equivalent dtypes:

• 'int32'
• np.int32
• np.int_
• int
• 'i4'

Example

Specifying the int32 data type.

1print(np.array([1, 2, 3, 4], dtype='int32'))
2print(np.array([1, 2, 3, 4], dtype=np.int32))
3print(np.array([1, 2, 3, 4], dtype=np.int_))
4print(np.array([1, 2, 3, 4], dtype=int))
5print(np.array([1, 2, 3, 4], dtype='i4'))

[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[1 2 3 4]


## Creating NumPy Arrays

We have seen how we can create a 1-D NumPy array using a Python list. NumPy arrays can also be multidimensional. One way of creating a multidimensional array is using a nested list.

Example

Creating a 2-D NumPy array using nested list comprehension.

1np.array([range(i, i + 3) for i in range(2,9,3)])

array([[ 2,  3,  4],
[ 5,  6,  7],
[ 8,  9, 10]])


### Initializing NumPy Arrays

NumPy offers several array creation routines to create arrays with certain initial placeholder values. These initialized arrays can subsequently be populated through loops or other means. Examples of such routines include np.zeros, np.ones, np.full and np.empty.

Array Creation Routine Output Array
np.zeros(shape, dtype=float) Filled with zeros.
np.ones(shape, dtype=None) Filled with ones.
np.full(shape, fill_value, dtype=None) Filled with a certain fill_value.
np.empty(shape, dtype=float) Entries not initialized.

Example

Create an integer array filled with zeros.

1np.zeros(8, dtype=int) #int32

array([0, 0, 0, 0, 0, 0, 0, 0])


Example

Create a 3X5 integer array filled with zeros.

1np.zeros((3, 5), dtype=int)

array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])


Example

Create a 3x5 floating-point array filled with ones.

1np.ones((3, 5), dtype=float) #float64

array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])


Example

Create a 3x5 array filled with a constant float.

1np.full((3, 5), 2.84, dtype=float)

array([[2.84, 2.84, 2.84, 2.84, 2.84],
[2.84, 2.84, 2.84, 2.84, 2.84],
[2.84, 2.84, 2.84, 2.84, 2.84]])


Example

Creating a 3-D array.

1import numpy as np
2np.zeros((2, 3, 4),dtype=int)

array([[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],

[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]])

Note

The shape of a 3-D array is specified as (frames, rows, columns).

Example

Create a 2x2 uninitialized array.

1np.empty((2,2),dtype=float)

array([[1., 2.],
[3., 4.]])


By “empty”, this means that the generated values will be whatever happens to already exist at that memory location.

### The numpy.eye() Function

The numpy.eye() function returns a 2-D array with ones on the diagonal and zeros elsewhere.

Syntax

The numpy.eye() function.

1numpy.eye(N, M=None, k=0, dtype=<class 'float'>)

Parameter Required? Default Value Description
N ✔️ Yes NA Number of rows in the output.
M ❌ No None Number of columns in the output. If None, defaults to N.
k ❌ No 0 Index of the diagonal: 0 refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal.
dtype ❌ No float Data type of the returned array.

Example

Create a 3x3 identity matrix.

1np.eye(3)

array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])


Example

Create a 3x3 upper triangular matrix.

1np.eye(3,None,1)

array([[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]])


Example

Create a 4x4 lower triangular matrix with integer values.

1np.eye(4,None,-2, dtype=int)

array([[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 1, 0, 0]])


### The numpy.arange() Function

The numpy.arange() function is similar to the Python range() function and returns an array of evenly spaced values within a given interval.

Syntax

The numpy.arange() function.

1numpy.arange(start=0, stop, step=1, dtype=None)

Parameter Required? Default Value Description
start ❌ No 0 Start of interval. The interval is inclusive of this value.
stop ✔️ Yes NA End of interval. The interval is not incluive of this value.
step ❌ No 1 Spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i].
dtype ❌ No Inferred. The type of the output array. If dtype is not given, data type is inferred from the other input arguments.

Example

Create an array of evenly spaced values using numpy.arange().

1np.arange(0, 10, 2)

array([0, 2, 4, 6, 8])


Example

Create an array of evenly spaced values using numpy.arange().

1np.arange(0, 1, 0.125)

array([0.   , 0.125, 0.25 , 0.375, 0.5  , 0.625, 0.75 , 0.875])


Example

Using numpy.arange() with negative step.

1np.arange(1 ,0, -0.2)

array([1. , 0.8, 0.6, 0.4, 0.2])


### The numpy.linspace() Function

The numpy.linspace() function returns a specified number of evenly-spaced values calculated over an interval.

Syntax

The numpy.linspace() function.

1numpy.linspace(start=0, stop, num=50, endpoint=True, retstep=False, dtype=None)

Parameter Required? Default Value Description
start ❌ No 0 The starting value of the sequence.
stop ✔️ Yes NA The end value of the sequence, unless endpoint is set to False. In that case, the sequence consists of all but the last of num + 1 evenly spaced samples, so that stop is excluded. .
num ❌ No 50 Number of samples to generate.
endpoint ❌ No True If True, stop is the last sample. Otherwise, it is not included.
retstep ❌ No False If True, return (samples, step), where step is the spacing between samples.
dtype ❌ No Inferred. The type of the output array. If dtype is not given, data type is inferred from start and stop.

Example

Create an array of evenly-spaced samples using numpy.linspace().

1np.linspace(0, 1, 9)

array([0.   , 0.125, 0.25 , 0.375, 0.5  , 0.625, 0.75 , 0.875, 1.   ])


Example

Create an array using numpy.linspace() with retstep=True.

1np.linspace(0, 1, 9, retstep=True)

(array([0.   , 0.125, 0.25 , 0.375, 0.5  , 0.625, 0.75 , 0.875, 1.   ]), 0.125)


Example

Create an array using numpy.linspace() with default number of samples.

1np.linspace(0, 10, retstep=True)

(array([ 0.        ,  0.20408163,  0.40816327,  0.6122449 ,  0.81632653,
1.02040816,  1.2244898 ,  1.42857143,  1.63265306,  1.83673469,
2.04081633,  2.24489796,  2.44897959,  2.65306122,  2.85714286,
3.06122449,  3.26530612,  3.46938776,  3.67346939,  3.87755102,
4.08163265,  4.28571429,  4.48979592,  4.69387755,  4.89795918,
5.10204082,  5.30612245,  5.51020408,  5.71428571,  5.91836735,
6.12244898,  6.32653061,  6.53061224,  6.73469388,  6.93877551,
7.14285714,  7.34693878,  7.55102041,  7.75510204,  7.95918367,
8.16326531,  8.36734694,  8.57142857,  8.7755102 ,  8.97959184,
9.18367347,  9.3877551 ,  9.59183673,  9.79591837, 10.        ]),
0.20408163265306123)


Example

Create an array of evenly-spaced int values using numpy.linspace().

1np.linspace(0, 1, 9, dtype=int)

array([ 0,  1,  2,  3,  5,  6,  7,  8, 10])


Note that the samples generated are truncated since dtype=int is specified.