Python Numpy πŸ˜€

In this article, we are going to discuss about Python Numpy.

Python Numpy

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

The array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, the number of dimensions of the array is called the rank of the array. A tuple of integers that specify the size of the array along each dimension is called the shape of the array. An array class in Numpy is called ndarray. Elements in numpy arrays are accessed using square brackets and can be initialized with Python nested lists.

Creating a Numpy Array 

Arrays in Numpy can be created in a number of ways, with different numbers of regions defining the size of the array. Arrays can also be created using different data types like lists, tuples, etc. The type of the resulting array is inferred from the type of the elements in the sequences. The array type can be explicitly defined when creating the array.

# Python program for Creation of Arrays

import numpy as num
 
# Creating a rank  Array
arr = num.array([1, 2, 3])
print("Array with Rank 1: \n",arr)
 
# Creating a rank  Array
arr = num.array([[1, 2, 3],
                [4, 5, 6]])
print("Array with Rank 2: \n", arr)
 
# Creating an array from tuple
arr = num.array((1, 3, 2))
print("\nArray created using "
      "tuple:\n", arr)

Output  as below

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

In a numpy array, indexing or accessing the index of the array can be done in a number of ways. To print a portion of an array, slice is run. Slicing an array defines a range in a new array that is used to print a range of elements from the original array. Because the split array contains a number of elements from the original array, changing the content using the split array will change the content of the original array.

# Python program

# indexing in numpy array
import numpy as np
 
# Initial Array
arr = num.array([[-1, 2, 0, 4],
                [4, -0.5, 6, 0],
                [2.6, 0, 7, 8],
                [3, -7, 4, 2.0]])
print("Initial Array: ")
print(arr)
 
# Printing a range of Array
# with the use of slicing method
sliced_arr = arr[:2, ::2]
print ("Array with first 2 rows and"
    " alternate columns(0 and 2):\n", sliced_arr)
 
# Printing elements at
# specific Indices
Index_arr = arr[[1, 1, 0, 3], 
                [3, 2, 1, 0]]
print ("\nElements at indices (1, 3), "
    "(1, 2), (0, 1), (3, 0):\n", Index_arr)

Output as  below

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.]
 [ 4.  6.]]

Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):
 [ 0. 54.  2.  3.]

Basic Array Operations

Arrays allows a wide range of operations to be performed on a given array or combination of arrays. These operations include some basic math operations, as well as unary and binary operations.

# Python program perform basic operations on single array
import numpy as num
 
# Defining Array 1
a = num.array([[1, 2],
              [3, 4]])
 
# Defining Array 2
b = num.array([[4, 3],
              [2, 1]])
               
# Adding 1 to every element
print ("Adding 1 to every element:", a + 1)
 
# Subtracting 2 from each element
print ("\nSubtracting 2 from each element:", b - 2)
 
# sum of array elements
# Performing Unary operations
print ("\nSum of all array "
       "elements: ", a.sum())
 
# Adding two arrays
# Performing Binary operations
print ("\nArray sum:\n", a + b)
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]]

Happy Learning :) 


Recommended Free Ebook
Similar Articles