Numpy Cheatsheet 1732341190
Numpy Cheatsheet 1732341190
print('NUMPY')
print('===================')
print('')
#importnumpyasnp
#print(dir(np))
#import numpy as np
#array=np.array({1,2,3,4,6})
#print(array)
#print(type(array))
#import numpy as np
#print(np.__version__)
#importnumpyasnp
#a=np.array([[1,2,3,5,6,7]])
#print(a.ndim)
#importnumpyasnp
#a=np.array([[[1,2,3,5,6,7]]])
#print(a.ndim)
#tocheckthedatatypeofthearray
#importnumpyasnp
#a=np.array([1,3,4,5])
#print(a.dtype)#int32
#importnumpyasnp
#a=np.array([1.3,3,4.5,5])
#print(a.dtype)#float64
##When the array Is created ,you can define the number of dimensions by using the 'ndmin' argument
#importnumpyasnp
#a=np.array([1,3,4,2],dtype='S')
#print(a)#[b'1'b'3'b'4'b'2']
#importnumpyasnp
#arr=np.array(['a','2','3'],dtype='i')
#print(a)#error
#importnumpyasnp
#arr=np.array([1.1,2.1,3.7])
#newarr=arr.astype(int)
#print(newarr)
#==============revision==========
#importnumpyasnp
#arr=np.array([1,2,3,4,5,6])
#print(arr)
#print(arr.ndim)
#print(arr.dtype)
#newarr=arr.astype('S')
#print(newarr)
#newarr=arr.astype('f')
#print(newarr)
#newarr=arr.astype('i')
#print(newarr)
#importnumpyasnp
#arr=np.array([1,2,3,4,5,6],dtype='f')
#print(arr)
#arr=np.array([1,2,3,4,5,6],dtype='S')
#print(arr)
#============16-09-24==============
#import numpy as np
#arr=np.array([[-1,0,1],[1,2,3]])
#newarr=arr.astype(bool)
#print(newarr)
#print(arr.shape)
##[[TrueFalseTrue]
##[TrueTrueTrue]]
##(2,3)
#Output:
#Array before copy: [1 2 3 4 5]
#Copied array: [1 2 3 4 5]
#Original array after changes: [32 2 3 4 5]
#print('=====================')
###View=>
#import numpy as np
#arr=np.array([1,2,3,4])
#print(f'Arraybeforeview:{arr}')
#newarr=arr.view()
#arr[0]=32
#print(f'Viewarray:{newarr}')
#print(f'Originalarrayafterchanges:{arr}')
Output:
#Array before view: [1 2 3 4]
#View array: [32 2 3 4]
#Original array after changes: [32 2 3 4]
#import numpy as np
#arr=np.array([1,2,3,4])
#a=arr.copy()
#b=arr.view()
#print(a.base) #copy=>ownsdata
#print(b.base) #view=>doesnotownsdata
##'Shape' is the attribute in numpy gives you the order of your array in the form of (rows, column)
#import numpy as np
#arr=np.array([[1,2,3,4],[4,6,5,7]])
#newarr=arr.shape
##The example above returns(2,4),which means that the array has 2 dimensions, where the first dimension has 2 elements and these cond has 4.
##Q-Create an array with 5 dimensions using 'ndmin' using a vector with values 1,2,3,4 and verify that last dimension has value 4.
#import numpy as np
#arr=np.array([1,2,3,4],ndmin=5)
#print(arr)
#print(f'Shapeofthatarray:{arr.shape}')
##Output=>(1,1,1,1,4)
##Reshaping arrays
1.Reshaping means changing the shape of an array.
2.The shape of an array is the number of elements in each dimension.
3.By reshaping we can add or remove dimensions or change number of elements in each dimension.
#=========ReshapeFrom1-Dto2-D=========
#Q-Convert the following 1-D array with 12 elements into a 2-D array.
#The outer most dimension will have 4 arrays, each with 3 elements:
##arr.reshape(number of arrays, number of elements in the array)
#import numpy as np
#arr=np.array([1,2,3,4,5,6,7,8,9,10,11,12])
#newarr=arr.reshape(4,3)
#print('Originalarray:',arr)
#print('Newarrayis:',newarr)
##Output is
#Originalarray:[123456789101112]
#Newarrayis:[[123]
#[456]
#[789]
#[101112]]
#=========ReshapeFrom1-Dto3-D=========
#Q-Convert the following 1-D array with 12 elements in to a 3-D array.
#The outer most dimension will have 2 arrays that contains 3 arrays, each with 2 elements:
##arr.reshape(Number of array1, number of arrays in array1, number of elements in each array)
#import numpy as np
#arr=np.array([1,2,3,4,5,6,7,8,9,10,11,12])
#newarr=arr.reshape(2,3,2)
#print(newarr)
##Output is
#[[[12]
#[34]
#[56]]
#[[78]
#[910]
#[1112]]]
#Q-Example: Try converting 1D array with 8 elements to a 2D array with 3 elements in each dimension (will raise an error):
#import numpy as np
#arr=np.array([1,2,3,4,5,6,7,8])
#newarr=arr.reshape(3,3)
#print(newarr)
##Output=>ValueError:cannotreshapearrayofsize8intoshape(3,3)
#import numpy as np
#arr=np.array([1,2,3,4,5,6,7,8,9])
#newarr=arr.reshape(3,3)
#print(newarr)
##Output=>[[123]
#[456]
#[789]]
#importnumpyasnp
#arr=np.array([1,2,3,4,5,6,7,8])
#print('Dimensionoforiginalarrayis:',arr)
#import numpy as np
#arr=np.array([1,2,3,4,5,6,7,8])
#print(arr.reshape(2,2,-1))
#import numpy as np
#arr=np.array([1,2,3,4,5,6,7,8])
#print(arr.reshape(2,-1,2))
#import numpy as np
#arr=np.array([1,2,3,4,5,6,7,8])
#print(arr.reshape(-1,2,2))
#import numpy as np
#arr=np.array([1,2,3,4,5,6,7,8])
#print(arr.reshape(-1,-1,-1))
##Output = Value Error: can only specify one unknown dimension
##Flatteningthearrays
#Flattening array means converting a multi-dimensional array into a 1D array.
#We can use reshape(-1) to do this.
#importnumpyasnp
#arr=np.array([[1,2,3],[4,5,6]])
#newarr=arr.reshape(-1)
#print(newarr)
#Note: There are a lot of functions for changing the shapes of array in numpy flatten ,ravel and also for rearranging the elements rot90, flip, fliplr,
flipud etc. These fall under Intermediate to Advanced section of numpy.
#============17-09-24==============
#ITERATING ARRAY
# import numpy as np
# arr = np.array([1,2,3,4,5])
#
# for x in arr:
# print(x)
## Theory => Joining means putting contents of two or more arrays in a single array.
#In SQL we join tables based on a key, whereas in NumPy we join arrays by axes.
#We pass a sequence of arrays that we want to join to the concatenate() function, along with the axis. If axis is not explicitly passed, it is taken as 0.
# import numpy as np
# arr1= np.array([[1,3,4,2],[5,6,7,8]])
# arr2 = np.array([[9,10,11,12],[13,14,15,16]])
# result = np.concatenate((arr1,arr2),axis=1)
# print(result)
## output =>
#[[ 1 3 4 2 9 10 11 12]
#[ 5 6 7 8 13 14 15 16]]
#-----------------------------------------------
##Joining Arrays Using Stack Functions
##Stacking is same as concatenation, the only difference is that stacking is done along a new axis.
##We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking.
##We pass a sequence of arrays that we want to join to the stack() method along with the axis. If axis is not explicitly passed it is taken as 0.
# import numpy as np
# arr1= np.array([[1,3,4,2],[5,6,7,8]])
# arr2 = np.array([[9,10,11,12],[13,14,15,16]])
# result = np.stack((arr1,arr2),axis=1)
# print(result)
#-----------------------------------------------
## Stacking Along Rows
# NumPy provides a helper function: hstack() to stack along rows.
# import numpy as np
#----------------------------------------------
# Stacking Along Columns
# NumPy provides a helper function: vsstack() to stack along columns.
# import numpy as np
# arr1= np.array([[1,3,4,2],[5,6,7,8]])
# arr2 = np.array([[9,10,11,12],[13,14,15,16]])
# result = np.vstack((arr1,arr2))
# print(result)
# Output =>
# [[ 1 3 4 2]
# [ 5 6 7 8]
# [ 9 10 11 12]
# [13 14 15 16]]
#--------------------------------------------
# Stacking Along Height (depth)
# NumPy provides a helper function: dstack() to stack along height, which is the same as depth.
# import numpy as np
# arr1= np.array([[1,3,4,2],[5,6,7,8]])
# arr2 = np.array([[9,10,11,12],[13,14,15,16]])
# result = np.dstack((arr1,arr2))
# print(result)
#-----------------------------------------
#NUMPY Splitting array
# import numpy as np
# arr1 = np.array([1,2,3,4,5,6])
# arr = np.array_split(arr1,3)
# print(arr)
#----------------------------------------
#NUMPY Array Search
# You can search an array for a certain value, and return the indexes that get a match.
# To search an array, use the where() method.
# import numpy as np
# arr = np.array([1, 2, 3, 4, 5, 4, 4])
# x = np.where(arr == 4)
# print(x)
#-----------------------------------------------
##Find the indexes where the values are even:
# import numpy as np
# arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# x = np.where(arr%2 == 0)
# print(x)
#-------------------------------------------------
##Find the indexes where the values are odd:
# import numpy as np
# arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# x = np.where(arr%2 == 1)
# print(x)
#------------------------------------------------
# NumPy Sorting Arrays
# Sorting means putting elements in an ordered sequence.
# Ordered sequence is any sequence that has an order corresponding to elements, like numeric or alphabetical, ascending or descending.
# The NumPy ndarray object has a function called sort(), that will sort a specified array.
# import numpy as np
# arr = np.array([3, 2, 0, 1])
# print(np.sort(arr))
# import numpy as np
# import numpy as np
# arr = np.array([True, False, True])
# print(np.sort(arr))
# import numpy as np
# arr = np.array([[3, 2, 4], [5, 0, 1]])
# print(np.sort(arr))
#--------------------------------------------
# Filtering Arrays
# Getting some elements out of an existing array and creating a new array out of them is called filtering.
# import numpy as np
# arr = np.array([1,2,3,4,5,6,7,8])
# filter_array =arr > 4
# newarr = arr[filter_array]
# print(filter_array)
# print(newarr)
# [False False False False True True True True] ##Boolean index list
# [5 6 7 8]
#-------------------------------------------------
# Creating the Filter Array
# In the example above we hard-coded the True and False values, but the common use is to create a filter array based on conditions.
# import numpy as np
#
# arr = np.array([34,21,56,78,43,57,89,21])
#
# filter_arr =[]
#
# for element in arr:
#
# if element > 50:
# filter_arr.append(True)
# else:
# filter_arr.append(False)
#
# newarr = arr[filter_arr]
#
# print(newarr)
# print(filter_arr)
#-----------------------------------------
# Creating Filter Directly From Array
# import numpy as np
#
# arr = np.array([41, 42, 43, 44])
#
# filter_arr = arr > 42
#
# newarr = arr[filter_arr]
#
# print(filter_arr)
# print(newarr)
#---------------------------------------