Unit - II ML Python Libraries
Python Libraries for Machine Learning
Introduction to Machine Learning Libraries
Machine Learning (ML) is a field of Artificial Intelligence that enables computers to learn patterns from data and make predictions or decisions without being explicitly programmed.
Python is the most popular language for ML because it has many powerful libraries. The most important ones are:
-
NumPy – for numerical computing
-
Pandas – for data handling and analysis
-
Matplotlib – for data visualization
-
Scikit-learn (sklearn) – for building ML models
1. NumPy (Numerical Python)
What is NumPy?
NumPy is a Python library used for working with arrays and numerical data.
It is faster and more efficient than Python lists.
NumPy is mainly used for:
-
Mathematical operations
-
Linear algebra
-
Handling large datasets
Importing NumPy
Creating Array: array()
An array is a collection of elements of the same type.
Output:
2D Array:
Accessing Array (Indexing)
Index starts from 0.
For 2D array:
Stacking & Splitting
stack()
Used to join arrays.
array_split()
Used to split arrays.
Math Functions
Statistics Functions
2. Pandas
What is Pandas?
Pandas is used for data manipulation and analysis.
It works with structured data like tables (rows and columns).
Two main structures:
-
Series (1D)
-
DataFrame (2D)
Import Pandas:
Series: Series()
A Series is a one-dimensional labeled array.
DataFrame: DataFrame()
A DataFrame is like an Excel table.
Read CSV File: read_csv()
CSV = Comma Separated Values.
Cleaning Empty Cells: dropna()
Removes rows with missing values.
Cleaning Wrong Data: drop()
Used to remove specific rows or columns.
Removing Duplicates: duplicated()
Pandas Plotting: plot()
3. Matplotlib
What is Matplotlib?
Matplotlib is used for data visualization (graphs and charts).
Import:
Line Plot: plot()
Labels: xlabel(), ylabel()
Grid: grid()
Bar Chart: bar()
Histogram: hist()
Subplot: subplot()
Pie Chart: pie()
Save Plot as PDF: savefig()
4. Scikit-learn (sklearn)
What is Scikit-learn?
Scikit-learn is the most popular library for Machine Learning algorithms.
It provides:
-
Classification
-
Regression
-
Clustering
-
Model evaluation
Key Concepts in Sklearn
-
Dataset – Collection of data
-
Features – Input variables
-
Target – Output variable
-
Model – Algorithm used for learning
-
Training – Teaching model with data
-
Testing – Checking model performance
-
Prediction – Output of the model
Steps to Build a Model in Sklearn
Step 1: Load Dataset
Step 2: Separate Features and Target
Step 3: Split Data
train_test_split()
80% → training
20% → testing
Step 4: Choose Model
Example: Linear Regression
Step 5: Train Model
Step 6: Make Prediction
Summary Table
| Library | Purpose |
|---|---|
| NumPy | Numerical operations |
| Pandas | Data handling |
| Matplotlib | Data visualization |
| Sklearn | Machine learning models |
Comments
Post a Comment