NumPy

NumPy is the very important library of python which help you and support you to dealing with large, multi -dimensional array and matrices, with collection of mathematical function use to operate these numeric array, The ancient name of NumPy is Numeric creted by Jim Hugunin but in 2005, Travis Oliphant created NumPy with feature of competing/dealing Numarray into Numeric, NumPy is an open-source software with many contributers.

The main target of NumPy is CPython to implimentation of Python, which is a non-optimizing bytecode interpreter. Mathematical operations or algorithms are use to operate in Python but it run slow to deal with the slowness problem NumPy provides multidimensional array and function that operate these mathematical functions to reduce the slowness, NumPy uses inner loops to dealing with these arrays.

NumPy also give the comparability with the MATLAB as they both are interpreter as it allows programmer to write the code less and fast with efficiency most of the operations used to work on the arrays and mathematical operations or equations.

Array Creation : to create array in python you have to import the library Numpy,

import numpy as np
x = np.array([1, 2, 3])
x # print x
array([1, 2, 3])

Array Indexing : To understand the indexing of an array you have to understand the objects of an array.

  • Slicing
  • Boolean array indexing
  • Integer array indexing
#array creation
 arr = np.array([[-1, 2, 0, 4], 
 [4, -0.5, 6, 0], 
 [2.6, 0, 7, 8],[3, -7, 4, 2.0]])   

#slicing
a = arr[:2,:2] 
print ("Array with first 2 rows and alternate""columns(0 and 2):\n", temp) 

# boolean array indexing
condition = arr > 0 
# condition  is a boolean array 
a = arr[condition] 
print("\n Elements greater than 0:\n",a)  

#indexing array indexing
a = arr[[0,1,2,3],[3,2,1,0]] 
print("\n Elements at indices (0, 3), (1, 2), (2, 1),""(3, 0):\n",a) 

Output:

Array with first 2 rows and alternate columns(0 and 2):
 [[-1.  0.]
 [ 4.  6.]]
Elements at indices (0, 3), (1, 2), (2, 1),(3, 0):
 [ 4.  6.  0.  3.]
Elements greater than 0:
 [ 2.   4.   4.   6.   2.6  7.   8.   3.   4.   2. ]

Basic Operations : Here are some basic operations are

import numpy as np 
a = np.array([1, 2, 5, 3]) 
print ("Adding 1 to every element:", a+1) 
print ("Subtracting 3 from each element:", a-3) 
print ("Multiplying each element by 10:", a*10) 
print ("Squaring each element:", a**2) 
a *= 2
print ("Doubled each element of original array:", a) 
a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]]) 
print ("\nOriginal array:\n", a) 
print ("Transpose of array:\n", a.T) 

Output:

Adding 1 to every element: [2 3 6 4]
Subtracting 3 from each element: [-2 -1  2  0]
Multiplying each element by 10: [10 20 50 30]
Squaring each element: [ 1  4 25  9]
Doubled each element of original array: [ 2  4 10  6]

Original array:
 [[1 2 3]
 [3 4 5]
 [9 6 0]]
Transpose of array:
 [[1 3 9]
 [2 4 6]
 [3 5 0]]

Leave a Comment

Your email address will not be published. Required fields are marked *