0% found this document useful (0 votes)
4 views51 pages

ML Code Output

The document outlines a series of experiments focused on implementing various NumPy and Pandas commands in Python. It includes tasks such as creating arrays, applying indexing, slicing, reshaping, and performing data manipulations with Pandas, including reading CSV and JSON files. Additionally, it covers creating different types of graphs using Matplotlib, including line graphs, bar graphs, pie charts, and histograms.

Uploaded by

jagratsati869
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)
4 views51 pages

ML Code Output

The document outlines a series of experiments focused on implementing various NumPy and Pandas commands in Python. It includes tasks such as creating arrays, applying indexing, slicing, reshaping, and performing data manipulations with Pandas, including reading CSV and JSON files. Additionally, it covers creating different types of graphs using Matplotlib, including line graphs, bar graphs, pie charts, and histograms.

Uploaded by

jagratsati869
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

Experiment 1: WAP to implement Numpy Commands

Exp. 1 a. Create Numpy array using list, tuple and dictionary


b. Create numpy 2d array of order 3*4
c. Apply indexing in numpy 1D and 2D array

To create NumPy arrays using list, tuple, and dictionary, create a 2D array of order
3×4, and apply indexing on 1D and 2D arrays.

Step 1: Import NumPy Library


import numpy as np
(a) Create NumPy Array Using List, Tuple, and Dictionary
Using List
list_data = [10, 20, 30, 40, 50]
arr_list = [Link](list_data)
print("NumPy array from list:", arr_list)
Using Tuple
tuple_data = (1, 2, 3, 4, 5)
arr_tuple = [Link](tuple_data)
print("NumPy array from tuple:", arr_tuple)
Using Dictionary (Keys Only)
dict_data = {1: 'A', 2: 'B', 3: 'C'}
arr_dict = [Link](list(dict_data.keys()))
print("NumPy array from dictionary keys:", arr_dict)

(b) Create NumPy 2D Array of Order 3 × 4


arr_2d = [Link]([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])

print("2D NumPy array (3x4):")


print(arr_2d)

1
(c) Apply Indexing in NumPy 1D and 2D Array
Indexing in 1D Array
arr_1d = [Link]([100, 200, 300, 400, 500])

print("1D Array:", arr_1d)


print("Element at index 0:", arr_1d[0])
print("Element at index 3:", arr_1d[3])

Indexing in 2D Array
print("2D Array:")
print(arr_2d)

print("Element at row 0, column 1:", arr_2d[0][1])


print("Element at row 2, column 3:", arr_2d[2][3])

Output (Sample)
NumPy array from list: [10 20 30 40 50]
NumPy array from tuple: [1 2 3 4 5]
NumPy array from dictionary keys: [1 2 3]

2D NumPy array (3x4):


[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

Element at index 0: 100


Element at row 2, column 3: 12

2
Experiment 2: WAP to implement Numpy Commands
Exp 2 a. Apply slicing in numpy 1D and 2D array
b. Apply type conversion of numpy arrays
c. c. Create copy and view of numpy array

To apply slicing in NumPy 1D and 2D arrays, perform type conversion of NumPy arrays, and
create copy and view of NumPy arrays.

Step 1: Import NumPy Library


import numpy as np

(a) Apply Slicing in NumPy 1D and 2D Array


Slicing in 1D Array
arr_1d = [Link]([10, 20, 30, 40, 50, 60])

print("Original 1D Array:", arr_1d)


print("Slicing from index 1 to 4:", arr_1d[1:5])
print("Slicing with step 2:", arr_1d[0:6:2])

Slicing in 2D Array
arr_2d = [Link]([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])

print("Original 2D Array:")
print(arr_2d)

print("First two rows and first three columns:")


print(arr_2d[0:2, 0:3])

(b) Apply Type Conversion of NumPy Arrays


arr = [Link]([1, 2, 3, 4, 5])
print("Original Array:", arr)
print("Data type:", [Link])

# Convert int to float


arr_float = [Link](float)
print("After Type Conversion (int to float):", arr_float)
print("New Data type:", arr_float.dtype)

3
(c) Create Copy and View of NumPy Array
original = [Link]([100, 200, 300, 400])

# View
view_arr = [Link]()
view_arr[0] = 999

print("Original array after view change:", original)


print("View array:", view_arr)

# Copy
copy_arr = [Link]()
copy_arr[1] = 888

print("Original array after copy change:", original)


print("Copy array:", copy_arr)

Output (Sample)
Original 1D Array: [10 20 30 40 50 60]
Slicing from index 1 to 4: [20 30 40 50]

Original 2D Array:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

After Type Conversion (int to float): [1. 2. 3. 4. 5.]

Original array after view change: [999 200 300 400]


Original array after copy change: [999 200 300 400]

4
Experiment 3: WAP to implement Numpy Commands

Exp 3 a. Apply reshaping, flattening


b. Apply iterating in numpy array
c. Apply joining, splitting numpy arrays

To apply reshaping and flattening, perform iteration on NumPy arrays, and implement joining
and splitting of NumPy arrays.

Step 1: Import NumPy Library


import numpy as np

(a) Apply Reshaping and Flattening


Reshaping a NumPy Array
arr = [Link]([1, 2, 3, 4, 5, 6])

reshaped_arr = [Link](2, 3)
print("Original Array:", arr)
print("Reshaped Array (2x3):")
print(reshaped_arr)

Flattening a NumPy Array


flat_arr = reshaped_arr.flatten()
print("Flattened Array:", flat_arr)

(b) Apply Iterating in NumPy Array


Iterating through 1D Array
arr_1d = [Link]([10, 20, 30, 40])

print("Iterating 1D Array:")
for element in arr_1d:
print(element)

Iterating through 2D Array


arr_2d = [Link]([[1, 2, 3],
[4, 5, 6]])

print("Iterating 2D Array:")
for row in arr_2d:
for item in row:
print(item)

5
(c) Apply Joining and Splitting NumPy Arrays
Joining NumPy Arrays
Using concatenate()
arr1 = [Link]([1, 2, 3])
arr2 = [Link]([4, 5, 6])

joined_arr = [Link]((arr1, arr2))


print("Joined Array:", joined_arr)
Joining 2D Arrays
a = [Link]([[1, 2], [3, 4]])
b = [Link]([[5, 6], [7, 8]])

joined_2d = [Link]((a, b), axis=0)


print("Joined 2D Array:")
print(joined_2d)

Splitting NumPy Arrays


SpliṄng 1D Array
arr = [Link]([10, 20, 30, 40, 50, 60])

split_arr = np.array_split(arr, 3)
print("Split 1D Array:", split_arr)
SpliṄng 2D Array
arr_2d = [Link]([[1, 2, 3, 4],
[5, 6, 7, 8]])

split_2d = [Link](arr_2d, 2)
print("Split 2D Array:")
print(split_2d)

Output (Sample)
Original Array: [1 2 3 4 5 6]
Reshaped Array:
[[1 2 3]
[4 5 6]]
Flattened Array: [1 2 3 4 5 6]

Iterating 1D Array:
10
20
30
40

Joined Array: [1 2 3 4 5 6]

Split 1D Array:
[array([10, 20]), array([30, 40]), array([50, 60])]

6
Experiment 4: WAP to implement Pandas

Exp 4 a. Creating series in pandas


b. b. Creating data frame in pandas

To implement Pandas in Python and demonstrate:

1. Creating a Series
2. Creating a DataFrame

Software / Tools Required

• Python 3.x
• Pandas Library
• Jupyter Notebook

a) Creating Series in Pandas


A Series in Pandas is a one-dimensional labeled array capable of holding data of any type
(integer, float, string, etc.). Each value in a Series is associated with an index.

Program: Creating a Pandas Series


# Import pandas library
import pandas as pd

# Creating a Series
data = [10, 20, 30, 40, 50]
series = [Link](data)

# Display the Series


print("Pandas Series:")
print(series)

Output
Pandas Series:
0 10
1 20
2 30
3 30
4 50
dtype: int64

Series with Custom Index


# Creating Series with custom index
series2 = [Link]([100, 200, 300], index=['A', 'B', 'C'])

print(series2)

7
b) Creating DataFrame in Pandas
A DataFrame is a two-dimensional data structure consisting of rows and columns, similar
to a table in a database or an Excel spreadsheet.

Program: Creating a Pandas DataFrame


# Import pandas library
import pandas as pd

# Creating a DataFrame using dictionary


data = {
'Name': ['Rahul', 'Amit', 'Neha'],
'Age': [22, 24, 21],
'Marks': [85, 90, 88]
}

df = [Link](data)

# Display the DataFrame


print("Pandas DataFrame:")
print(df)

Output
Pandas DataFrame:
Name Age Marks
0 Rahul 22 85
1 Amit 24 90
2 Neha 21 88

DataFrame with Custom Index


df2 = [Link](data, index=['S1', 'S2', 'S3'])
print(df2)

8
Experiment 5: WAP to implement Pandas

Exp 5 a. Reading CSV file


b. Reading data from Jason file
a. Printing heat and tail of data set.

To implement basic Pandas operations such as:

1. Reading data from a CSV file


2. Reading data from a JSON file
3. Printing head and tail of the dataset

Software / Libraries Required


• Python 3.x
• Pandas

Step-wise Program

Step 1: Import Pandas Library


import pandas as pd

Step 2: Read Data from CSV File


# Reading CSV file
csv_data = pd.read_csv("[Link]")

print("CSV Data:")
print(csv_data)

Step 3: Read Data from JSON File


# Reading JSON file
json_data = pd.read_json("[Link]")

print("\nJSON Data:")
print(json_data)

Step 4: Display First 5 Rows (Head)


print("\nFirst 5 rows of CSV dataset:")
print(csv_data.head())

9
Step 5: Display Last 5 Rows (Tail)
print("\nLast 5 rows of CSV dataset:")
print(csv_data.tail())

Sample Input Files


[Link]
ID,Name,Marks
1,Amit,78
2,Rahul,85
3,Pooja,90
4,Neha,88
5,Ravi,76
6,Suman,82

[Link]
[
{"ID":1,"Name":"Amit","Marks":78},
{"ID":2,"Name":"Rahul","Marks":85},
{"ID":3,"Name":"Pooja","Marks":90}
]

Output
CSV Data Output
ID Name Marks
0 1 Amit 78
1 2 Rahul 85
2 3 Pooja 90
3 4 Neha 88
4 5 Ravi 76
5 6 Suman 82

JSON Data Output


ID Name Marks
0 1 Amit 78
1 2 Rahul 85
2 3 Pooja 90

Head Output
ID Name Marks
0 1 Amit 78
1 2 Rahul 85
2 3 Pooja 90
3 4 Neha 88
4 5 Ravi 76

10
Tail Output
ID Name Marks
1 2 Rahul 85
2 3 Pooja 90
3 4 Neha 88
4 5 Ravi 76
5 6 Suman 82

11
Experiment 6: WAP to create following graphs using matplotlib
Exp 6 a. Draw a line in a diagram from position (0,0) to position (6,250):

b. Create a Multiple Lines graph

c. Draw titles and axis labels in a graph

d. Add Grid Lines in a graph

e. Display Multiple Plots in a graphs

f. Create Bars graphs

g. Create pie chart

h. Create Histogram.

To create different types of graphs using Matplotlib in Python such as:

• Line graph
• Multiple line graph
• Graph with title & labels
• Graph with grid
• Multiple plots
• Bar graph
• Pie chart
• Histogram

Software / Library Required


• Python 3.x
• Matplotlib

Step 1: Import Required Library


import [Link] as plt

a) Draw a Line from Position (0,0) to (6,250)


x = [0, 6]
y = [0, 250]

[Link](x, y)
[Link]()

12
b) Create a Multiple Lines Graph
x = [1, 2, 3, 4]
y1 = [10, 20, 30, 40]
y2 = [15, 25, 35, 45]

[Link](x, y1)
[Link](x, y2)
[Link]()

c) Draw Titles and Axis Labels in a Graph


x = [1, 2, 3, 4]
y = [10, 20, 30, 40]

[Link](x, y)
[Link]("Simple Line Graph")
[Link]("X Axis")
[Link]("Y Axis")
[Link]()

d) Add Grid Lines in a Graph


x = [1, 2, 3, 4]
y = [10, 20, 30, 40]

[Link](x, y)
[Link](True)
[Link]()

e) Display Multiple Plots in a Graph


x = [1, 2, 3, 4]

[Link](1, 2, 1)
[Link](x, [10, 20, 30, 40])
[Link]("Plot 1")

[Link](1, 2, 2)
[Link](x, [40, 30, 20, 10])
[Link]("Plot 2")

[Link]()

f) Create Bar Graph


names = ["A", "B", "C", "D"]
values = [5, 7, 3, 8]

[Link](names, values)
[Link]("Bar Graph")
[Link]()

13
g) Create Pie Chart
labels = ["Python", "Java", "C++", "Others"]
sizes = [40, 30, 20, 10]

[Link](sizes, labels=labels, autopct='%1.1f%%')


[Link]("Pie Chart")
[Link]()

h) Create Histogram
data = [10, 20, 20, 30, 30, 30, 40, 50]

[Link](data)
[Link]("Histogram")
[Link]()

14
Experiment 7: WAP Encoding Categorical Data.

To encode categorical data into numerical form using:

1. Label Encoding
2. One-Hot Encoding

Software / Libraries Required


• Python 3.x
• Pandas
• Scikit-learn

Theory
• Categorical data contains text values (e.g., Male/Female, Red/Blue).
• Machine learning algorithms require numerical input, so encoding is necessary.

Step-wise Program

Step 1: Import Required Libraries


import pandas as pd
from [Link] import LabelEncoder

Step 2: Create Sample Dataset


data = {
'Name': ['Amit', 'Rahul', 'Neha', 'Pooja'],
'Gender': ['Male', 'Male', 'Female', 'Female'],
'City': ['Delhi', 'Mumbai', 'Delhi', 'Kolkata']
}

df = [Link](data)
print("Original Data:")
print(df)

Method 1: Label Encoding


Step 3: Apply Label Encoding
le = LabelEncoder()

15
df['Gender_Encoded'] = le.fit_transform(df['Gender'])
df['City_Encoded'] = le.fit_transform(df['City'])

print("\nAfter Label Encoding:")


print(df)

Method 2: One-Hot Encoding


Step 4: Apply One-Hot Encoding
one_hot_df = pd.get_dummies(df, columns=['Gender', 'City'])

print("\nAfter One-Hot Encoding:")


print(one_hot_df)

Output
Original Data
Name Gender City
0 Amit Male Delhi
1 Rahul Male Mumbai
2 Neha Female Delhi
3 Pooja Female Kolkata

After Label Encoding


Name Gender City Gender_Encoded City_Encoded
0 Amit Male Delhi 1 0
1 Rahul Male Mumbai 1 2
2 Neha Female Delhi 0 0
3 Pooja Female Kolkata 0 1

After One-Hot Encoding


Name Gender_Female Gender_Male City_Delhi City_Kolkata City_Mumbai
0 Amit 0 1 1 0
0
1 Rahul 0 1 0 0
1
2 Neha 1 0 1 0
0
3 Pooja 1 0 0 1
0

16
Experiment 8: WAP for Splitting the dataset into Training and Test set.

To split a given dataset into training data and testing data using Python.

Software / Libraries Required


• Python 3.x
• NumPy
• Scikit-learn

Theory
• Training set is used to train the machine learning model.
• Test set is used to evaluate the performance of the model.
• Common split ratio is 70:30 or 80:20.

Step-wise Program
Step 1: Import Required Libraries
import numpy as np
from sklearn.model_selection import train_test_split

Step 2: Create Sample Dataset


# Independent variables
X = [Link]([
[1, 10],
[2, 20],
[3, 30],
[4, 40],
[5, 50]
])

# Dependent variable
Y = [Link]([100, 200, 300, 400, 500])

Step 3: Split the Dataset


X_train, X_test, Y_train, Y_test = train_test_split(
X, Y, test_size=0.3, random_state=42
)

Step 4: Display Training and Test Data


print("Training Data (X_train):")
print(X_train)

print("\nTest Data (X_test):")


print(X_test)

17
print("\nTraining Output (Y_train):")
print(Y_train)

print("\nTest Output (Y_test):")


print(Y_test)

Output
Training Data (X_train):
[[1 10]
[3 30]
[4 40]]

Test Data (X_test):


[[2 20]
[5 50]]

Training Output (Y_train):


[100 300 400]

Test Output (Y_test):


[200 500]

18
Experiment 9: WAP for Feature Scaling.

To apply Feature Scaling techniques on a dataset so that all features are on the same scale.

Software / Libraries Required


• Python 3.x
• NumPy
• Pandas
• Scikit-learn

Theory
Feature scaling is used to normalize the range of independent variables.
Common techniques:

1. Standardization (Z-score scaling)


2. Normalization (Min–Max scaling)

Step-wise Program

Step 1: Import Required Libraries


import numpy as np
import pandas as pd
from [Link] import StandardScaler, MinMaxScaler

Step 2: Create Sample Dataset


data = {
'Age': [18, 22, 25, 30, 35],
'Salary': [15000, 22000, 27000, 35000, 40000]
}

df = [Link](data)
print("Original Dataset:")
print(df)

Method 1: Standardization
Step 3: Apply Standard Scaler
scaler = StandardScaler()
standardized_data = scaler.fit_transform(df)

df_standardized = [Link](standardized_data, columns=[Link])

19
print("\nAfter Standardization:")
print(df_standardized)

Method 2: Normalization
Step 4: Apply Min–Max Scaler
minmax = MinMaxScaler()
normalized_data = minmax.fit_transform(df)

df_normalized = [Link](normalized_data, columns=[Link])


print("\nAfter Normalization:")
print(df_normalized)

Output
Original Dataset
Age Salary
0 18 15000
1 22 22000
2 25 27000
3 30 35000
4 35 40000

After Standardization
Age Salary
0 -1.414214 -1.336306
1 -0.707107 -0.534522
2 0.000000 0.000000
3 0.707107 0.801784
4 1.414214 1.069045

After Normalization
Age Salary
0 0.00 0.00
1 0.24 0.21
2 0.41 0.36
3 0.71 0.71
4 1.00 1.00

20
Experiment 10: WAP to implement Simple linear regression.

To implement Simple Linear Regression to predict a continuous target variable


using a single feature. Train the model, make predictions, and visualize the
regression line to understand the relationship between the feature and target.

# [Link]

# Salary data

# Step-1: Data Pre-processing

# Import Libraries and read csv file

import numpy as nm
import [Link] as mtp
import pandas as pd
data_set= pd.read_csv('Salary_Data.csv')
print(data_set)

# Extracting dependent and independent variables

x= data_set.iloc[:, :-1].values
y= data_set.iloc[:, 1].values

# Splitting the dataset into training and test set.


from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 1/3, random_state=0)

print("x_train")
print(x_train)

print("x_test")
print(x_test)

print("y_train")
print(y_train)

print("y_test")
print(y_test)

# Step-2: Fitting the Simple Linear Regression to the Training Set


from sklearn.linear_model import LinearRegression

21
regressor= LinearRegression()
[Link](x_train, y_train)

# Step: 3. Prediction of test set result:


#Prediction of Test and Training set result
y_pred_test= [Link](x_test)
y_pred_train= [Link](x_train)

# y_pred= [Link](x_test)
# x_pred= [Link](x_train)

print("y_train")
print(y_train)

print("y_pred_train")
print(y_pred_train)

print("y_test")
print(y_test)

print("y_pred_test")
print(y_pred_test)

# Step: 4. visualizing the Training set results:


[Link](x_train, y_train, color="green")
[Link](x_train, y_pred_train, color="red")
[Link]("Salary vs Experience (Training Dataset)")
[Link]("Years of Experience")
[Link]("Salary(In Rupees)")
[Link]()

# Step: 5. visualizing the Test set results:

#visualizing the Test set results


#visualizing the Test set results
[Link](x_test, y_test, color="blue")
[Link](x_test, y_pred_test, color="red")
[Link]("Salary vs Experience (Test Dataset)")
[Link]("Years of Experience")
[Link]("Salary(In Rupees)")
[Link]()

print([Link](x_test, y_test))

Output:

22
0.9749154407708353

23
Experiment 11: WAP to implement Multiple linear regression.

To implement Multiple Linear Regression to predict a continuous target


variable using multiple features. Train the model, make predictions, and
evaluate the contribution of each feature to the target variable.
# Multiple Linear Regression

# [Link]

# dataset(50_CompList),

# Step-1: Data Pre-processing Step:

# importing libraries

import numpy as nm

import [Link] as mtp

import pandas as pd

#importing datasets

data_set= pd.read_csv('50_Startups.csv')

print(data_set)

#Extracting Independent and dependent Variable

x= data_set.iloc[:, :-1].values

y= data_set.iloc[:, 3].values

print(x)

print(y)

# Splitting the dataset into training and test set.

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.2, random_state=0)

print("x_train")

print(x_train)

print("x_test")

24
print(x_test)

print("y_train")

print(y_train)

print("y_test")

print(y_test)

# #Fitting the MLR model to the training set:

from sklearn.linear_model import LinearRegression

regressor= LinearRegression()

[Link](x_train, y_train)

# Step: 3- Prediction of Test set results:

#Predicting the Test set result;

y_pred= [Link](x_test)

print("y_pred")

print(y_pred)

print('Train Score: ', [Link](x_train, y_train))

print('Test Score: ', [Link](x_test, y_test))

Output

Train Score: 0.9499572530324031

Test Score: 0.9393955917820571

25
Experiment 12: To implement Logistic Regression for a binary
classification problem. Train the model, predict class labels, and evaluate
the model accuracy.

• To understand and implement the Logistic Regression algorithm.


• To perform binary classification using Logistic Regression.
• To evaluate model performance using accuracy, confusion matrix, and ROC curve.

Tools Required:

• Python 3.x
• Libraries: pandas, numpy, scikit-learn, matplotlib, seaborn

Step 1: Import Required Libraries


# Data handling
import pandas as pd
import numpy as np

# Machine Learning
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, confusion_matrix, classification_report,
roc_curve, roc_auc_score

# Visualization
import [Link] as plt
import seaborn as sns

Step 2: Load Dataset


# Load Iris dataset from sklearn
from [Link] import load_iris

iris = load_iris()
X = [Link]
y = [Link]

# For binary classification, select only Setosa (0) and Versicolor (1)
binary_indices = y != 2 # Remove class 2 (Virginica)
X = X[binary_indices]
y = y[binary_indices]

# Convert to DataFrame for better visualization


df = [Link](X, columns=iris.feature_names)
df['target'] = y

26
# Display first 5 rows
print([Link]())

Step 3: Explore Dataset


# Check for missing values
print([Link]().sum())

# Check class distribution


[Link](x='target', data=df)
[Link]("Target Class Distribution")
[Link]()

# Basic statistics
print([Link]())

Step 4: Split Dataset into Training and Testing Sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

print(f"Training samples: {len(X_train)}, Testing samples: {len(X_test)}")

Step 5: Build Logistic Regression Model


# Create Logistic Regression model
lr_model = LogisticRegression()

# Train the model


lr_model.fit(X_train, y_train)

Step 6: Make Predictions


# Predict on test data
y_pred = lr_model.predict(X_test)

# Display first 10 predictions


print("Predicted labels:", y_pred[:10])
print("Actual labels: ", y_test[:10])

Step 7: Evaluate Model Performance


# Accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy*100:.2f}%")

# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cm)

# Classification Report

27
print("Classification Report:\n", classification_report(y_test, y_pred))

# Visualize Confusion Matrix


[Link](cm, annot=True, fmt='d', cmap='Greens', xticklabels=[0,1], yticklabels=[0,1])
[Link]("Predicted")
[Link]("Actual")
[Link]("Confusion Matrix")
[Link]()

Step 8: ROC Curve & AUC


# Predict probabilities
y_prob = lr_model.predict_proba(X_test)[:, 1]

# Compute ROC curve


fpr, tpr, thresholds = roc_curve(y_test, y_prob)
auc_score = roc_auc_score(y_test, y_prob)

# Plot ROC Curve


[Link](fpr, tpr, color='blue', label=f'ROC curve (AUC = {auc_score:.2f})')
[Link]([0, 1], [0, 1], color='red', linestyle='--')
[Link]("False Positive Rate")
[Link]("True Positive Rate")
[Link]("ROC Curve")
[Link]()
[Link]()

Sample output

Step 2 Output: Dataset Preview


sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0

Step 3 Output: Class Distribution


# Target counts
0 50
1 50
Name: target, dtype: int64

(Bar chart will show equal distribution of class 0 and 1, each with 50 samples.)

28
Step 4 Output: Train-Test Split
Training samples: 70, Testing samples: 30

Step 6 Output: Predictions


Predicted labels: [1 0 0 1 0 1 1 0 1 0]
Actual labels: [1 0 0 1 0 1 1 0 1 0]

(First 10 predictions match perfectly with actual labels.)

Step 7 Output: Model Evaluation


Accuracy: 100.00%

Confusion Matrix:
[[15 0]
[ 0 15]]

Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 15


1 1.00 1.00 1.00 15

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

(Heatmap of confusion matrix will show perfect classification with diagonal cells
highlighted.)

Step 8 Output: ROC Curve & AUC


AUC Score: 1.00

29
Experiment 13: WAP to implement cross validation.

To implement k-Fold Cross-Validation to evaluate the performance of a model.


Split the data into k folds, train and test the model on different folds, and
calculate the average performance metric
# Applying K fold cross validation

# [Link]

from sklearn.model_selection import train_test_split

from sklearn.model_selection import KFold

from sklearn.model_selection import cross_val_score

from sklearn.linear_model import LinearRegression

from numpy import mean

from numpy import absolute

from numpy import sqrt

import pandas as pd

df = [Link]({'y': [6, 8, 12, 14, 14, 15, 17, 22, 24, 23],

'x1': [2, 5, 4, 3, 4, 6, 7, 5, 8, 9],

'x2': [14, 12, 12, 13, 7, 8, 7, 4, 6, 5]})

print(df)

#define predictor and response variables

X = df[['x1', 'x2']]

y = df['y']

#define cross-validation method to use

# cv = KFold(n_splits=10, random_state=1, shuffle=True)

cv = KFold(n_splits=10)

#build multiple linear regression model

30
model = LinearRegression()

#use k-fold CV to evaluate model

scores = cross_val_score(model, X, y, scoring='neg_mean_absolute_error',cv=cv, n_jobs=-1)

#view mean absolute error

mae= mean(absolute(scores))

print("MSE = ", mae)

print("Scores = ", scores)

Output:

MSE = 3.1461548083469744

31
Experiment 14: To Implement Over-Fitting and Bias–Variance Analysis.

To demonstrate over-fitting in a regression model and analyze the bias–variance trade-off


using training and testing errors.

Software / Libraries Required

• Python 3.x
• NumPy
• Matplotlib
• Scikit-learn

Theory

• Over-fitting: Model performs very well on training data but poorly on test data.
• Under-fitting: Model is too simple and performs poorly on both training and test
data.
• Bias: Error due to overly simple model.
• Variance: Error due to overly complex model.

Step-wise Program

Step 1: Import Required Libraries

import numpy as np
import [Link] as plt
from [Link] import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error
from sklearn.model_selection import train_test_split

Step 2: Generate Sample Dataset

[Link](0)

X = [Link](1, 10, 30).reshape(-1, 1)


y = 3 * [Link]() + [Link](30) * 2

Step 3: Split Dataset into Training and Test Set

X_train, X_test, y_train, y_test = train_test_split(


X, y, test_size=0.3, random_state=42)

Step 4: Create Polynomial Features (High Degree Model)

poly = PolynomialFeatures(degree=8)
X_train_poly = poly.fit_transform(X_train)
X_test_poly = [Link](X_test)

32
Step 5: Train the Regression Model

model = LinearRegression()
[Link](X_train_poly, y_train)

Step 6: Predict on Training and Test Data

y_train_pred = [Link](X_train_poly)
y_test_pred = [Link](X_test_poly)

Step 7: Calculate Training and Test Errors

train_error = mean_squared_error(y_train, y_train_pred)


test_error = mean_squared_error(y_test, y_test_pred)

print("Training Error:", train_error)


print("Test Error:", test_error)

Step 8: Plot Over-Fitting Curve

X_plot = [Link](1, 10, 100).reshape(-1, 1)


X_plot_poly = [Link](X_plot)
y_plot = [Link](X_plot_poly)

[Link](X_train, y_train, color='blue', label='Training


Data')
[Link](X_test, y_test, color='green', label='Test Data')
[Link](X_plot, y_plot, color='red', label='Overfitted
Model')
[Link]("X")
[Link]("Y")
[Link]("Over-Fitting Demonstration")
[Link]()
[Link]()

Output

Error Output

Training Error: 0.15


Test Error: 8.92

33
Experiment 15: WAP to implement decision tree.

To implement a Decision Tree Classifier for a classification dataset. Train the


model, visualize the tree structure, predict test data, and evaluate accuracy and
performance metrics
######### Decision Tree 1

# [Link]

# importing libraries

import numpy as nm

import [Link] as mtp

import pandas as pd

#importing datasets

data_set= pd.read_csv('user_data.csv')

# data Set Download

# [Link]

#Extracting Independent and dependent Variable

x= data_set.iloc[:, [2,3]].values

y= data_set.iloc[:, 4].values

# Splitting the dataset into training and test set.

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.25, random_state=0)

#feature Scaling

from [Link] import StandardScaler

st_x= StandardScaler()

x_train= st_x.fit_transform(x_train)

34
x_test= st_x.transform(x_test)

#Fitting Decision Tree classifier to the training set

from [Link] import DecisionTreeClassifier

classifier= DecisionTreeClassifier(criterion='entropy', random_state=0)

[Link](x_train, y_train)

#Predicting the test set result

y_pred= [Link](x_test)

#Creating the Confusion matrix

from [Link] import confusion_matrix

cm= confusion_matrix(y_test, y_pred)

print("Confusion Matrix is : ")

print(cm)

from [Link] import accuracy_score

score =accuracy_score(y_pred, y_test)

print("Test Accuracy Score ")

print(score)

Output:

Confusion Matrix is :

[[62 6]

[ 3 29]]

Test Accuracy Score

0.91

35
Experiment 16: WAP to implement Naïve Bayes classifier.

## Python Implementation of the Naïve Bayes algorithm:

# Steps to implement:

# Data Pre-processing step

# Fitting Naive Bayes to the Training set

# Predicting the test result

# Test accuracy of the result(Creation of Confusion matrix)

# Visualizing the test set result.

# importing the libraries

import numpy as nm

import [Link] as mtp

import pandas as pd

#importing datasets

# data Set Download

# [Link]

dataset = pd.read_csv('user_data.csv')

x = [Link][:, [2, 3]].values

y = [Link][:, 4].values

# Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_state = 0)

# Feature Scaling

from [Link] import StandardScaler

sc = StandardScaler()

36
x_train = sc.fit_transform(x_train)

x_test = [Link](x_test)

# Fitting Naive Bayes to the Training set

from sklearn.naive_bayes import GaussianNB

classifier = GaussianNB()

[Link](x_train, y_train)

# Predicting the Test set results

y_pred = [Link](x_test)

# Making the Confusion Matrix

from [Link] import confusion_matrix

cm = confusion_matrix(y_test, y_pred)

print("Confusion Matrix is : ")

print(cm)

# Accuracy

from [Link] import accuracy_score

score =accuracy_score(y_pred, y_test)

print("Test Accuracy Score ")

print(score)

Output:

Confusion Matrix is :

[[65 3]

[ 7 25]]

Test Accuracy Score

0.9

37
Experiment 17: To implement a k-Nearest Neighbors classifier. Train the
model, predict class labels for test data, experiment with different values of
k, and evaluate model performance.

• To understand and implement the k-Nearest Neighbors (k-NN) algorithm.


• To perform classification using k-NN.
• To evaluate model performance with accuracy, confusion matrix, and visualization.

Tools Required:

• Python 3.x
• Libraries: pandas, numpy, scikit-learn, matplotlib, seaborn

Step 1: Import Required Libraries


# Data handling
import pandas as pd
import numpy as np

# Machine Learning
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score, confusion_matrix, classification_report

# Visualization
import [Link] as plt
import seaborn as sns

Step 2: Load Dataset


# Load Iris dataset
iris = load_iris()
X = [Link]
y = [Link]

# Convert to DataFrame for better visualization


df = [Link](X, columns=iris.feature_names)
df['target'] = y

# Display first 5 rows


print([Link]())

Step 3: Explore Dataset


# Check for missing values
print([Link]().sum())

38
# Class distribution
[Link](x='target', data=df)
[Link]("Target Class Distribution")
[Link]()

# Summary statistics
print([Link]())

Step 4: Split Dataset into Training and Testing Sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

print(f"Training samples: {len(X_train)}, Testing samples: {len(X_test)}")

Step 5: Build k-NN Model


# Create k-NN classifier
k = 5 # Number of neighbors
knn_model = KNeighborsClassifier(n_neighbors=k)

# Train the model


knn_model.fit(X_train, y_train)

Step 6: Make Predictions


# Predict on test data
y_pred = knn_model.predict(X_test)

# Display first 10 predictions


print("Predicted labels:", y_pred[:10])
print("Actual labels: ", y_test[:10])

Step 7: Evaluate Model Performance


# Accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy*100:.2f}%")

# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cm)

# Classification Report
print("Classification Report:\n", classification_report(y_test, y_pred))

# Visualize Confusion Matrix


[Link](cm, annot=True, fmt='d', cmap='Oranges', xticklabels=iris.target_names,
yticklabels=iris.target_names)
[Link]("Predicted")

39
[Link]("Actual")
[Link]("Confusion Matrix")
[Link]()

Step 8: Optional – Experiment with Different k Values


# Find best k value (optional)
accuracy_list = []
k_values = range(1, 21)

for k in k_values:
knn = KNeighborsClassifier(n_neighbors=k)
[Link](X_train, y_train)
y_pred_k = [Link](X_test)
accuracy_list.append(accuracy_score(y_test, y_pred_k))

# Plot accuracy vs k
[Link](k_values, accuracy_list, marker='o')
[Link]("Number of Neighbors (k)")
[Link]("Accuracy")
[Link]("k-NN Accuracy vs k Value")
[Link]()

Sample output
Step 2 Output: Dataset Preview
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0

Step 3 Output: Class Distribution


# Target counts
0 50
1 50
2 50
Name: target, dtype: int64

(Bar chart will show three classes, each with 50 samples.)

Step 4 Output: Train-Test Split


Training samples: 105, Testing samples: 45

40
Step 6 Output: Predictions
Predicted labels: [1 0 2 1 2 0 1 1 0 0]
Actual labels: [1 0 2 1 2 0 1 1 0 0]

(First 10 predictions match perfectly with actual labels.)

Step 7 Output: Model Evaluation


Accuracy: 97.78%

Confusion Matrix:
[[14 0 0]
[ 0 16 2]
[ 0 1 12]]

Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 14


1 0.94 0.89 0.91 18
2 0.86 0.92 0.89 13

accuracy 0.98 45
macro avg 0.93 0.94 0.93 45
weighted avg 0.97 0.98 0.97 45

(Confusion matrix heatmap will show almost perfect classification, slight misclassifications
occur between class 1 and 2.)

Step 8 Output: Accuracy vs k Plot

• For k values from 1 to 20, the accuracy may fluctuate slightly:

k=1 -> Accuracy: 95.56%


k=2 -> Accuracy: 95.56%
k=3 -> Accuracy: 97.78%
k=5 -> Accuracy: 97.78%
k=7 -> Accuracy: 97.78%
...
k=20 -> Accuracy: 93.33%

(Plot will show a peak around k=3 to k=7, then slightly decreasing.)

Experiment 18: To implement a Random Forest Classifier on a dataset.

41
Train multiple decision trees, combine their predictions, and evaluate
accuracy and feature importance.

• To understand and implement the Random Forest algorithm.


• To perform classification using the Random Forest model.
• To evaluate model performance using accuracy, confusion matrix, and feature
importance.

Tools Required:

• Python 3.x
• Libraries: pandas, numpy, scikit-learn, matplotlib, seaborn

Step 1: Import Required Libraries


# Data handling
import pandas as pd
import numpy as np

# Machine Learning
from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier
from [Link] import accuracy_score, confusion_matrix, classification_report

# Visualization
import [Link] as plt
import seaborn as sns

Step 2: Load Dataset


# Load Iris dataset from sklearn
from [Link] import load_iris

iris = load_iris()
X = [Link] # Features
y = [Link] # Target variable

# Convert to DataFrame for easier handling


df = [Link](X, columns=iris.feature_names)
df['target'] = y

# Display first 5 rows


print([Link]())

Step 3: Explore Dataset


# Dataset info
print([Link]())

42
# Check for missing values
print([Link]().sum())

# Basic statistics
print([Link]())

# Visualize target distribution


[Link](x='target', data=df)
[Link]("Target Class Distribution")
[Link]()

Step 4: Split Dataset into Training and Testing Sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

print(f"Training samples: {len(X_train)}, Testing samples: {len(X_test)}")

Step 5: Build Random Forest Model


# Create Random Forest Classifier
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)

# Train the model


rf_model.fit(X_train, y_train)

Step 6: Make Predictions


# Predict on test data
y_pred = rf_model.predict(X_test)

# Display first 10 predictions


print("Predicted labels:", y_pred[:10])
print("Actual labels: ", y_test[:10])

Step 7: Evaluate Model Performance


# Accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy*100:.2f}%")

# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cm)

# Classification Report
print("Classification Report:\n", classification_report(y_test, y_pred))

# Visualize Confusion Matrix

43
[Link](cm, annot=True, fmt='d', cmap='Blues', xticklabels=iris.target_names,
yticklabels=iris.target_names)
[Link]("Predicted")
[Link]("Actual")
[Link]("Confusion Matrix")
[Link]()

Step 8: Feature Importance


# Get feature importance
importances = rf_model.feature_importances_
features = iris.feature_names

# Create a DataFrame
feat_df = [Link]({'Feature': features, 'Importance': importances})
feat_df = feat_df.sort_values(by='Importance', ascending=False)

# Display feature importance


print(feat_df)

# Visualize feature importance


[Link](x='Importance', y='Feature', data=feat_df)
[Link]("Feature Importance")
[Link]()

Sample Output:

Step 2 Output: Dataset Preview


sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0

Step 3 Output: Dataset Info & Stats


<class '[Link]'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
# Column Non-Null Count Dtype

0 sepal length (cm) 150 non-null float64


1 sepal width (cm) 150 non-null float64
2 petal length (cm) 150 non-null float64
3 petal width (cm) 150 non-null float64
4 target 150 non-null int64

44
dtypes: float64(4), int64(1)
memory usage: 6.0 KB

Missing values per column:


sepal length (cm) 0
sepal width (cm) 0
petal length (cm) 0
petal width (cm) 0
target 0
dtype: int64

Basic statistics:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
count 150.000000 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.199333 1.000000
std 0.828066 0.435866 1.765298 0.762238 0.819232
min 4.300000 2.000000 1.000000 0.100000 0.000000
25% 5.100000 2.800000 1.600000 0.300000 0.000000
50% 5.800000 3.000000 4.350000 1.300000 1.000000
75% 6.400000 3.300000 5.100000 1.800000 2.000000
max 7.900000 4.400000 6.900000 2.500000 2.000000

(The target class distribution plot would show roughly 50 samples per class.)

Step 4 Output: Train-Test Split


Training samples: 105, Testing samples: 45

Step 6 Output: Predictions


Predicted labels: [1 0 2 1 2 0 1 1 0 0]
Actual labels: [1 0 2 1 2 0 1 1 0 0]

Step 7 Output: Model Evaluation


Accuracy: 97.78%

Confusion Matrix:
[[14 0 0]
[ 0 17 1]
[ 0 0 13]]

Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 14


1 1.00 0.94 0.97 18
2 0.93 1.00 0.97 13

45
accuracy 0.98 45
macro avg 0.98 0.98 0.98 45
weighted avg 0.98 0.98 0.98 45

(Confusion matrix heatmap will visually show almost perfect classification.)

Step 8 Output: Feature Importance


Feature Importance
2 petal length (cm) 0.454321
3 petal width (cm) 0.429876
0 sepal length (cm) 0.077654
1 sepal width (cm) 0.038149

(Bar chart will clearly show petal length and petal width are the most important
features.)

46
Experiment 19: WAP to implement support vector machine.

# SVM

# [Link]

# Data Pre-processing Step

# importing libraries

import numpy as nm

import [Link] as mtp

import pandas as pd

#importing datasets

data_set= pd.read_csv('user_data.csv')

print(data_set.head())

#Extracting Independent and dependent Variable

x= data_set.iloc[:, [2,3]].values

y= data_set.iloc[:, 4].values

print(x)

print(y)

# Splitting the dataset into training and test set.

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.25, random_state=0)

#feature Scaling

from [Link] import StandardScaler

st_x= StandardScaler()

x_train= st_x.fit_transform(x_train)

47
x_test= st_x.transform(x_test)

print(x_train)

print(x_test)

from [Link] import SVC # "Support vector classifier"

classifier = SVC(kernel='linear', random_state=0)

[Link](x_train, y_train)

#Predicting the test set result

y_pred= [Link](x_test)

#Creating the Confusion matrix

from [Link] import confusion_matrix

cm= confusion_matrix(y_test, y_pred)

print("Confusion Matrix is : ")

print(cm)

from [Link] import accuracy_score

score =accuracy_score(y_pred, y_test)

print("Test Accuracy Score ")

print(score)

Output:

Confusion Matrix is :

[[66 2]

[ 8 24]]

Test Accuracy Score

0.9

48
Experiment 20: To implement PCA for dimensionality reduction on a
dataset. Reduce features to principal components, visualize the
transformed data, and analyze variance explained by each component.

• To understand and implement Principal Component Analysis (PCA).


• To reduce dimensionality of a dataset while preserving variance.
• To visualize high-dimensional data in 2D or 3D using principal components.

Tools Required:

• Python 3.x
• Libraries: pandas, numpy, matplotlib, seaborn, scikit-learn

Step 1: Import Required Libraries


import pandas as pd
import numpy as np
from [Link] import load_iris
from [Link] import StandardScaler
from [Link] import PCA
import [Link] as plt
import seaborn as sns

Step 2: Load Dataset


iris = load_iris()
X = [Link]
y = [Link]

df = [Link](X, columns=iris.feature_names)
df['target'] = y

print([Link]())

Step 3: Standardize the Features


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

print(X_scaled[:5])

Step 4: Apply PCA


pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

explained_variance = pca.explained_variance_ratio_

49
print(explained_variance)

Step 5: Visualize PCA


pca_df = [Link](X_pca, columns=['PC1', 'PC2'])
pca_df['target'] = y

[Link](x='PC1', y='PC2', hue='target', palette='Set1', data=pca_df)


[Link]("PCA of Iris Dataset")
[Link]("Principal Component 1")
[Link]("Principal Component 2")
[Link]()

Step 6: Optional – Check Feature Contribution


components = [Link](pca.components_, columns=iris.feature_names,
index=['PC1','PC2'])
print(components)

Sample output:

Step 2 Output: Dataset Preview


sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0

Step 3 Output: Standardized Features (first 5 rows)


[[ 0.90068117 1.03205701 -1.3412724 -1.3129777 ]
[ 0.11217824 -0.12495841 -1.3412724 -1.3129777 ]
[-0.6763247 0.4025493 -1.39813805 -1.3129777 ]
[-0.99152616 0.13879545 -1.28440676 -1.3129777 ]
[ 0.59787938 1.29681087 -1.3412724 -1.3129777 ]]

Step 4 Output: PCA Components & Explained Variance


PCA Components (first 5 rows):
[[-0.521, 0.269, -0.580, -0.565],
[-0.377, -0.923, -0.024, -0.066]]

Explained Variance Ratio:

50
[0.7277, 0.2303]

Total Variance Explained by 2 Components: 95.80%

(This means the first 2 principal components retain ~95.8% of the original dataset’s
variance.)

Step 6 Output: Feature Contribution (PCA Component Weights)


PCA Component Weights:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
PC1 0.521 -0.269 0.580 0.565
PC2 0.377 0.923 0.024 0.066

(This shows which features contribute most to each principal component. PC1 is mostly
influenced by petal length and width, PC2 by sepal width.)

51

You might also like