NumPy

Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Besides its obvious scientific uses, Numpy can also be used as an efficient multi-dimensional container of generic data.

Arrays in Numpy: NumPy’s main object is the homogeneous multidimensional array.

  • It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers.

  • In NumPy dimensions are called axes. The number of axes is rank.

  • NumPy’s array class is called ndarray. It is also known by the alias array.

[[ 1, 2, 3],

[ 4, 2, 5]]

Here, rank = 2 (as it is 2-dimensional or it has 2 axes) first dimension(axis) length = 2, second dimension has length = 3 overall shape can be expressed as: (2, 3)

Example 1:

# Python program to demonstrate  
# basic array characteristics 
import numpy as np 
  
# Creating array object 
arr = np.array( [[ 1, 2, 3], 
                 [ 4, 2, 5]] ) 
  
# Printing type of arr object 
print("Array is of type: ", type(arr)) 
  
# Printing array dimensions (axes) 
print("No. of dimensions: ", arr.ndim) 
  
# Printing shape of array 
print("Shape of array: ", arr.shape) 
  
# Printing size (total number of elements) of array 
print("Size of array: ", arr.size) 
  
# Printing type of elements in array 
print("Array stores elements of type: ", arr.dtype) 

Array is of type: <class 'numpy.ndarray'>

No. of dimensions: 2

Shape of array: (2, 3)

Size of array: 6

Array stores elements of type: int32

Example 2:

Array created using passed list:

[[1. 2. 4.]

[5. 8. 7.]]

Array created using passed tuple:

[1 3 2]

An array initialized with all zeros:

[[0. 0. 0. 0.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]]

An array initialized with all 6s.Array type is complex:

[[6.+0.j 6.+0.j 6.+0.j]

[6.+0.j 6.+0.j 6.+0.j]

[6.+0.j 6.+0.j 6.+0.j]

A random array:

[[0.48429795 0.62234042]

[0.2816561 0.63487782]]

A sequential array with steps of 5:

[ 0 5 10 15 20 25]

A sequential array with 10 values between 0 and 5:

[0. 0.55555556 1.11111111 1.66666667 2.22222222 2.77777778 3.33333333 3.88888889 4.44444444 5. ]

Original array:

[[1 2 3 4]

[5 2 4 2]

[1 2 0 1]]

Reshaped array:

[[[1 2 3]

[4 5 2]]

[[4 2 1]

[2 0 1]]]

Original array:

[[1 2 3]

[4 5 6]]

Fattened array:

[1 2 3 4 5 6]

Example 3:

OUTPUT:

Array with Rank 1:

[1 2 3]

Array with Rank 2:

[[1 2 3]

[4 5 6]]

Array created using passed tuple:

[1 3 2]

Accessing the array Index:

Example 4:

OUTPUT:

Initial Array:

[[-1. 2. 0. 4. ]

[ 4. -0.5 6. 0. ]

[ 2.6 0. 7. 8. ]

[ 3. -7. 4. 2. ]]

Array with first 2 rows and alternate columns(0 and 2):

[[-1. 0.]

[ 2.6. 7.]]

Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):

[ 0. 6. 2. 3.]

Basic Array Operations:

Example 5:

OUTPUT:

Adding 1 to every element:

[[2 3]

[4 5]]

Subtracting 2 from each element:

[[ 2 1]

[ 0 -1]]

Sum of all array elements: 10

Array sum:

[[5 5]

[5 5]]

Indexing using index arrays:

Example 6:

OUTPUT:

A sequential array with a negative step: [10 8 6 4 2]

Elements at these indices are: [4 8 6]

Types of Indexing:

1. Basic Slicing and indexing:

Example 7:

OUTPUT:

Array is:

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]

a[-8:17:1] = [12 13 14 15 16]

a[10:] = [10 11 12 13 14 15 16 17 18 19]

2. Advanced indexing:

Advanced indexing is triggered when obj is :

  1. an array of type integer or Boolean

  2. or a tuple with at least one sequence object

  3. is a non tuple sequence object

Example 8:

OUTPUT:

[1 3 6]

Example 9:

OUTPUT:

[80 100]

Example 10:

OUTPUT:

[1600 6400]

numpy.sum() in Python

numpy.sum(axis) : This function returns the sum of array elements over the specified axis.

axis : axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened(works on all the axis). axis = 0 means along the column and axis = 1 means working along the row.s function returns the sum of array elements over the specified axis.

Example 11:

OUTPUT:

[10 9 20]

[[ 5 5]

[16 4]]

Example 12:

Largest element is: 9

Row-wise maximum elements: [6 7 9]

Column-wise minimum elements: [1 1 2]

Sum of all array elements: 38

Cumulative sum along each row:

[[ 1 6 12]

[ 4 11 13]

[ 3 4 13]]

Example 13:

Array sum:

[[5 5]

[5 5]]

Array multiplication:

[[4 6]

[6 4]]

Matrix multiplication:

[[ 8 5]

[20 13]]

Last updated

Was this helpful?