Numpy Cheat Sheet
Numpy Cheat Sheet
It also has functions for working in domain of linear algebra, fourier transform, and matrices.
NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it
freely.
--------------------------------------------------
------------
NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of supporting functions that make
working with ndarray very easy.
Arrays are very frequently used in data science, where speed and resources are very important.
--------------------------------------------------
------------
This is the main reason why NumPy is faster than lists. Also it is optimized to work with latest
CPU architectures.
--------------------------------------------------
------------
Array
• The Items in The Array Have to Be Of The Same Type
• You Can Be Sure Whats The Storage Size Needed for The Array
----------------------------------------------
----------------
- NumPy On Github => https://github.com/numpy/numpy
--------------------------------------------------
------------
Installation of NumPy
If you have Python and PIP already installed on a system, then installation of NumPy is very easy.
-Import NumPy
Once NumPy is installed, import it in your applications by adding the import keyword: import
numpy
NumPy as np
NumPy is usually imported under the np alias.
import numpy as np
# Checking NumPy Version
# The version string is stored under __version__ attribute.
print(np.__version__)
myArray = np.array(myList)
print(myList)
print(myArray)
to know type
print(type(myList)) # class "list"
print(type(myArray)) # class "numpy.ndarray"
Accessing elements
print (myArray[0])
print (myList[0])
Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).
#Multidimensional
# 0 1
d_2 = np.array( [ [1, 2], # 0
[3, 4] ]) # 1
#3D
# 0 1 2 COLUM
d_3 = np.array([[[1,2],[5,3],[3,8]], # 0 Row
[[10,5],[13,8],[0,5]], # 1
[[1,4],[5,0],[1,1]]]) # 2
number of dimensions
print(d_0.ndim)
print(d_1.ndim)
print(d_2.ndim)
print(d_3.ndim)
- Note
- Arrar is Homogeneous : Can Contains The Same Type of
Objects
- List is Heterogeneous : Can Contains Different Types of
Objects.
my_list_of_data = [1, 2, "A", "B", True, 10.50]
my_array_of_data = np.array([1, 2, "A", "B", True, 10.50])
print(my_list_of_data)
print(my_array_of_data)
print(type(my_list_of_data[0])) # Class int
print(type(my_array_of_data[0])) # Class numpy.str
print(type(array_Int[0])) #True
print(type(array_float[1])) #2
print(type(array_str[5])) #10.5
print(type(array_bollean[3])) #False
list_start = time.time()
list1 = [(i) for i in range(element)]
print (list1)
print(f"list time : { time.time()-list_start}")
array_stat = time.time()
my_array = np.arange(element)
print (my_array)
print(f"array time : { time.time()-array_stat}")
#memory
import sys
m_array = np.arange(100)
print (m_array.itemsize) #number of bites
print (m_array.size) #number of elements in array
print(f"All Bytes :{m_array.itemsize*m_array.size} ")
m_list = range(100)
print(sys.getsizeof(m_list[1]))
print(len(m_list))
print(f"All Bytes : {sys.getsizeof(m_list[1])*len(m_list)}")
- Array Slicing
# Slicing => [Start: End :Steps] Not including end
s_array = np.array([1,2,3,4,5,6,7])
print(s_array.ndim)
print(s_array[5])
print(s_array[1:5])
print(s_array[:5])
print(s_array[::-1])
s_array2 =np.array([["A", "B", "X"], ["C", "D", "Y"], ["E", "F", "Z"],
["M", "N", "O"]])
print(s_array2.ndim)
print(s_array2[0])
print("="*20)
print(s_array2[:3])
print("="*20)
print(s_array2[1:4])
print("="*20)
print(s_array2[::-1])
print("="*20)
print(s_array2[:3,:2])
print("="*20)
print(s_array2[:2,:3])
print("="*20)
print(s_array2[:2,:1])
'?' boolean
'f' floating-point
'M' datetime
print(my_array1.dtype)
print(my_array2.dtype)
print(my_array3.dtype)
print(my_array4.dtype)
print(my_array5.dtype)
print(my_array5)
my_array7 = my_array7.astype('float')
print(my_array7.dtype)
print(my_array7)
my_array7 = my_array7.astype('bool')
print(my_array7.dtype)
print(my_array7)
Arithmetic Operations
• Addition
• Subtraction
• Multiplication
• Dividation
• min
• max
• sum
• ravel => Returns Flattened Array 1 Dimension With Same Type
note
Provided that the two matrices are of the same shape
A_array = np.array([1,2,3])
A_array1 = np.array([4,5,6])
print(A_array + A_array1)
print(A_array - A_array1)
print(A_array / A_array1)
print(A_array*A_array1)
print(A_array1.min())
print(A_array1.max())
print(A_array1.sum())
print("#" * 50)
my_array2 = np.array([
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
print(my_array2.ndim)
print(my_array2.shape)
print("#" * 50)
my_array3 = np.array([
[[1, 2, 3], [1, 2, 3]],
[[1, 2, 3], [1, 2, 3]]
])
print(my_array3.ndim)
print(my_array3.shape)
reshaped_array4 = my_array4.reshape(3, 4)
print(reshaped_array4.ndim)
print(reshaped_array4.shape)
print(reshaped_array4)
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.
arr1 = np.array([1,
2,
3])
# Example
# Join two 2-D arrays along rows (axis=1):
arr1 = np.array([[1, 2],
[3, 4]])
Joining merges multiple arrays into one and Splitting breaks one array into multiple.
We use array_split() for splitting arrays, we pass it the array we want to split and the number of
splits.
newarr = np.array_split(arr, 3)
print(newarr)
print(newarr[0])
print(newarr[1])
print(newarr[2])
Searching Arrays
You can search an array for a certain value, and return the indexes that get a match.
x = np.where(arr == 4)
print(x)
# Example
# Find the indexes where the values are even:
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
x = np.where(arr%2 == 0)
print(x)
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.
print(np.sort(arr))
# Example
# Sort the array alphabetically:
arr = np.array(['banana', 'cherry', 'apple'])
print(np.sort(arr))
# Example
# Sort a boolean array:
print(np.sort(arr))
# Example
# Sort a 2-D array:
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.
If the value at an index is True that element is contained in the filtered array, if the value at that
index is False that element is excluded from the filtered array.
# Example
# Create an array from the elements on index 0 and 2:
import numpy as np
arr = np.array([41, 42, 43, 44])
newarr = arr[x]
print(newarr)
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)
We can directly substitute the array instead of the iterable variable in our condition and it will
work just as we expect it to.
print(filter_arr)
print(newarr)
Conclusion
NumPy is an essential library for numerical computing in Python, offering powerful tools for
handling arrays, matrices, and various mathematical operations with high efficiency. This cheat
sheet covers the most commonly used features, like array creation, reshaping, mathematical
operations, and indexing, which are foundational to performing data manipulation and analysis.
By mastering these functions, you can streamline your data processing workflows, leverage
vectorized operations to improve performance, and lay a solid groundwork for more advanced
scientific computing and machine learning tasks.