ML Lab (R22) Manual
ML Lab (R22) Manual
OUTPUT:
Dataset: [10, 20, 30, 40, 50]
Mean: 30.00
Median: 30.00
Mode: 10
Variance: 250.00
Standard Deviation: 15.81
RESULT: calculating the values of mean mode and variance and standard
deviation for sample data set
2. Study of Python Basic Libraries such as Statistics, Math, Numpy and Scipy
AIM: Study of Python Basic Libraries such as Statistics, Math, Numpy and Scipy
PROCEDURE:for python basic libraries by import command use the necessary
libraries to develop various designs which we can solve complex mathematical
problems.
PROGRAM:
Study of library such as statistics and math function
1. Math:
o The built-in math module provides various mathematical functions and
constants.
o It includes functions for trigonometry, logarithms, exponentiation, and
more.
import math
# Calculate the square root
sqrt_value = math.sqrt(25)
print(f"Square root of 25: {sqrt_value:.2f}")
# Compute the factorial
factorial_value = math.factorial(5)
print(f"Factorial of 5: {factorial_value}")
# Calculate the sine of an angle (in radians)
angle_radians = math.radians(30)
sine_value = math.sin(angle_radians)
print(f"Sine of 30 degrees: {sine_value:.2f}")
NumPy(Numerical Python):
NumPy is a fundamental library for numerical computations in Python.
It provides support for multi-dimensional arrays (ndarrays) and efficient
operations on these arrays.
Python
import numpy as np
# Create a 1D array
arr1d = np.array([1, 2, 3])
print(f"1D Array: {arr1d}")
# Create a 2D array
arr2d = np.array([[1, 2, 3], [4, 5, 6]])
print(f"2D Array: {arr2d}")
Example usage:
Python
import numpy as np#
#Create a 1D array
arr1d = np.array([1, 2, 3])
print(f"1D Array: {arr1d}")
# Create a 2D array
arr2d = np.array([[1, 2, 3], [4, 5, 6]])
print(f"2D Array: {arr2d}")
SciPy(Scientific Python):
SciPy builds upon NumPy and provides additional functionality for scientific and engineering
purposes.
It includes modules for optimization, integration, linear algebra, signal processing, and more.
Python
import scipy.optimize as opt
# Solve an optimization problem
def objective(x):
return x[0]**2 + x[1]**2
result = opt.minimize(objective, [1, 1])
print(f"Optimal solution: {result.x}")
# Perform numerical integration
from scipy.integrate import quad
def integrand(x):
return x**2
area, error = quad(integrand, 0, 2)
print(f"Integral result: {area:.2f}")
o Example usage:
Python
import scipy.optimize as opt
#Solve an optimization problem
def objective(x):
return x[0]**2 + x[1]**2
result = opt.minimize(objective, [1, 1])
print(f"Optimal solution: {result.x}")
# Perform numerical Integration
from scipy.integrate import quad
def integrand(x):
return x**2
area, error = quad(integrand, 0, 2)
print(f"Integral result: {area:.2f}")
OUTPUT:
Calculate the square root
Square root of 25: 5.00
Factorial of 5: 120
Sine of 30 degrees: 0.50
PROGRAM:
essential Python libraries for Machine Learning (ML) applications,
Example:
1. Pandas
import pandas as pd
# Create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 22]}
df = pd.DataFrame(data)
# Display the DataFrame
print(df)
2.Matplotlib:
Purpose: Matplotlib is a popular plotting library for creating visualizations in Python.
Key Features:Line plots, scatter plots, bar charts, histograms, etc
Customization: You can control colors, labels, and other plot properties.
Integration with Pandas: Matplotlib works seamlessly with Pandas DataFrames.
Example (creating a simple line plot):
Matplotlib libraries import from Python for drawing sample graph
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4]
y = [10, 15, 7, 20]
# Create a line plot
plt.plot(x, y, marker='o', label='Sales')
plt.xlabel('Month')
plt.ylabel('Revenue')
plt.title('Monthly Sales')
plt.legend()
plt.show()
OUTPUT:
Importing pandas from python libraries for sample values
Name Age
0 Alice 25
1 Bob 30
2 Charlie 22
Importing matplot from
python libraries for sample
values
RESULT: Importing libraries from python and basic calculation and plotting graph on sample
values
4. write a python program to implement simple linear regression
AIM: procedure for drawing a decision line in python with linear regression technique
PROCEDURE: in machine learning for predicting the output which samples are trained to
machine decision line is vital for take the output consideration for acquiring results how decision
line is used and implementation of libraries is as follows.
PROGRAM:
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
OUTPUT:
RESULT: Importing libraries from python and basic calculation and plotting
graph on sample values a python program to implement simple linear regression
5.implementation of multiple linear regression for house price prediction using sklearn.
AIM: procedure for drawing a multiple linear regression for house price prediction using sklearn.
PROCEDURE: in machine learning for predicting the output which samples are trained to
machine decision line is vital for take the output consideration for multiple linear regression for
house price prediction using sklearn.
PROGRAM:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn import preprocessing
# importing data
df = pd.read_csv('Real-estate1.csv')
df.drop('No', inplace=True, axis=1)
print(df.head())
print(df.columns)
# plotting a scatterplot
sns.scatterplot(x='X4 number of convenience stores',
y='Y house price of unit area', data=df)
print(X)
print(y)
# making predictions
predictions = model.predict(X_test)
# model evaluation
print('mean_squared_error : ', mean_squared_error(y_test, predictions))
print('mean_absolute_error : ', mean_absolute_error(y_test, predictions))
OUTPUT:
X5 latitude X6 longitude
0 24.98298 121.54024
1 24.98034 121.53951
2 24.98746 121.54391
3 24.98746 121.54391
4 24.97937 121.54245
.. ... ...
409 24.94155 121.50381
410 24.97433 121.54310
411 24.97923 121.53986
412 24.96674 121.54067
413 24.97433 121.54310
RESULT: after importing libraries sample data is trained and imported from csv
file for predicted expected target data by extravagating math formulae.
6.implementation of decision tree using sklearn and its parameters tuning.
AIM: procedure for decision tree parameters and its tuning by sklearn libraries
PROCEDURE: in machine learning for predicting the output which samples are trained to
machine decision line is vital for take the output consideration forDecision tree for house price
prediction using sklearn.
PROGRAM:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import metrics
import seaborn as sns
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import tree
#replacing the categories of target variable with the actual names of the species
target = np.unique(iris.target)
target_n = np.unique(iris.target_names)
target_dict = dict(zip(target, target_n))
data['Species'] = data['Species'].replace(target_dict)
# Separating the independent dependent variables of the dataset
x = data.drop(columns = "Species")
y = data["Species"]
names_features = x.columns
target_labels = y.unique()
# Plotting heatmap
sns.heatmap(matrix, annot = True, fmt = "g", ax = axis, cmap = "magma")
axis.set_title('Confusion Matrix')
axis.set_xlabel("Predicted Values", fontsize = 10)
axis.set_xticklabels([''] + target_labels)
axis.set_ylabel( "True Labels", fontsize = 10)
axis.set_yticklabels(list(target_labels), rotation = 0)
plt.show()
Output:
RESULT: after importing libraries sample data is trained from samples for
predicted expected parameter data by extravagating its tuning.
7.implementation of knn using sklearn
PROCEDURE: in machine learning for predicting the output which samples are trained to
machine decision line is vital for take the output consideration forK-NN for data csv format file
using sklearn.
PROGRAM:
implementation of knn using sklearn
output:
2: Training Score = 0.97, Test Score = 0.92
3: Training Score = 0.96, Test Score = 0.94
4: Training Score = 0.95, Test Score = 0.94
5: Training Score = 0.95, Test Score = 0.94
6: Training Score = 0.94, Test Score = 0.94
7: Training Score = 0.94, Test Score = 0.94
8: Training Score = 0.94, Test Score = 0.94
9: Training Score = 0.94, Test Score = 0.94
10: Training Score = 0.94, Test Score = 0.94
11: Training Score = 0.94, Test Score = 0.94
12: Training Score = 0.94, Test Score = 0.94
13: Training Score = 0.94, Test Score = 0.94
14: Training Score = 0.94, Test Score = 0.94
15: Training Score = 0.94, Test Score = 0.94
16: Training Score = 0.94, Test Score = 0.94
17: Training Score = 0.94, Test Score = 0.94
18: Training Score = 0.94, Test Score = 0.94
19: Training Score = 0.94, Test Score = 0.94
20: Training Score = 0.94, Test Score = 0.94
RESULT: after importing libraries sample data is trained from samples for
predicted expected parameter data by extravagating its tuning.
8.implementation of logic regression using sklearn
PROCEDURE: in machine learning for predicting the output which samples are trained to
machine decision line is vital for take the output consideration for logic regression for data csv
format file using sklearn.
PROGRAM:
RESULT: after importing libraries sample data is trained from samples for
predicted expected values towards sample csv file.
PROCEDURE: in machine learning for predicting the output which samples are trained to
machine decision line is vital for take the output consideration fork means for samples under
clustering method.
PROGRAM:
implementation of k means clustering
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
# Load the dataset (fixing file path issue)
data = pd.read_csv(r'C:\Users\91970\data.csv')
# Select relevant features
X = data[['feature1', 'feature2']].dropna() # Drop missing values if any
# Create and fit the K-Means model
kmeans = KMeans(n_clusters=3, n_init=10) # Explicitly set n_init to avoid warnings
kmeans.fit(X)
# Get cluster centers and labels
cluster_centers = kmeans.cluster_centers_
labels = kmeans.labels_
# Visualize the clusters
plt.scatter(X['feature1'], X['feature2'], c=labels, cmap='rainbow', alpha=0.6, edgecolors='k')
plt.scatter(cluster_centers[:, 0], cluster_centers[:, 1], color='black', marker='x', s=100,
label='Centroids')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('K-Means Clustering')
plt.legend()
plt.show()
Output:
RESULT: after importing libraries sample data is trained from samples for
predicted expected values towards sample csv file.
# Load dataset
df = pd.read_csv('data10.csv') # Replace with actual dataset
# Data Preprocessing
X = df.iloc[:, :-1] # Features
y = df.iloc[:, -1] # Target
# Initialize classifiers
classifiers = {
"Logistic Regression": LogisticRegression(),
"K-Nearest Neighbors": KNeighborsClassifier(),
"Support Vector Machine": SVC(),
"Decision Tree": DecisionTreeClassifier(),
"Random Forest": RandomForestClassifier()
}
print(f"{name} Performance:")
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))
# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(5, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title(f'Confusion Matrix - {name}')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()
Output:
Feature_1 Feature_2 Feature_3 Feature_4 Target
0 43.708611 32.101582 0 33.417560 1
1 95.564288 4.206998 1 48.761708 0
2 75.879455 8.081436 0 39.504238 0
3 63.879264 44.927709 0 24.230963 0
4 24.041678 30.321453 1 21.015853 0
Logistic Regression Performance:
Accuracy: 0.55
Classification Report:
precision recall f1-score support
accuracy 0.55 40
macro avg 0.55 0.55 0.55 40
weighted avg 0.56 0.55 0.55 40