practical file machine learning
practical file machine learning
AIM:
To install Python on a computer system and verify its successful setup through command-line and
IDLE interaction.
Theory:
Why Python?
Requirement:
Procedure:
1
o From the Start Menu or Applications folder, open IDLE (Python’s integrated
development environment).
o Type a simple command like print("Hello, World!") and run it.
Observations:
Result:
Python was successfully installed on the system. Both command-line and IDLE interaction confirmed
that Python is working correctly.
2
Practical -2
Aim
To perform basic array operations like creation, addition, multiplication, slicing, and reshaping.
Requirements:
bash
CopyEdit
pip install numpy
# Print arrays
print("Array a:", a)
print("Array b:", b)
OUTPUT:
Array a: [1 2 3]
Array b: [4 5 6]
a + b = [5 7 9]
a * b = [ 4 10 18]
3x3 Array:
[[0 1 2]
[3 4 5]
[6 7 8]]
First row: [0 1 2]
3
Second column: [1 4 7]
Transposed Array:
[[0 3 6]
[1 4 7]
[2 5 8]]
4
Practical-3
Aim
To perform basic and advanced statistical analysis on a dataset using Python, including:
Required Tools
Python 3.x
Libraries:
o pandas – for data handling
o numpy – for numerical operations
o scipy – for statistical testing
o matplotlib / seaborn – for visualization (optional)
Theory:
Descriptive Statistics
Hypothesis Testing
🔷 Python Code
import numpy as np
5
import pandas as pd
from scipy import stats
# ---------------------
# Descriptive Statistics
# ---------------------
mean_B = np.mean(group_B)
median_B = np.median(group_B)
# Five-number summary
summary_A = {
'Min': np.min(group_A),
'Q1': np.percentile(group_A, 25),
'Median': np.median(group_A),
'Q3': np.percentile(group_A, 75),
'Max': np.max(group_A)
}
summary_B = {
'Min': np.min(group_B),
'Q1': np.percentile(group_B, 25),
'Median': np.median(group_B),
'Q3': np.percentile(group_B, 75),
'Max': np.max(group_B)
}
# ---------------------
# Hypothesis Testing
# ---------------------
# Display Results
results = {
'Group_A': {'Mean': mean_A, 'Median': median_A, '5-Number Summary':
summary_A},
'Group_B': {'Mean': mean_B, 'Median': median_B, '5-Number Summary':
summary_B},
't-test': {'t-statistic': t_stat, 'p-value': t_pval},
'Mann-Whitney U': {'U-statistic': u_stat, 'p-value': u_pval}
}
6
results
Results:
Descriptive Statistics:
5-number Min: 80, Q1: 87, Median: 89.5, Q3: 92.5, Min: 75, Q1: 77.25, Median: 79.5, Q3:
summary Max: 100 81.75, Max: 85
Since p-value < 0.05 for both tests, the difference in scores between Group A and Group B is
statistically significant.
Parametric and non-parametric tests agree, strengthening our confidence.
7
Practical-4
Aim:
To implement the ID3 Decision Tree algorithm in Python using a dataset (weather11.csv)
and print the tree in a readable hierarchical format.
Python 3.0
pandas
math
collections
Dataset Used:
Filename: weather11.csv
Attributes: Outlook, Temperature, Humidity, Wind, PlayTennis
Target Variable: PlayTennis (Yes/No)
1. Choosing the best attribute to split the data based on a metric called Information
Gain.
2. Recursively splitting the dataset on the best attributes.
3. Stopping when:
o All data belongs to a single class.
o There are no more attributes to split.
Theory:
It’s a supervised learning algorithm used to build a decision tree for classification tasks, especially
when your data has categorical features (like "Sunny", "Rain", "Yes", "No").
import pandas as pd
import math
8
# Load dataset
df = pd.read_csv(r'C:\Users\hp\OneDrive\Desktop\weather11.csv')
values = data[target_attr].unique()
entropy = 0
return entropy
vals = data[split_attr].unique()
subset_entropy = 0
# ID3 Algorithm
9
if len(data[target_attr].unique()) == 1:
return data[target_attr].iloc[0]
if not features:
return Counter(data[target_attr]).most_common(1)[0][0]
tree[best_feature][val] = subtree
return tree
# Prediction function
return tree
attribute = next(iter(tree))
value = instance[attribute]
if value in tree[attribute]:
10
return predict(tree[attribute][value], instance)
else:
if isinstance(tree, dict):
print(f"{indent}{attr} = {val}")
else:
print(f"{indent}→ {tree}")
target = df.columns[-1]
features = list(df.columns[:-1])
print("\nDecision Tree:\n")
print_tree(decision_tree)
# Generate predictions
y_true = df[target]
11
y_pred = df.apply(lambda row: predict(decision_tree, row), axis=1)
print("\nConfusion Matrix:\n")
print(confusion_matrix(y_true, y_pred))
print("\nClassification Report:\n")
print(classification_report(y_true, y_pred))
Result:
Decision Tree:
Outlook = Sunny
Humidity = High
→ No
Humidity = Normal
→ Yes
Outlook = Overcast
→ Yes
Outlook = Rain
Wind = Strong
→ No
Wind = Weak
→ Yes
Confusion Matrix:
[[5 0]
[0 9]]
12
Classification Report:
accuracy 1.00 14
13
Practical-5
Aim
To implement Simple Linear Regression using Python to predict the dependent variable (Y) based on
one independent variable (X).
Required Tools
Python 3.x
Jupyter Notebook or any Python IDE
Libraries:
o numpy
o pandas
o matplotlib
o sklearn (scikit-learn)
bash
CopyEdit
pip install numpy pandas matplotlib scikit-learn
🔷 Theory
Linear Regression is a supervised machine learning algorithm used for predicting a continuous
dependent variable (Y) based on the value of an independent variable (X).
It tries to fit a linear relationship in the form:
Y=mX+cY = mX + cY=mX+c
Where:
Python Code
14
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Predictions
y_pred = model.predict(X_test)
# Results
print("Slope (m):", model.coef_[0])
print("Intercept (c):", model.intercept_)
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R-squared Score:", r2_score(y_test, y_pred))
# Plotting
plt.scatter(X, y, color='blue', label='Actual Data')
plt.plot(X, model.predict(X), color='red', label='Regression Line')
plt.xlabel('Hours Studied')
plt.ylabel('Marks Obtained')
plt.title('Simple Linear Regression')
plt.legend()
plt.show()
Sample Output:
yaml
CopyEdit
Slope (m): 5.0
Intercept (c): 30.0
Mean Squared Error: 0.0
R-squared Score: 1.0
15
Explanation:
Practical -6
Aim:
To build and train a Convolutional Neural Network using TensorFlow and Keras for
classifying handwritten digits from the MNIST dataset.
Theory:
16
automatically and adaptively learn spatial hierarchies of features through backpropagation by
using multiple building blocks, such as:
The MNIST dataset contains 70,000 images (28x28 grayscale) of handwritten digits (0–9),
divided into 60,000 training and 10,000 test images.
Requirements:
Python 3.x
TensorFlow (install via pip install tensorflow)
Keras (included with TensorFlow 2.x)
NumPy and Matplotlib (for data and visualization)
Code:
import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
17
# Train the model
history = model.fit(x_train, y_train, epochs=5, validation_data=(x_test,
y_test))
# Plot accuracy
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Training vs Validation Accuracy')
plt.show()
Output:
Epoch 1/5
1875/1875 [==============================] - 13s 7ms/step - loss: 0.1392 -
accuracy: 0.9583 - val_loss: 0.0448 - val_accuracy: 0.9856
Epoch 2/5
1875/1875 [==============================] - 12s 6ms/step - loss: 0.0452 -
accuracy: 0.9862 - val_loss: 0.0325 - val_accuracy: 0.9893
Epoch 3/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.0307 -
accuracy: 0.9906 - val_loss: 0.0332 - val_accuracy: 0.9896
Epoch 4/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.0213 -
accuracy: 0.9933 - val_loss: 0.0304 - val_accuracy: 0.9904
Epoch 5/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.0165 -
accuracy: 0.9948 - val_loss: 0.0315 - val_accuracy: 0.9906
313/313 - 1s - loss: 0.0315 - accuracy: 0.9906
18
Practical-7
Aim:
Theory:
Support Vector Machine (SVM) is a supervised machine learning algorithm used for
classification and regression tasks. It works by finding the optimal hyperplane that best
separates different classes in the feature space. For non-linear classification problems, SVM
uses kernel tricks like RBF or polynomial kernels to project data into higher dimensions.
Tools Required:
Python 3.x
Jupyter Notebook / Google Colab / any IDE
Libraries:
o pandas
o scikit-learn
o matplotlib
o seaborn
Dataset Used:
Implements an SVM model using the Iris dataset to classify the first two features and visualizes the
decision boundary.
Code:
import numpy as np
19
# Load the Iris dataset
iris = datasets.load_iris()
y = iris.target
svm_model.fit(X_train, y_train)
y_pred = svm_model.predict(X_test)
# Print accuracy
20
h = .02 # Step size in the mesh
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.show()
plot_decision_boundary(svm_model, X, y)
output:
Classification Report:
21
0 1.00 1.00 1.00 10
accuracy 0.90 30
Hyperplane:
Practical-10
22
Aim:
To implement association rule mining using the Apriori algorithm in Python to find frequent
itemsets and generate strong association rules from transactional data.
Theory:
Association rule mining is a data mining technique used to discover interesting relations
between variables in large datasets. It is widely used in market basket analysis to identify
products that frequently co-occur in transactions.
Key terms:
The Apriori algorithm identifies frequent itemsets by exploiting the property that all subsets
of a frequent itemset must also be frequent.
Requirements:
Python 3.x
pandas for data manipulation
mlxtend for the Apriori and association rule functions
(Install with pip install mlxtend)
Code:
python
CopyEdit
import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules
23
# Convert dataset to one-hot encoded DataFrame
from mlxtend.preprocessing import TransactionEncoder
te = TransactionEncoder()
te_data = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_data, columns=te.columns_)
# Display results
print("Frequent Itemsets:\n", frequent_itemsets)
print("\nAssociation Rules:\n", rules[['antecedents', 'consequents',
'support', 'confidence', 'lift']])
Frequent Itemsets:
support itemsets
0 0.8 (bread)
1 0.8 (butter)
2 0.6 (milk)
Next, after generating the association rules with a minimum confidence of 0.7.
Association Rules:
24
Study of res net and mobile net
1. ResNet (Residual Networks)
ResNet, or Residual Networks, was introduced by Kaiming He et al. in their 2015 paper "Deep
Residual Learning for Image Recognition." ResNet became a landmark in deep learning due to its
innovative residual connections, which allowed the successful training of very deep neural networks
with hundreds or even thousands of layers. Before ResNet, training deeper neural networks resulted
in diminishing performance due to vanishing/exploding gradients, making it very difficult to train
models efficiently as the depth increased.
Residual Blocks: ResNet introduces residual blocks, where the input to the block is passed
through the main convolutional layers and then added back to the output. This is referred to
as a skip connection or identity mapping.
25
The mathematical expression of a residual block is:
This addition of the identity mapping allows the gradients to flow more easily through the network
during backpropagation, addressing the vanishing gradient problem.
Architecture of ResNet
ResNet uses convolutional layers, batch normalization, and ReLU activation functions.
The core unit of ResNet is the residual block. There are different versions of residual blocks
based on the number of layers, which leads to different versions of ResNet (e.g., ResNet-18,
ResNet-50, ResNet-101, ResNet-152).
For example:
o ResNet-18 and ResNet-34: These have fewer layers (18 and 34, respectively) and are
easier to train, making them suitable for smaller datasets.
o ResNet-50, ResNet-101, and ResNet-152: These are deeper networks with more
residual blocks. These architectures perform better for large datasets like ImageNet
and are suitable for tasks requiring higher accuracy.
Advantages of ResNet
Deep Networks: The main advantage of ResNet is that it can train extremely deep networks
(e.g., ResNet-152) without suffering from degradation in performance.
Residual Learning: The residual learning mechanism enables the model to focus on learning
residual mappings instead of the entire transformation, simplifying the learning task.
Improved Gradient Flow: Residual connections improve the gradient flow during
backpropagation, making training easier for deep networks.
Applications of ResNet
26
Object Detection: ResNet is often used as a backbone for object detection models like Faster
R-CNN.
Semantic Segmentation: Networks like DeepLab use ResNet as the encoder for
segmentation tasks.
Feature Extraction: Due to its powerful feature extraction capabilities, ResNet is widely used
in transfer learning for various vision tasks.
Challenges
Computational Cost: Deeper ResNet architectures, especially those with hundreds of layers,
require considerable computational power for training and inference.
Memory Usage: As the number of layers increases, the memory usage during training also
grows.
2. MobileNet
Introduction
A traditional convolution operation involves applying a kernel to all channels of the input feature
map, resulting in a computationally expensive operation. In contrast, depthwise separable
convolutions split this operation into two steps:
This decomposition reduces the number of parameters and computations by a large factor, making
the network much more efficient.
Architecture of MobileNet
27
Width Multiplier: MobileNet includes a hyperparameter called the width multiplier
(denoted by α\alphaα), which scales the number of channels in each layer. A lower value of
α\alphaα reduces the size of the network, making it faster but less accurate.
Resolution Multiplier: Another parameter, the resolution multiplier (denoted by ρ\rhoρ),
scales the input resolution, reducing the input size for faster processing at the cost of some
accuracy.
Advantages of MobileNet
Applications of MobileNet
Real-Time Object Detection: MobileNet can be used in combination with frameworks like
Single Shot Multibox Detector (SSD) for real-time object detection tasks.
Face Recognition: Due to its efficiency, MobileNet is often used for face recognition
applications on mobile devices.
Augmented Reality (AR): MobileNet is suitable for AR applications on smartphones and
other embedded devices that require low-latency processing.
Speech Recognition: Used in speech-to-text applications where real-time processing is
essential.
Challenges
Lower Accuracy: While MobileNet is highly efficient, its accuracy tends to be lower
compared to larger networks like ResNet, especially on complex tasks.
Trade-off Between Speed and Accuracy: There is always a trade-off when using MobileNet,
where reducing the width or resolution multiplier can result in lower accuracy but faster
inference.
28
Aspect ResNet MobileNet
Number of
High (especially in deep versions) Low (optimized for efficiency)
Parameters
Training Time Longer for deep versions Faster due to fewer parameters
Less flexible, mostly suited for large Highly flexible with width and
Flexibility
datasets resolution multipliers
Conclusion
ResNet is an excellent choice for applications that require high accuracy and can afford high
computational cost, such as large-scale image classification, object detection, and semantic
segmentation.
MobileNet, on the other hand, is ideal for mobile devices, IoT applications, and any scenario
where computational efficiency, speed, and lower memory usage are more important than
achieving the highest accuracy possible.
29