NumPy Tutorial
NumPy Install
# pip install numpy ------> It will install numpy library into you
computer
Import numpy and Array with types
# Here's the numpy is imported
import numpy as np
# Array -----> Array is a data structure in programming used to store
data of same type.
# Types of Array ------> |One Dimensional| |Two Dimensional| |
Three Dimensional| arrays
#One Dimensional Array
one_dimensional_array= [Link]([1,2,3,4,5])
print("1d Array: ")
print(one_dimensional_array)
print("Dimension: ",one_dimensional_array.ndim)
#Two Dimensional Array ----> Array which is the form of row and
coloums (MATRICES)
two_dimensionl_array=[Link]([
[1,2,3],
[4,5,6]
])
print("2d Array: ")
print(two_dimensionl_array)
print("Dimemsion: ",two_dimensionl_array.ndim)
# Dimension is
taken by this syntax= name of [Link]
#Three Dimensional Array -----> Array which contain 2d arrays in a
stack
three_dimensional_array = [Link]([
[
[1, 2, 3],
[4, 5, 6]
],
[
[1, 2, 3],
[4, 5, 6]
]
])
print("3d Array: ")
print(three_dimensional_array)
print("Dimension: ", three_dimensional_array.ndim)
Creating Array using lists and tuples
import numpy as np
# list
list=[1,2,3,4,5]
# tuple
tuple=[1,2,3,4,5]
# Now create array using list and tuple
arr=[Link](list)
print("I'm the array which is created by list: ",arr)
arr1=[Link](tuple)
print("I'm the array which is created by tuple: ",arr1)
2d Arrays Of Special Types
# Zeroes Array
import numpy as np
zero_array=[Link](shape=(2,3))
print("Zero Matrix")
print(zero_array)
#Ones Array
one_array=[Link](shape=(2,3))
print("One Matrix")
print(one_array)
#Specific Number Matrix
specific_matrix = [Link]((2,3), 7)
print("Specific Matrix")
print(specific_matrix)
#Identity Matrix
identity_matrix=[Link](3)
print("Identity Matix")
print(identity_matrix)
Size & Data Type
# Array Size
import numpy as np
arr=[Link]([1,2,3,4,5])
print([Link])
# Array Data Type
print([Link])
#The Current Data Type of array is int
#Now we convert this int into string
new_arr=[Link](str)
print(new_arr.dtype)
Operations in array and their use
import numpy as np
arr=[Link]([1,2,3,4,5])
# Common operators are + , - , * , / , % , ** etc
# " + " Operator
print("Use of + operator: ",arr+1)
# " - " Operator
print("Use of - operator: ",arr-1 )
# "*" Operator
print("Use of * operator: ",arr*2)
Aggregation Functions in numpy
import numpy as np
arr=[Link]([10,20,30,40,50])
# Function for sum of all elements of array
print("Sum of all elements: ",[Link](arr))
# Function of the average of all elements of array
print("Average /Mean: ",[Link](arr))
# Function to get minimum number from the array
print("Minimum: ",[Link](arr))
# Function to get the maximum number from the array
print("Maximum: ",[Link](arr))
# Function to get standerd deviation of an array
print("Standerd Deviation: ",[Link](arr))
# Function to get the variance of an array
print("Variance: ",[Link](arr))