0% found this document useful (0 votes)
14 views35 pages

Essential Python Libraries Overview

Uploaded by

Babar Hussain
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)
14 views35 pages

Essential Python Libraries Overview

Uploaded by

Babar Hussain
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

Python Libraries

1. N UMPY
2. ME T PLOT LIB
3. S KL EA RN
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?
n 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.
Installation and import 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


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.
alias: In Python alias are an alternate name for referring to the same thing.
Create an alias with the as keyword while importing:
◦ import numpy as np
Now the NumPy package can be referred to as np instead of numpy.
Example
◦ import numpy as np
◦ arr = [Link]([1, 2, 3, 4, 5])
◦ print(arr)
Create a NumPy ndarray Object

NumPy is used to work with arrays. The array object in NumPy is called ndarray.
We can create a NumPy ndarray object by using the array() function.
Example:
◦ import numpy as np
◦ arr = [Link]([1, 2, 3, 4, 5])
◦ print(arr)
◦ print(type(arr))
Dimensions in Arrays

A dimension in arrays is one level of array depth (nested arrays). Nested array: are arrays that have
arrays as their elements.
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
Example
◦ Create a 0-D array with value 42
◦ import numpy as np
◦ arr = [Link](42)
◦ print(arr)
Dimensions in Arrays
1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
These are the most common and basic arrays.
Example
Create a 1-D array containing the values 1,2,3,4,5:
◦ import numpy as np
◦ arr = [Link]([1, 2, 3, 4, 5])
◦ print(arr)
Dimensions in Arrays
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
These are often used to represent matrix or 2nd order tensors.
NumPy has a whole sub module dedicated towards matrix operations called [Link]
Example
Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:
◦ import numpy as np
◦ arr = [Link]([[1, 2, 3], [4, 5, 6]])
◦ print(arr)
Access Elements of Array
import numpy as np import numpy as np
arr = [Link]([1, 2, 3, 4]) arr = [Link]([[1,2,3,4,5], [6,7,8,9,10]])
print(arr[2]) print('2nd element on 1st row: ', arr[0, 1])
print(arr[2] + arr[3])
Assignment
Try the following function of numpy and show the outputs
1. NumPy Array Slicing
2. NumPy Array Shape
3. NumPy Joining Array
4. NumPy Splitting Array
5. NumPy Sorting Arrays
What is Matplotlib?
Matplotlib is a low level graph plotting library in python that serves as a visualization utility.
Matplotlib was created by John D. Hunter.
Matplotlib is open source and we can use it freely.
Matplotlib is mostly written in python, a few segments are written in C, Objective-C and Javascript
for Platform compatibility.
Installation and Importof Matplotlib

Install it using this command:

◦ C:\Users\Your Name>pip install matplotlib

Once Matplotlib is installed, import it in your applications by adding the import module statement:

◦ import matplotlib
Matplotlib Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually
imported under the plt alias:
import [Link] as plt
Now the Pyplot package can be referred to as plt.
Example:
Draw a line in a diagram from position (0,0) to position (6,250):
◦ import [Link] as plt
◦ import numpy as np
◦ xpoints = [Link]([0, 6])
◦ ypoints = [Link]([0, 250])
◦ [Link](xpoints, ypoints)
◦ [Link]()
Matplotlib Pyplot
Without Line
Example:
Draw two points from position (0,0) to position (6,250):
◦ import [Link] as plt
◦ import numpy as np
◦ xpoints = [Link]([0, 6])
◦ ypoints = [Link]([0, 250])
◦ [Link](xpoints, ypoints, ‘o’)
◦ [Link]()
Matplotlib Labels and Title
With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.
Example
Add labels to the x- and y-axis:
◦ import numpy as np
◦ import [Link] as plt
◦ x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
◦ y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
◦ [Link](x, y)
◦ [Link]("Sports Watch Data")
◦ [Link]("Average Pulse")
◦ [Link]("Calorie Burnage")
◦ [Link]()
Example
Set font properties for the title and labels:
◦ import numpy as np
◦ import [Link] as plt
◦ x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
◦ y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
◦ font1 = {'family':'serif','color':'blue','size':20}
◦ font2 = {'family':'serif','color':'darkred','size':15}
◦ [Link]("Sports Watch Data", fontdict = font1, loc = ‘center')
◦ [Link]("Average Pulse", fontdict = font2)
◦ [Link]("Calorie Burnage", fontdict = font2)

◦ [Link](x, y)
◦ [Link]()
Matplotlib Scatter
Creating Scatter Plots
With Pyplot, you can use the scatter() function to draw a scatter plot.
The scatter() function plots one dot for each observation. It needs two arrays of the same length,
one for the values of the x-axis, and one for values on the y-axis:
import [Link] as plt
import numpy as np

x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])

[Link](x, y)
[Link]()
Example
Example
Draw two plots on the same figure:
import [Link] as plt
import numpy as np
#day one, the age and speed of 13 cars:
x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])
[Link](x, y)
#day two, the age and speed of 15 cars:
x = [Link]([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y =
[Link]([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
[Link](x, y)
[Link]()
Matplotlib Adding Grid Lines
With Pyplot, you can use the grid() function to add grid lines to the plot.

Example: Add grid lines to the plot:

import numpy as np

import [Link] as plt

x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])

y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

[Link]("Sports Watch Data")

[Link]("Average Pulse")

[Link]("Calorie Burnage")

[Link](x, y)

[Link](True)

[Link]()
What is Scikit-learn (sklearn) ?
 Building machine learning models from scratch can be complex and time-consuming.
 However with the right tools and frameworks this process can become significantly easier.
 Scikit-learn is one such tool that makes machine learning model creation easy.
 It provides user-friendly tools for task.

 Scikit-learn is a open-source Python library that include wide range of machine learning
models, pre-processing, cross-validation and visualization algorithms and all accessible with
simple interface.
 Its simplicity and versatility make it a better choice for both beginners and advanced data
scientists to build and implement machine learning models like Classification, Regression,
Clustering and many more.
Installation of Scikit- learn
The latest version of Scikit-learn is 1.1 and it requires Python 3.8 or newer.
Scikit-learn requires NumPy SciPy as its dependencies.
Before installing scikit-learn, ensure that you have NumPy and SciPy installed. Once you have a
working installation of NumPy and SciPy, the easiest way to install scikit-learn is using pip:
!pip install -U scikit-learn
Getting Start with Scikit- learn
Step 1: Load a Dataset
A dataset is nothing but a collection of data. A dataset generally has two main components:
Features: They are also known as predictors, inputs or attributes. These are simply the variables
of our data. They can be more than one and hence represented by a feature matrix ('x' is a
common notation to represent feature matrix). A list of all the feature names is termed feature
names.
Response: They are also known as the target, label or output. This is the output variable
depending on the feature variables. We generally have a single output variable column and it is
represented by a response vector ( 'y' is a common notation to represent response vector). All
the possible values taken by a response vector are termed target names.
Getting Start with Scikit- learn
1. Loading exemplar dataset: Scikit-learn comes with few loaded example datasets like the iris
and digits datasets for classification and the boston house prices dataset for regression.

Given below is an example of how we can load an exemplar dataset:


# load the iris dataset as an example
from [Link] import load_iris
iris = load_iris()
# store the feature matrix (X) and response vector (y)
X = [Link]
y = [Link]
# store the feature and target names
feature_names = iris.feature_names
target_names = iris.target_names
# printing features and target names of our dataset
print("Feature names:", feature_names)
print("Target names:", target_names)
# X and y are numpy arrays
print("\nType of X is:", type(X))
# printing first 5 input rows
print("\nFirst 5 rows of X:\n", X[:5])
Getting Start with Scikit- learn
load_iris(): loads the Iris dataset into the variable iris.
Features and Targets: X contains the input data (features like petal length, width, etc) and y
contains the target values (species of the iris flower).
Names: feature_names and target_names provide the names of the features and the target
labels respectively.
Inspecting Data: We print the feature names and target names check the type of X and display
the first 5 rows of the feature data to understand the structure.
Step 2: Splitting the Dataset
In machine learning working with large datasets can be computationally expensive. For this we
split the data into two parts: training data and testing data. This approach helps reduce
computational cost and also helps to evaluate model's performance and accuracy on unseen
data.
 Split the dataset into two pieces: a training set and a testing set.
 Train the model on the training set.
 Test the model on the testing set and evaluate how well our model did.
1. Load the Iris Dataset
◦ from [Link] import load_iris
◦ iris = load_iris()
◦ X = [Link]
◦ y = [Link]

2. Import and Use train_test_split to Split the Data


◦ from sklearn.model_selection import train_test_split
◦ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)
In this step we import train_test_split from sklearn.model_selection. This function splits the
dataset into two parts: a training set and a testing set.
X_train and y_train: These are the features and target values used for training the model.
X_test and y_test: These are the features and target values used for testing the model after it
has been trained.
test_size=0.4: 40% of the data is allocated to the testing set while the remaining 60% is used for
training.
random_state=1: This ensures that the split is consistent, meaning you'll get the same random
split every time you run the code.
3. Check the Shapes of the Split Data
When splitting data into training and testing sets verifying the shape ensures that both sets have
correct proportions of data avoiding any potential errors in model evaluation or training.
◦ print("X_train Shape:", X_train.shape)
◦ print("X_test Shape:", X_test.shape)
◦ print("Y_train Shape:", y_train.shape)
◦ print("Y_test Shape:", y_test.shape)

The number of rows in X_train should be 60% of the original dataset, and the number of rows in
X_test should be 40%.
y_train should have the same number of rows as X_train, and y_test should have the same
number of rows as X_test.
Step 4: Training the Model
Now it's time to train our models using our dataset. Scikit-learn provides a wide range of machine
learning algorithms that have a unified/consistent interface for fitting, predicting accuracy, etc. The
example given below uses Logistic Regression.
Note: We will not go into the details of how the algorithm works as we are interested in
understanding its implementation only.
Training Using Logistic Regression:
◦ from sklearn.linear_model import LogisticRegression
◦ log_reg = LogisticRegression(max_iter=200)
◦ log_reg.fit(X_train, y_train)
We create a logistic regression classifier object using log_reg = LogisticRegression(max_iter=200).
The classifier is trained using the X_train data and the corresponding response vector y_train.
This is done by calling log_reg.fit(X_train, y_train), where the logistic regression model adjusts its
weights.
Making Predictions:
y_pred = log_reg.predict(X_test)
Now, we need to test our classifier on the X_test data, log_reg.predict method is used for this
purpose. It returns the predicted response vector y_pred.
Testing Accuracy:
from sklearn import metrics
print("Logistic Regression model accuracy:", metrics.accuracy_score(y_test, y_pred))
Now, we are interested in finding the accuracy of our model by comparing y_test and y_pred.
This is done using the metrics module's method accuracy_score
Consider the case when you want your model to make predictions on new sample data. Then
the sample input can simply be passed in the same way as we pass any feature matrix.
Here we used it as sample = [[3, 5, 4, 2], [2, 3, 5, 4]]
Example
sample = [[3, 5, 4, 2], [2, 3, 5, 4]]
preds = log_reg.predict(sample)
pred_species = [iris.target_names[p] for p in preds]
print("Predictions:", pred_species)
Features of Scikit-learn
Pre-built functions: It offers ready to use functions for common tasks like data preprocessing,
model training and prediction eliminating the need to write algorithms from scratch.
Efficient model evaluation: It includes tools for model evaluation such as cross-validation and
performance metrics making it easy to assess and improve model accuracy.
Variety of algorithms: It provides a wide range of algorithms for classification, regression,
clustering and more like support vector machines, random forests and k-means.
Integration with scientific libraries: Built on top of NumPy, SciPy and matplotlib making it easy to
integrate with other libraries for data analysis.
Benefits of using Scikit-learn Libraries
Consistent and simple interface: Scikit-learn provides a uniform API across different models
making it easy to switch between algorithms without having to learn a new syntax.
Extensive model tuning options: It offers a wide range of tuning parameters and grid search
tools to fine-tune models for better performance.
Active community and support: The library has a large, engaged community ensuring regular
updates, bug fixes and a wealth of user-contributed resources like forums, blogs and Q&A sites.

You might also like