Welcome to NumPy New Course:
In this course we will learn NumPy from the absolute basics to the advanced level after reading out all the tutorials you will have a full understanding of how NumPy is used in different fields like Artificial Intelligence, Data Science, Machine Learning, or some other related field.
let's get started.
Video Tutorial (Optional)
NumPy
NumPy stands for 'Numerical Python'
def: "Numpy is the core library for scientific computing in python"
Numpy library is used for different types of purposes that could be:
- Used in Data Science
- Used in Machine Learning
- Automation
- Deep Learning etc.
Let's Start to study NumPy in details
How to install NumPy In your system:
If you want to install NumPy in your system write the following code in your command prompt.
pip install numpy
But I will prefer to download Anaconda in your system that will automatically install NumPy and some other libraries of python in your system
Install Anaconda:
If you don't know how to install Anaconda Click Here to watch the tutorial.
Once you have downloaded Anaconda open Jupyter Notebook in any of your browsers and start code with us.
Once everything is done you need to import NumPy in your notebook as follow
import numpy
But if you want to call NumPy with the short name then write the following code
import numpy as np
you will use the short name for NumPy as np whenever you call NumPy
Before starting to work with NumPy methods and functions lets understand different types of arrays of metrics used by NumPy:
NumPy uses different types of array as follow:
Numpy Array Types
There are three types of NumPy arrays:
- One dimensional NumPy Array.
- Two-dimensional NumPy Array.
- Three-dimensional NumPy Array.
1. One Dimensional:
In one dimensional there is only one row but having different columns
i-e: array = [1,2,3,4]
in this case, we have only one row and 4 columns
2. Two Dimensional:
In two dimensional there is multiple rows and multiple columns
i-e: array = [[1,2,3],
[4,5,6]]
in this case, we have two rows and three columns
3. Three Dimensional
In three dimensional many arrays are having many axes
But in this course, we will only focus on 1D and 2D arrays while you don't need to play with 3D or 4D arrays.
Creating Arrays:
Create 1D Array:
If you want to create a 1D array with one row and 4 columns in NumPy use the following code
np.array([1,2,3,4])
Create 2D Array:
If you want to create a 2D array with 2 rows and 3 columns use the following code
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
Create an array of Zeros:
If you want to create an array with 3 rows and 4 columns while all the elements are zeros use the following:
zeros = np.zeros((3,4))
Use Arrange Method to create an array:
if you want to create an array that is evenly spaced through arrange method use the following code
arr = np.arange(10,100,10)
The above code will show you numbers in a list starting from 10 up to 90 because the last one is always skipping if you want to print up to 100 use 110 instead of 100
Create an array with random numbers
if you want to create an array with some random numbers use the following code where the (2,2) show's rows and columns in your array
random_arr = np.random.random((2,2))
Create an empty array
If you want to create an empty array use the following code
empty = np.empty((4,5))
Data Types
NumPy array uses multiple types of data as some of them are as follow:
1.
integersNumPy array used integers values i-e 1,2,,3,4,55,33,22,33
2. floating numbers
NumPy array used floating-point numbers which have the decimal point in between
i-e 33.22, 55.33, 66.777, 0.1233
3. boolean type true or false
boolean types are two states either True or False
4. python fixed string
string are characters stored in the NumPy array as like array['nomi', 'abcd']
Inspecting NumPy Array
Array Shape
If you want to check the shape of your array and what type of dimension your array exist you can check by array name and then shape function as follow:
a.shape
Length of Array
If you want to check the length of your array and how many rows and columns your array exist you can do as follow:
len(a)
Number of Array Dimensions
If you want to find the type of your array that is it 2d or 1d you can do it by the following method.
a.ndim
find Number of Array elements
if you want to find several array elements in your array you can use the following code:a.size
Find Name of data type
if you want to find what type of data your array exist you can check that as follow:a.dtype
Asking For Help
if you want get information about all the NumPy tools you can do that just as follow:np.info(a)
give your array name inside parenthesis and you will know all the information about your array
Array Mathematics
Arithmetic Operations
We can do arithmetic operations on our array such as:- Subtraction
- Addition
- Multiplication
- Division
- Exponentiation
- Square root
- Print sines of an array
- Element-wise cosine
- Element-wise natural logarithm
Subtraction
If you want to subtract one array or matric from another array you can do that in two ways
first is just write as follows if you have two arrays.
array1 - array2
The second method is
np.subtract(array1 - array2)
Addition
The addition of two arrays is also the same as subtraction as follow: array1 + array2 the
second method is
np.add(array1 , array2)
Multiplication
if you want to do multiplication if two matrics follow the same method as you already know a * b
or
np.multiply(a,b)
Division
also, the division will be the same as: a/b
or
np.divide(a,b)
Exponentiation
if you want to take the exponent of your array use as follow: np.exp(a)
This will take the exponent of each element in your array as like:
a = np.array([[3,4,5],[5,6,7]])
np.exp(a)
output:
array([[ 20.08553692, 54.59815003, 148.4131591 ],
[ 148.4131591 , 403.42879349, 1096.63315843]])
Square root
Also in the same way you can take the square root of each element in your array as like:np.sqrt(array)
Print sines and cosine of an array
also, you can print the sines and cosines of an array as:np.sin(a)
np.cos(a)
it will take the sin and cosine of each element in your array
Natural logarithm
if you want to take the log of each element in your array do as follow: np.log(a)
Comparison
if you want to compare two arrays you can do as follow: a == b
output: array([[False, True, True], [False, False, False]]
it will give you the result in bool type if two elements at the same index are equal to each other it will show you True otherwise False
also
if you want to compare your array elements with a specific element then you can do as follows
a > 2
for best practice create an array then do a comparison as above
Array-wise comparison
if you want to compare two arrays you can do that as follow by writing np.array_equal and then put your two arrays between parenthesis.np.array_equal(array1, array2)
Aggregate Functions
Array-wise sum
if you want to print the sum of your array you can do as follow: array.sum()
it will find the sum of all elements in your array
Find Minimum value in the array
if you want to find minimum value in your array you can do as follow: array_name.min()
Find Maximum value in the array
if you want to find maximum value in your array you can do that as follow: array_name.max()
Mean
if you want to find the mean or average of an array of elements then you can do that as follow: array_name.mean()
Sort an array
if you want to sort an array in ascending order or descending order you can do that as follow array_name.sort()
Subsetting, Slicing, Indexing
Select the element any index
if you want to select an element from your array through indexing you can do that as write your array name and then inside large brackets specify the index as follow:a[2]
in this case, we are collecting elements from the third index as we know that in python index always starts from zero.
Select the element from any row at any column
if you want to collect a specific element from the array at any row or column just specify the column and row index as followarray_name[1,2]
in this case, we are collecting an element from the second row and third column
Slicing
you also can dod slicing as follow:array_name[:1]
this will select all items at row 0
Reversed an array
you can also be doing reverse indexing for example if you want to get the last element in your array you can get that by -1 or if second last then -2 as like below:array_name[-1]
this will give you the last element in your array
Array Manipulation
Transposing Array
if you want to take the transpose of an array transpose means converting rows into columns or columns into rows you can get the transpose as follows:np.transpose(array_name)
this will change rows into columns and columns into rows
Append items to an array
you can also add or remove items in your array if you want to add or append an item you can do that as follow: np.append(a,b)
Insert items in an array
you can also insert items into your array as follow: np.insert(array_name, 1,5)
this will add 1, and 5, into your array
Delete items from an array
also, you can delete items from your array through indexing as follow: np.delete(array_name,[1])
till now you have gain everything about NumPY which is necessary for data science and machine learning projects if you want to know more about NumPy you can read the NumPy documentation here
Thanks and good luck from Nomi
No comments:
Post a Comment