0% found this document useful (0 votes)
8 views

AI/ML python modules

This lab manual introduces basic Python libraries and syntax essential for machine learning (ML), emphasizing Python's ease of use, rich ecosystem, and community support. It covers setting up a Python environment, installing essential libraries like NumPy, Matplotlib, and Pandas, and provides guidance on data manipulation and visualization techniques. The document also includes references for further learning and tutorials.

Uploaded by

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

AI/ML python modules

This lab manual introduces basic Python libraries and syntax essential for machine learning (ML), emphasizing Python's ease of use, rich ecosystem, and community support. It covers setting up a Python environment, installing essential libraries like NumPy, Matplotlib, and Pandas, and provides guidance on data manipulation and visualization techniques. The document also includes references for further learning and tutorials.

Uploaded by

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

Lab Manual - 2: Introduction to Python for ML

Objective: Learn basic Python libraries and syntax relevant for ML.
Machine learning has revolutionized the way we approach data-driven problems, enabling
computers to learn from data and make predictions or decisions without explicit
programming. Python, with its rich ecosystem of libraries and tools, has become the de facto
language for implementing machine learning algorithms.

Why Python for Machine Learning?


Python has emerged as the preferred language for machine learning (ML) for several
compelling reasons:
1. Ease of Use and Readability: Python’s syntax is clean, concise, and resembles
pseudo-code, making it easy to learn and understand. This readability reduces the
cognitive load when writing and maintaining ML code, especially important in
complex algorithms.
2. Rich Ecosystem of Libraries: Python boasts a vast array of libraries and frameworks
specifically tailored for ML and data science. Libraries like NumPy, Pandas,
Matplotlib, and Scikit-Learn provide efficient tools for data manipulation, numerical
operations, visualization, and implementing ML algorithms seamlessly.
3. Community Support and Popularity: Python enjoys widespread adoption in the
data science and ML communities. Its popularity means there’s extensive community
support, abundant resources (tutorials, forums, libraries), and active development,
ensuring rapid advancements and continuous improvement.
4. Flexibility and Versatility: Python’s versatility allows ML engineers to work across
various domains, from data preprocessing to deploying models in production. It
integrates well with other languages and platforms, facilitating seamless integration
into existing systems.
5. State-of-the-Art Tools and Frameworks: Python serves as the foundation for
leading ML frameworks such as TensorFlow, PyTorch, and scikit-learn, which
offer robust capabilities for deep learning, neural networks, and traditional ML
models. These frameworks leverage Python’s strengths in simplicity and efficiency.

Setting up Python environment for Machine Learning


1. Install Python (Download Python): Go to python.org and download the latest
version of Python (currently Python 3.x). Installation: Follow the installation
instructions for your operating system (Windows, macOS, or Linux). Make sure to
check the option to add Python to PATH during installation.
2. Install Package Management Tools pip: Python’s package installer, pip, comes
bundled with Python installations from version 3.4 onwards. It’s essential for
installing and managing Python packages.
3. Setting up Virtual Environments (Optional but Recommended)
Step 1: Create your directory for Python Virtual Environments: Two Ways to Create a
Directory

1 By using GUI (Graphical User Interface):

1. Open the desired location in File Explorer.


2. Right-click on an empty space, select New > Folder.
3. Enter the folder name and press Enter

2 By using CMD (Command Prompt):

1. Open Command Prompt (Win + R, type cmd, press Enter).


2. Navigate to the desired location using the cd command:

cd path_to_location
mkdir FolderName

Step 2: Now in which ever directory you are, this line below will create a virtualenv there,
and here also you can name it anything.
python -m venv myenv

Step 3: Now if you are same directory then type,


myenv\Scripts\activate

You can explicitly specify your path too. Similarly like Linux you can deactivate it like
deactivate

Reference:https://www.geeksforgeeks.org/creating-python-virtual-environment-windows-
linux/?ref=ml_lbp

Install Essential Python Libraries for Machine Learning


Python Numpy
Numpy is a general-purpose array-processing package. It provides a high-performance
multidimensional array object, and tools for working with these arrays. It is the fundamental
package for scientific computing with Python. Besides its obvious scientific uses, Numpy can
also be used as an efficient multi-dimensional container of generic data.
Firstly, open your virtual environment, where install numpy, like as:
pip install numpy

Check Numpy library is working or not:


Create numpy object np , like as:
import numpy as np
1. Arrays in Numpy
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by
a tuple of positive integers. In Numpy, number of dimensions of the array is called rank
of the array. A tuple of integers giving the size of the array along each dimension is
known as shape of the array. An array class in Numpy is called as ndarray. Elements in
Numpy arrays are accessed by using square brackets and can be initialized by using
nested Python Lists.
Creating a Numpy Array : Arrays in Numpy can be created by multiple ways, with
various number of Ranks, defining the size of the Array. Arrays can also be created with
the use of various data types such as lists, tuples, etc. The type of the resultant array is
deduced from the type of the elements in the sequences. Note: Type of array can be
explicitly defined while creating the array.
2. Basic Array Operations
In numpy, arrays allow a wide range of operations which can be performed on a
particular array or a combination of Arrays. These operations include some basic
Mathematical operation as well as Unary and Binary operations.

3. Matrix manipulation in Python


In python matrix can be implemented as 2D list or 2D Array. Forming matrix from latter,
gives the additional functionalities for performing various operations in matrix. These
operations and array are defines in module “numpy“.
Operation on Matrix:
1. add() :- This function is used to perform element wise matrix addition.
2. subtract() :- This function is used to perform element wise matrix subtraction.
3. divide() :- This function is used to perform element wise matrix division.
4. multiply() :- This function is used to perform element wise matrix multiplication.
5. dot() :- This function is used to compute the matrix multiplication, rather than element
wise multiplication.
6. sqrt() :- This function is used to compute the square root of each element of matrix.
7. sum(x,axis) :- This function is used to add all the elements in matrix. Optional “axis”
argument computes the column sum if axis is 0 and row sum if axis is 1.
8. “T” :- This argument is used to transpose the specified matrix.
Using nested loops:
Approach:
 Define matrices A and B.
 Get the number of rows and columns of the matrices using the len() function.
 Initialize matrices C, D, and E with zeros using nested loops or list
comprehension.
 Use nested loops or list comprehension to perform the element-wise addition,
subtraction, and division of matrices.
 Print the resulting matrices C, D, and E.
References for NumPy Tutorial – Python Library
https://www.geeksforgeeks.org/numpy-tutorial/?ref=lbp

Python matplotlib
Matplotlib is a powerful and versatile open-source plotting library for Python, designed to
help users visualize data in a variety of formats.
pip install matplotlib
Key Features of Matplotlib
 Versatile Plotting: Create a wide variety of visualizations, including line plots,
scatter plots, bar charts, and histograms.
 Extensive Customization: Control every aspect of your plots, from colors and
markers to labels and annotations.
 Seamless Integration with NumPy: Effortlessly plot data arrays directly,
enhancing data manipulation capabilities.
 High-Quality Graphics: Generate publication-ready plots with precise control over
aesthetics.
 Cross-Platform Compatibility: Use Matplotlib on Windows, macOS, and Linux
without issues.
 Interactive Visualizations: Engage with your data dynamically through interactive
plotting features.
Example of a Plot in Matplotlib : Let’s create a simple line plot using Matplotlib,
showcasing the ease with which you can visualize data.

Output:
How you implement python matplotlib in your virtual environment:
import matplotlib.pyplot as plt
Run python in your virtual environment then write your code:

The second way to use Matplotlib in your virtual environment is to write your code in a
notebook, save it with a .py extension, then activate your virtual environment and run the
file by typing python filename.py.
Different Types of Plots in Matplotlib
Matplotlib offers a wide range of plot types to suit various data visualization needs. Here
are some of the most commonly used types of plots in Matplotlib:
1. Line Graph
2. Bar Chart
3. Histogram
4. Scatter Plot
5. Pie Chart
6. 3D Plot

Bar chart in Matplotlib


A bar plot or bar chart is a graph that represents the category of data with rectangular
bars with lengths and heights that is proportional to the values which they represent. The
bar plots can be plotted horizontally or vertically. A bar chart describes the comparisons
between the discrete categories. It can be created using the bar() method.
Scatter Plot in Matplotlib
Scatter plots are ideal for visualizing the relationship between two continuous variables.
We’ll see how scatter plots help identify patterns, correlations, or clusters within data
points.

Reference for matplotlib tutorial: https://www.geeksforgeeks.org/matplotlib-tutorial/


Pyhton Pandas
Pandas is a powerful data manipulation and analysis library for Python. It provides data
structures like series and dataframes to effectively easily clean, transform, and analyze
large datasets and integrates seamlessly with other python libraries, such as numPy and
matplotlib. It offers powerful functions for data transformation, aggregation, and
visualization, which are crucial for effective analysis.
pip install pandas

To use Pandas in your code, import it with:


import pandas as pd
1. Creating DataFrames
DataFrame is a two-dimensional table-like data structure with labeled rows and columns,
where each column can have a different data type (e.g., integers, strings, floats).

Outout:
In this example, a dictionary named data is created with keys representing column names
(Name, Age) and values as lists containing the respective data. pd.DataFrame() function
is then used to convert this dictionary into a DataFrame, which is stored in the variable df.
2. Reading CSV Files
CSV (Comma Separated Values) files are a common format for storing large datasets in
plain text. The Pandas library in Python provides, read_csv() function, to load these files
into a DataFrame. For our example we will use people.csv.

Output:

3. Data Manipulation in Python using Pandas


In Machine Learning, the model requires a dataset to operate, i.e. to train and test. But
data doesn’t come fully prepared and ready to use. There are discrepancies like Nan/
Null / NA values in many rows and columns. Sometimes the data set also contains some
of the rows and columns which are not even required in the operation of our model. In
such conditions, it requires proper cleaning and modification of the data set to make it an
efficient input for our model.
1. Adding data in DataFrame using Append Function
Next, for some reason we want to add a new student in the datagram, i.e you want to add
a new row to your existing data frame, that can be achieved by the following code
snippet. One important concept is that the “dataframe” object of Python, consists of rows
which are “series” objects instead, stack together to form a table. Hence adding a new
row means creating a new series object and appending it to the dataframe.

2. Getting Shape and information of the data


Let’s exact information of each column, i.e. what type of value it stores and how many of
them are unique. There are three support functions, .shape, .info() and .corr() which
output the shape of the table, information on rows and columns, and correlation between
numerical columns.

Output:
3. Dropping Columns from Data
Let’s drop a column from the data. We will use the drop function from the pandas. We
will keep axis = 1 for columns.

Output:
4. Dropping Rows from Data
Let’s try dropping a row from the dataset, for this, we will use drop function. We will
keep axis=0.

Output:

Reference for pandas tutorial : https://www.geeksforgeeks.org/pandas-tutorial/


Other References:
1. https://machinelearningmastery.com/machine-learning-in-python-step-by-step/
2. https://python-course.eu/machine-learning/
3. https://pythonprogramming.net/machine-learning-tutorial-python-introduction/
4. https://www.w3schools.com/python/python_ml_getting_started.asp
5. https://www.tutorialspoint.com/machine_learning_with_python/index.htm

You might also like