MACHINE LEARNING LAB
Program 1: Write a python program to compute Central Tendency
Measures: Mean, Median, Mode Measure of Dispersion: Variance,
Standard Deviation
Solution:
import statistics as stats
def compute_measures(data):
# Central Tendency Measures
mean = [Link](data)
median = [Link](data)
mode = [Link](data)
# Measures of Dispersion
variance = [Link](data)
standard_deviation = [Link](data)
return mean, median, mode, variance, standard_deviation
# Example data
data = [1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9]
mean, median, mode, variance, std_dev = compute_measures(data)
print(f"Mean: {mean}")
print(f"Median: {median}")
print(f"Mode: {mode}")
print(f"Variance: {variance}")
print(f"Standard Deviation: {std_dev}")
Program 2: Study of Python Basic Libraries such as Statistics,
Math, Numpy and Scipy
Solution:
Python is widely used for scientific computing, data analysis, and
statistical applications. Some of the core libraries that facilitate these
tasks include statistics, math, NumPy, and SciPy. Here’s a brief overview
of each:
1. Statistics Library (statistics)
The statistics module in Python provides functions for statistical
calculations, making it easy to compute descriptive statistics such as
mean, median, variance, etc. This module is part of the Python Standard
Library, so you don't need to install any additional packages.
Key Functions:
o mean(): Calculates the arithmetic mean.
o median(): Finds the median (middle value).
o mode(): Identifies the most common data point.
o stdev(): Computes the standard deviation of a dataset.
o variance(): Calculates the variance.
Example Usage:
python
Copy code
import statistics as stats
data = [1, 2, 3, 4, 5, 5]
print("Mean:", [Link](data))
print("Median:", [Link](data))
print("Mode:", [Link](data))
Math Library (math)
The math module provides access to mathematical functions like
trigonometric functions, logarithms, and constants like pi and e. It’s also
part of the Python Standard Library.
Key Functions:
o sqrt(x): Square root of x.
o pow(x, y): x raised to the power y.
o sin(x), cos(x), tan(x): Trigonometric functions.
o log(x, base): Logarithm of x to the given base.
o Constants like [Link] and math.e.
Example Usage:
python
Copy code
import math
print("Square Root of 16:", [Link](16))
print("Cosine of 45 degrees:", [Link]([Link](45)))
print("Log base 10 of 100:", [Link](100, 10))
NumPy
NumPy is a powerful library for numerical computing in Python. It provides
support for large multi-dimensional arrays and matrices, along with a
collection of mathematical functions to operate on these arrays. NumPy is
a fundamental package for scientific computing in Python.
Key Features:
o ndarray: A fast and flexible multidimensional array object.
o Mathematical operations: sum(), mean(), dot() for matrix
multiplication, etc.
o Array manipulation: Reshaping, slicing, indexing, etc.
o Linear algebra, random number generation, Fourier
transforms.
Example Usage:
python
Copy code
import numpy as np
array = [Link]([1, 2, 3, 4])
print("Array:", array)
print("Mean of array:", [Link](array))
print("Dot product:", [Link](array, array))
SciPy
SciPy builds on NumPy by adding a collection of functions for scientific and
technical computing. It is particularly useful for optimization, integration,
interpolation, eigenvalue problems, and other advanced mathematical
computations.
Key Features:
o Optimization: [Link] (e.g., minimize())
o Integration: [Link] (e.g., quad())
o Interpolation: [Link] (e.g., interp1d())
o Signal Processing: [Link] (e.g., find_peaks())
o Linear Algebra: [Link] (e.g., inv() for matrix inversion)
o Statistical Functions: [Link] (e.g., [Link]() for the
cumulative distribution function of a normal distribution)
Example Usage:
python
Copy code
from scipy import optimize, integrate
# Example: Find the minimum of a quadratic function
def f(x):
return x**2 + 4*x + 4
result = [Link](f, 0)
print("Minimum of the function:", result.x)
# Example: Integrate a simple function
result = [Link](lambda x: x**2, 0, 1)
print("Integral of x^2 from 0 to 1:", result[0])
Program 3: Study of Python Libraries for ML application such as
Pandas and Matplotlib
Solution:
Pandas
Pandas is a powerful library for data manipulation and analysis. It provides
data structures like Series (1-dimensional) and DataFrame (2-dimensional)
that are essential for handling and analyzing structured data.
Key Features:
o Data Structures:
Series: A one-dimensional array with labels (similar to a
column in a table).
DataFrame: A two-dimensional table with labeled axes
(rows and columns), akin to a spreadsheet or SQL table.
o Data Manipulation:
Filtering and Selecting Data: Using loc[], iloc[], and
boolean indexing.
Handling Missing Data: Functions like dropna() and
fillna().
Merging and Joining: Using merge(), join(), and
concat().
Grouping and Aggregation: Using groupby() and
aggregation functions like mean(), sum(), etc.
Reshaping Data: Pivot tables, stacking/unstacking
data.
Example Usage:
python
Copy code
import pandas as pd
# Creating a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Score': [85, 90, 95]}
df = [Link](data)
# Selecting a column
print(df['Name'])
# Filtering data
print(df[df['Age'] > 28])
# Grouping data
print([Link]('Age').mean())
Matplotlib
Matplotlib is a plotting library that allows you to create static, animated,
and interactive visualizations in Python. It's particularly useful for creating
publication-quality figures in various formats and interactive environments
across platforms.
Key Features:
o Plotting:
plot(): Basic line plots.
scatter(): Scatter plots.
bar(): Bar charts.
hist(): Histograms.
pie(): Pie charts.
o Customization:
Labels, titles, legends, and annotations.
Adjusting axes, grid lines, and plot styles.
Multiple subplots in a single figure using subplot() or
subplots().
o Interactive Plots:
Integration with Jupyter notebooks and other
environments for interactive plotting.
o Saving Plots: Save figures in various formats (e.g., PNG,
PDF) using savefig().
Example Usage:
python
Copy code
import [Link] as plt
# Basic Line Plot
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
[Link](x, y)
[Link]("Basic Line Plot")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
# Scatter Plot
[Link](x, y, color='red')
[Link]("Scatter Plot")
[Link]()
# Histogram
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
[Link](data, bins=4)
[Link]("Histogram")
[Link]()
Integration in Machine Learning Workflows
In machine learning, Pandas and Matplotlib are often used together:
1. Data Preprocessing:
o Load datasets using Pandas (from CSV, Excel, SQL databases).
o Clean and prepare the data, handling missing values,
encoding categorical variables, etc.
o Perform exploratory data analysis (EDA) to understand the
data distribution, correlations, etc.
2. Data Visualization:
o Use Matplotlib to visualize the data and results from EDA, such
as plotting distributions, correlations, and trends.
o Visualize the performance of machine learning models, such
as plotting learning curves, confusion matrices, and feature
importances.
3. Model Development and Evaluation:
o Use Pandas to split the data into training and testing sets,
prepare features, and evaluate model performance.
These libraries are essential for any machine learning practitioner as they
provide the foundation for understanding and working with data, enabling
more effective and insightful model development.
Program 4: Write a Python program to implement Simple Linear
Regression
Solution:
Below is a Python program that implements Simple Linear Regression
using the scikit-learn library. Simple Linear Regression is a method to
predict the value of a dependent variable (Y) based on the value of an
independent variable (X).
Python Program: Simple Linear Regression
python
Copy code
# Import necessary libraries
import numpy as np
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
# Sample Data (X: Independent variable, Y: Dependent variable)
X = [Link]([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).reshape(-1, 1)
Y = [Link]([1.5, 2.3, 2.9, 3.8, 5.1, 5.9, 6.8, 7.5, 8.6, 9.7])
# Split the data into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2,
random_state=42)
# Create the Simple Linear Regression model
model = LinearRegression()
# Train the model using the training data
[Link](X_train, Y_train)
# Make predictions using the testing data
Y_pred = [Link](X_test)
# Print the coefficients
print(f"Coefficient (Slope): {model.coef_[0]}")
print(f"Intercept: {model.intercept_}")
# Evaluate the model
mse = mean_squared_error(Y_test, Y_pred)
r2 = r2_score(Y_test, Y_pred)
print(f"Mean Squared Error: {mse}")
print(f"R^2 Score: {r2}")
# Plot the regression line with the training data
[Link](X_train, Y_train, color='blue', label='Training Data')
[Link](X_train, [Link](X_train), color='red', label='Regression
Line')
[Link]("Simple Linear Regression")
[Link]("Independent Variable (X)")
[Link]("Dependent Variable (Y)")
[Link]()
[Link]()
# Plot the regression line with the testing data
[Link](X_test, Y_test, color='green', label='Testing Data')
[Link](X_test, Y_pred, color='red', label='Regression Line')
[Link]("Simple Linear Regression (Test Data)")
[Link]("Independent Variable (X)")
[Link]("Dependent Variable (Y)")
[Link]()
[Link]()
Explanation:
1. Libraries:
o numpy: For numerical operations.
o matplotlib: For plotting the data and regression line.
o scikit-learn: For implementing the linear regression model and
evaluating its performance.
2. Data:
o We define a simple dataset with one independent variable X
and a dependent variable Y.
3. Model Training:
o The data is split into training and testing sets using
train_test_split.
o A LinearRegression model is created and trained on the
training data (X_train, Y_train).
4. Model Evaluation:
o After training, the model predicts the values for the testing
data (X_test).
o The program calculates and prints the Mean Squared Error
(MSE) and R^2 Score, which are metrics to evaluate the
model's performance.
5. Visualization:
o The program plots the regression line along with the training
data and then with the testing data, showing how well the
model fits the data.
Output:
The program will print the coefficient (slope), intercept of the
regression line, and the model's performance metrics.
Two plots will be generated, showing the regression line with both
the training and testing data points.
This implementation of Simple Linear Regression can be used as a
foundation for more complex regression models and applications in
machine learning.
Program 5: Implementation of Multiple Linear Regression for
House Price Prediction using sklearn
Solution:
Python Program: Multiple Linear Regression for House Price
Prediction
python
Copy code
# Import necessary libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
import [Link] as plt
# Sample dataset: Features - ['Bedrooms', 'Size (sq ft)', 'Age'], Target -
'Price'
data = {
'Bedrooms': [2, 3, 3, 4, 4, 5, 3, 2, 5, 4],
'Size (sq ft)': [1500, 1600, 1700, 1875, 2150, 2400, 1600, 1290, 2450,
1950],
'Age': [10, 15, 7, 5, 12, 18, 10, 4, 20, 8],
'Price': [300000, 350000, 375000, 425000, 500000, 600000, 350000,
275000, 620000, 450000]
# Create a DataFrame from the dataset
df = [Link](data)
# Separate the features (independent variables) and target (dependent
variable)
X = df[['Bedrooms', 'Size (sq ft)', 'Age']]
Y = df['Price']
# Split the data into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2,
random_state=42)
# Create the Multiple Linear Regression model
model = LinearRegression()
# Train the model using the training data
[Link](X_train, Y_train)
# Make predictions using the testing data
Y_pred = [Link](X_test)
# Print the coefficients and intercept
print(f"Coefficients: {model.coef_}")
print(f"Intercept: {model.intercept_}")
# Evaluate the model
mse = mean_squared_error(Y_test, Y_pred)
r2 = r2_score(Y_test, Y_pred)
print(f"Mean Squared Error: {mse}")
print(f"R^2 Score: {r2}")
# Visualize the actual vs predicted prices
[Link](Y_test, Y_pred)
[Link]([min(Y_test), max(Y_test)], [min(Y_test), max(Y_test)], color='red')
# Line for perfect prediction
[Link]("Actual Prices")
[Link]("Predicted Prices")
[Link]("Actual vs Predicted Prices")
[Link]()
Explanation:
1. Libraries:
o pandas: For data manipulation and creating a DataFrame from
the dataset.
o scikit-learn: For splitting the data, training the Multiple Linear
Regression model, and evaluating its performance.
o matplotlib: For visualizing the actual vs. predicted house
prices.
2. Data:
o The dataset consists of three features: Bedrooms, Size (sq ft),
and Age, and a target variable Price.
3. Model Training:
o The features (X) and target (Y) are separated.
o The data is split into training and testing sets using
train_test_split.
o A LinearRegression model is created and trained on the
training data (X_train, Y_train).
4. Model Evaluation:
o The model predicts the prices for the testing data (X_test).
o The program calculates and prints the coefficients, intercept,
Mean Squared Error (MSE), and R^2 Score to evaluate the
model's performance.
5. Visualization:
o A scatter plot is generated to visualize the actual vs. predicted
house prices, with a line indicating perfect predictions.
Output:
Coefficients: The weights for each feature (e.g., the impact of each
feature on the house price).
Intercept: The constant term in the linear equation.
Mean Squared Error (MSE): A measure of the average squared
difference between the predicted and actual values.
R^2 Score: A metric indicating how well the model explains the
variance in the target variable.
Plot: A scatter plot comparing actual and predicted house prices.
Example Output:
plaintext
Copy code
Coefficients: [ 1000. 200. -2500.]
Intercept: 50000.0
Mean Squared Error: 1208333333.3333333
R^2 Score: 0.9248370244115391
The coefficients would indicate how much the price changes with
each feature. For instance, in this example, increasing the size by 1
sq ft increases the price by $200, and each additional bedroom adds
$1,000 to the price, while each additional year of age reduces the
price by $2,500.
The plot will show how close the predictions are to the actual prices.
This basic implementation can be expanded with real-world datasets,
feature engineering, and model tuning to create a robust house price
prediction model.