0% found this document useful (0 votes)
34 views13 pages

Numpy Cheat Sheet

Uploaded by

abdoalsenaweabdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views13 pages

Numpy Cheat Sheet

Uploaded by

abdoalsenaweabdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

What is NumPy?

NumPy is a Python library used for working with arrays.

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 stands for Numerical Python.

--------------------------------------------------
------------

Why Use NumPy?


In Python we have lists that serve the purpose of arrays, but they are slow to process.

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.

--------------------------------------------------
------------

Why is NumPy Faster Than Lists?


NumPy arrays are stored at one continuous place in memory unlike lists, so processes can
access and manipulate them very efficiently.

This behavior is called locality of reference in computer science.

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 Arrays Are Indexed From 0

----------------------------------------------
----------------
- 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.

Install it using this command: C:\Users\Your Name> pip install numpy

-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__)

# print methods in the library


print(dir(np))

How to create array


myList = [1,2,3,4,5]

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).

# to create Multidimensional & 3d array


d_0 = np.array(5) # zero dimensional array

d_1 = np.array([5,10]) # vector array

#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

# to access element [row] [colum]


print(d_3[0]) #first row
print (d_3[0][0]) #first row & first colum
print (d_3[0][0][0]) #first row,colum & element
print (d_3[0, 0 ,0]) #first row,colum & element
print (d_3[0, 0 ,-1]) #fist row,colum & last element
print (d_3[0, -1 ,-1]) #fist row, last colum & element
print (d_3[-1, -1 ,-1]) #last row, colum & element

number of dimensions
print(d_0.ndim)
print(d_1.ndim)
print(d_2.ndim)
print(d_3.ndim)

Compare Data location & Type


print("element's myArray location memory")
print(id(myArray[0]))
print(id(myArray[1]))
print("element's myList location memory")
print(id(myList[0]))
print(id(myList[1]))

- 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

- Array deal with data


array_Int = np.array([True,2])
array_float = np.array([1.2,2])
array_str = np.array([1, 2, "A", "B", True, 10.50])
array_bollean = np.array([0,1,True,False,5])

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

- compare performance & memory usage


#preformance
import time
element = 90000

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])

-Data Types & Control Array


https://numpy.org/devdocs/user/basics.types.html
https://docs.scipy.org/doc/numpy/reference/
arrays.dtypes.html#specifying-and-constructing-data-types

'?' boolean

'b' (signed) byte

'B' unsigned byte

'i' (signed) integer

'u' unsigned integer

'f' floating-point

'c' complex-floating point


'm' timedelta

'M' datetime

'O' (Python) objects

'S', 'a' zero-terminated bytes (not recommended)

'U' Unicode string

'V' raw data (void)

# Show Array Data Type


my_array1 = np.array([1, 2, 3 ])
my_array2 = np.array([1.5, 20.15, 3.601])
my_array3 = np.array(["Ammar_gamal", "B", "Ahmed"])

print(my_array1.dtype)
print(my_array2.dtype)
print(my_array3.dtype)

Create Array With Specific Data Type


my_array4 = np.array([1, 2, 3], dtype= float)
my_array5 = np.array([1.5, 20.15, 3.601], dtype= "i") #"int","i", int
# my_array6 = np.array(["Ammar_gamal", "B", "Ahmed"],dtype = int)
error

print(my_array4.dtype)
print(my_array5.dtype)

print(my_array5)

Change Data Type Of Existing Array


my_array7 = np.array([0, 1, 2, 3, 0, 4])
print(my_array7.dtype)
print(my_array7)

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())

A_array2 = np.array([[[1, 2], [3, 4]],


[[5, 6], [7, 8]]])
print(A_array2.ndim)
print(A_array2.ravel())
x = A_array2.ravel()
print(x.ndim)

Shape & Reshape


my_array1 = np.array([1,
2,
3,
4])
print(my_array1.ndim)
print(my_array1.shape) #number of element in row

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)

my_array4 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])


print(my_array4.ndim)
print(my_array4.shape)

reshaped_array4 = my_array4.reshape(3, 4)
print(reshaped_array4.ndim)
print(reshaped_array4.shape)
print(reshaped_array4)

Joining NumPy Arrays


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.

arr1 = np.array([1,
2,
3])

arr2 = np.array([4, 5, 6])

arr = np.concatenate((arr1, arr2))


print(arr1.ndim)
print(arr)

# Example
# Join two 2-D arrays along rows (axis=1):
arr1 = np.array([[1, 2],
[3, 4]])

arr2 = np.array([[5, 6],


[7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr1.ndim)
print(arr)
print(arr.ndim)

Splitting NumPy Arrays


Splitting is reverse operation of Joining.

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.

arr = np.array([1, 2, 3, 4, 5, 6])

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.

To search an array, use the where() method.

arr = np.array([1, 2, 3, 4, 5, 4, 4])

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.

arr = np.array([3, 2, 0, 1])

print(np.sort(arr))

# Example
# Sort the array alphabetically:
arr = np.array(['banana', 'cherry', 'apple'])

print(np.sort(arr))

# Example
# Sort a boolean array:

arr = np.array([True, False, True])

print(np.sort(arr))

# Example
# Sort a 2-D array:

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.

In NumPy, you filter an array using a boolean index list.

A boolean index list is a list of booleans corresponding to indexes in the array.

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])

x = [True, False, True, False]

newarr = arr[x]

print(newarr)

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.

arr = np.array([41, 42, 43, 44])

# Create an empty list


filter_arr = []

# go through each element in arr


for element in arr:
# if the element is higher than 42, set the value to True, otherwise
False:
if element > 42:
filter_arr.append(True)
else:
filter_arr.append(False)

newarr = arr[filter_arr]

print(filter_arr)
print(newarr)

Creating Filter Directly From Array


The above example is quite a common task in NumPy and NumPy provides a nice way to tackle
it.

We can directly substitute the array instead of the iterable variable in our condition and it will
work just as we expect it to.

arr = np.array([41, 42, 43, 44])

filter_arr = arr > 42


newarr = arr[filter_arr]

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.

You might also like