0% found this document useful (0 votes)
2 views44 pages

Ml Lab Programs

The document outlines various statistical and machine learning techniques, including computing central tendency measures (mean, median, mode) and measures of dispersion (variance, standard deviation) using Python libraries. It also describes data preprocessing techniques such as attribute selection, handling missing values, discretization, and outlier elimination, followed by the application of KNN for classification and regression, and decision tree algorithms for both classification and regression tasks with parameter tuning. Each section includes code examples and explanations of the methods used, demonstrating their practical implementation in data analysis and machine learning.

Uploaded by

chandana.polina
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views44 pages

Ml Lab Programs

The document outlines various statistical and machine learning techniques, including computing central tendency measures (mean, median, mode) and measures of dispersion (variance, standard deviation) using Python libraries. It also describes data preprocessing techniques such as attribute selection, handling missing values, discretization, and outlier elimination, followed by the application of KNN for classification and regression, and decision tree algorithms for both classification and regression tasks with parameter tuning. Each section includes code examples and explanations of the methods used, demonstrating their practical implementation in data analysis and machine learning.

Uploaded by

chandana.polina
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Compute Central Tendency Measures: Mean, Median ,Mode Measure of Dispersion:


Variance, Standard Deviation
Aim: To compute Central Tendency Measures: Mean, Median, Mode Measure of Dispersion:
Variance, Standard Deviation
Description:
To compute the central tendency measures (mean, median, mode) and measure of
dispersion (variance, standard deviation) for a dataset in Python, we can use basic Python
functions and libraries such as numpy and scipy. Here's how you can compute these measures:
Central Tendency Measures:
1. Mean: The arithmetic average of a dataset.
2. Median: The middle value when the data is sorted in ascending order.
3. Mode: The most frequently occurring value(s) in the dataset.
Measures of Dispersion:
1. Variance: A measure of how much the data points deviate from the mean.
2. Standard Deviation: The square root of the variance, providing a measure of the spread of
data points.
Program:
import numpy as np
from scipy import stats
data = [1, 5, 6, 7, 10, 10, 17, 19, 16, 28]
mean = [Link](data)
median = [Link](data)
mode_result = [Link](data)
if isinstance(mode_result.mode, [Link]):
mode = mode_result.mode[0]
else:
mode = mode_result.mode
print("Mode of the data:", mode)
variance = [Link](data)
std_deviation = [Link](data)
print(f"Mean: {mean}")
print(f"Median: {median}")
print(f"Mode: {mode}")
print(f"Variance: {variance}")
print(f"Standard Deviation: {std_deviation}")
Mode of the data: 10
Mean: 11.9
Median: 10.0
Mode: 10
Variance: 58.49000000000001
Standard Deviation: 7.647875521999558
[Link] the following Pre-processing techniques for a given dataset.
[Link] selection
[Link] Missing Values
[Link]
[Link] of Outliers

Aim:
To apply the following Pre-processing techniques for a given dataset.
a. Attribute selection
b. Handling Missing Values
c. Discretization

d. Elimination of Outliers
Description:
Pre processing is a crucial step in the machine learning pipeline to ensure the data is in a usable
format for training algorithms. Below, we'll demonstrate how to apply the following pre
processing techniques to a given dataset:
1. Attribute Selection: Selecting a subset of relevant features (attributes) for use in the
model.
2. Handling Missing Values: Dealing with missing or NaN values in the dataset.
3. Discretization: Converting continuous features into categorical bins or intervals.
4. Elimination of Outliers: Identifying and removing outliers that may affect the model's
performance.
Program:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from [Link] import SimpleImputer
from [Link] import KBinsDiscretizer
from scipy import stats
data = {
'age': [25, 30, 35, [Link], 40, 45, 50, 55, 60, 65],
'income': [50000, 55000, 60000, 65000, [Link], 70000, 75000, 80000, 85000, 90000],
'score': [70, 85, 90, 78, 88, 92, 76, 95, 91, 100],
'outlier_column': [10, 12, 11, 13, 10, 150, 10, 14, 11, 10] # Contains an outlier
}
df = [Link](data)
selected_features = df[['age', 'income', 'score']]
imputer = SimpleImputer(strategy='most_frequent')
df_imputed = [Link](imputer.fit_transform(selected_features),
columns=selected_features.columns)
discretizer = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='uniform')
df_imputed['age_binned'] = discretizer.fit_transform(df_imputed[['age']])
z_scores = [Link]([Link](df_imputed[['age', 'income', 'score']]))
df_no_outliers = df_imputed[(z_scores < 3).all(axis=1)]
print("Original DataFrame:")
print(df)
print("\nDataFrame after Attribute Selection and Imputation:")
print(df_imputed)
print("\nDataFrame after Discretization (Binned Age):")
print(df_imputed[['age', 'age_binned']])
print("\nDataFrame after Outlier Elimination:")
print(df_no_outliers)
Output:
Original DataFrame:
age income score outlier_column
0 25.0 50000.0 70 10
1 30.0 55000.0 85 12
2 35.0 60000.0 90 11
3 NaN 65000.0 78 13
4 40.0 NaN 88 10
5 45.0 70000.0 92 150
6 50.0 75000.0 76 10
7 55.0 80000.0 95 14
8 60.0 85000.0 91 11
9 65.0 90000.0 100 10

DataFrame after Attribute Selection and Imputation:


age income score age_binned
0 25.0 50000.0 70.0 0.0
1 30.0 55000.0 85.0 0.0
2 35.0 60000.0 90.0 0.0
3 25.0 65000.0 78.0 0.0
4 40.0 50000.0 88.0 1.0
5 45.0 70000.0 92.0 1.0
6 50.0 75000.0 76.0 1.0
7 55.0 80000.0 95.0 2.0
8 60.0 85000.0 91.0 2.0
9 65.0 90000.0 100.0 2.0
DataFrame after Discretization (Binned Age):
age age_binned
0 25.0 0.0
1 30.0 0.0
2 35.0 0.0
3 25.0 0.0
4 40.0 1.0
5 45.0 1.0
6 50.0 1.0
7 55.0 2.0
8 60.0 2.0
9 65.0 2.0

DataFrame after Outlier Elimination:


age income score age_binned
0 25.0 50000.0 70.0 0.0
1 30.0 55000.0 85.0 0.0
2 35.0 60000.0 90.0 0.0
3 25.0 65000.0 78.0 0.0
4 40.0 50000.0 88.0 1.0
5 45.0 70000.0 92.0 1.0
6 50.0 75000.0 76.0 1.0
7 55.0 80000.0 95.0 2.0
8 60.0 85000.0 91.0 2.0
9 65.0 90000.0 100.0 2.0
3. Apply KNN algorithm for classification and regression
Aim:
1. KNN for Classification
We'll apply KNN to a classification problem using the famous Iris dataset, which has class labels
and numeric features.
2. KNN for Regression
We'll also apply KNN to a regression problem using a simple synthetic dataset, where the goal is
to predict a continuous target variable.
Description
KNN for Classification:
 We use the Iris dataset, which is a well-known dataset for classification tasks.
 We split the data into training and testing sets using train_test_split.
 Standard Scaler is used to standardize the features, which is important for KNN since it’s
distance-based.
 We initialize the K Neighbors Classifier with 5 neighbors and fit it to the training data.
 After predicting the labels for the test set, we calculate the accuracy using accuracy_score
to evaluate the model.
KNN for Regression:
 For regression, we generate a simple synthetic dataset. The feature X_reg is a set of
random numbers, and the target y_reg is linearly dependent on X_reg with some added
Gaussian noise.
 Similar to classification, we split the dataset, standardize the features, and initialize the
KNeighborsRegressor with 5 neighbors.
 We predict the target values and calculate the Mean Squared Error (MSE) to evaluate the
regression model.
 Finally, we plot the true vs. predicted values for the regression task to visualize the
performance of the KNN regressor.
Program:
import numpy as np
import pandas as pd
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import KNeighborsClassifier, KNeighborsRegressor
from [Link] import accuracy_score, mean_squared_error
import [Link] as plt
iris = load_iris()
X_class = [Link]
y_class = [Link]
X_train_class, X_test_class, y_train_class, y_test_class = train_test_split(X_class, y_class,
test_size=0.3, random_state=42)
scaler_class = StandardScaler()
X_train_class = scaler_class.fit_transform(X_train_class)
X_test_class = scaler_class.transform(X_test_class)
knn_classifier = KNeighborsClassifier(n_neighbors=5)
knn_classifier.fit(X_train_class, y_train_class)
y_pred_class = knn_classifier.predict(X_test_class)
accuracy = accuracy_score(y_test_class, y_pred_class)
print(f"KNN Classification Accuracy: {accuracy * 100:.2f}%")
[Link](42)
X_reg = [Link](100, 1) * 10
y_reg = 2 * X_reg + [Link](100, 1) * 2
X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(X_reg, y_reg, test_size=0.3,
random_state=42)
knn_regressor = KNeighborsRegressor(n_neighbors=5)
knn_regressor.fit(X_train_reg, y_train_reg)
y_pred_reg = knn_regressor.predict(X_test_reg)
mse = mean_squared_error(y_test_reg, y_pred_reg)
print(f"KNN Regression Mean Squared Error (MSE): {mse:.2f}")
[Link](figsize=(8, 6))
[Link](X_test_reg, y_test_reg, color='blue', label='True values')
[Link](X_test_reg, y_pred_reg, color='red', label='Predicted values')
[Link]('Feature')
[Link]('Target')
[Link]('KNN Regression: True vs Predicted Values')
[Link]()
[Link]()
4. Demonstrate decision tree algorithm for a classification problem and perform parameter
tuning for better results
Aim: To apply decisiontreealgorithmforaclassificationproblemandperformparameter tuning for
better results
Description:
Splitting the Dataset:
We use train_test_split from sklearn.model_selection to split the data into training and testing
sets. The test set size is 30% (test_size=0.3), and we set the random_state for reproducibility.
Training the Decision Tree:
We create a DecisionTreeClassifier object and fit it to the training data using the fit() method.
The random_state=42 ensures that the tree's structure is reproducible.
Making Predictions:
We use the trained Decision Tree model to make predictions on the test set using the predict()
method.
Model Evaluation:
We compute the accuracy using accuracy_score(), which measures how many predictions were
correct.
We generate a classification report using classification_report(), which provides metrics like
precision, recall, F1-score for each class.
We also display the confusion matrix using confusion_matrix(), which shows the number of true
positives, false positives, true negatives, and false negatives.
Visualizing the Decision Tree:
We visualize the Decision Tree using plot_tree() from [Link]. The feature_names parameter
adds the names of the features, and the class_names parameter labels the target classes (species).
The tree is visualized with color coding, showing how the splits happen based on feature values.
Parameter tuning
 criterion: The function to measure the quality of a split. gini is faster, and entropy
provides more interpretability in some cases.
 max_depth: Limits how deep the tree can grow. This helps prevent overfitting. None
means the tree will expand until each leaf is pure.
 min_samples_split: The minimum number of samples required to split an internal node.
Higher values can prevent the tree from growing too deep.
 min_samples_leaf: The minimum number of samples required at each leaf node. Setting
this to higher values can also help prevent over fitting.
 max_features: The number of features to consider when looking for the best split.
Limiting this can improve performance, especially for large datasets.
Program:
import numpy as np
import pandas as pd
from [Link] import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV
from [Link] import DecisionTreeClassifier
from [Link] import accuracy_score, classification_report
from [Link] import StandardScaler
import [Link] as plt
iris = load_iris()
data = [Link](data=[Link], columns=iris.feature_names)
X = [Link]
y = [Link]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
dt_classifier = DecisionTreeClassifier(random_state=42)
param_grid = {
'criterion': ['gini', 'entropy'],
'max_depth': [3, 5, 10, None],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 5],
'max_features': [None, 'sqrt', 'log2'],
grid_search = GridSearchCV(estimator=dt_classifier, param_grid=param_grid, cv=5,
scoring='accuracy', n_jobs=-1)
grid_search.fit(X_train, y_train)
print("Best Parameters Found by GridSearchCV:", grid_search.best_params_)
best_dt_classifier = grid_search.best_estimator_
y_pred = best_dt_classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy of the Decision Tree Classifier: {accuracy * 100:.2f}%")
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
from [Link] import plot_tree
[Link](figsize=(12, 8))
plot_tree(best_dt_classifier, filled=True, feature_names=iris.feature_names,
class_names=iris.target_names,
rounded=True, fontsize=10)
[Link]("Decision Tree Visualization")
[Link]()
Output:
Best Parameters Found by GridSearchCV: {'criterion': 'gini', 'max_depth': 5, 'max_features':
None, 'min_samples_leaf': 1, 'min_samples_split': 10}
Accuracy of the Decision Tree Classifier: 100.00%

Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 19


1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 13

accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45
[Link] decision tree algorithm for a regression problem
Aim: To demonstrate decision tree algorithm for are gression problem
Description:
The decision tree algorithm can be used for regression problems to predict a continuous target
variable. In a regression decision tree, the data is split into subsets based on the feature that
minimizes the variance (or another criterion like mean squared error, MSE) in each subset. This
process continues until the tree reaches a stopping criterion, such as a maximum depth or minimum
number of data points in a node.
Steps in a Decision Tree Regression:
1. Choose a feature to split on: At each node, the decision tree algorithm chooses the feature
that minimizes the sum of squared errors (SSE) or variance within the resulting subsets.
2. Split the data: Based on the chosen feature, the data is split into two subsets.
3. Repeat the process: This splitting process is recursively applied to each subset.
4. Stopping condition: The process stops when a certain condition is met (e.g., maximum tree
depth or minimum samples per leaf).
Program:
import numpy as np
import pandas as pd
from [Link] import make_regression
from sklearn.model_selection import train_test_split
from [Link] import DecisionTreeRegressor
from [Link] import mean_squared_error, r2_score
from [Link] import StandardScaler
import [Link] as plt
data=pd.read_csv("/content/housing_large_data.csv")
X=data[['housing_median_age','total_rooms','total_bedrooms','population','households','median_inc
ome']]
y=data[['median_house_value']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
dt_regressor = DecisionTreeRegressor(random_state=42)
dt_regressor.fit(X_train, y_train)
y_pred = dt_regressor.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error (MSE): {mse:.2f}")
print(f"R-squared (R²) Score: {r2:.2f}")
X_grid = [Link](X['housing_median_age'].min(), X['housing_median_age'].max(), 0.01)[:,
[Link]]
X_grid_df = [Link](X_grid, columns=['housing_median_age'])
for col in [Link][1:]:
X_grid_df[col] = 0
y_grid_pred = dt_regressor.predict([Link](X_grid_df))
[Link](figsize=(10, 6))
[Link](X['housing_median_age'], y, color='red', label='Data')
[Link](X_grid, y_grid_pred, color='blue', label='Model Prediction')
[Link]("Decision Tree Regression")
[Link]("Housing Median Age")
[Link]("Target")
[Link]()
[Link]()
from [Link] import plot_tree
[Link](figsize=(12, 8))
plot_tree(dt_regressor, filled=True, feature_names=X_train,
rounded=True, fontsize=10)
[Link]("Decision Tree Regression Visualization")
[Link]()
6. Apply Random Forest algorithm for classification and regression
Aim: To apply Random Forest algorithm for classification and regression
Description:
 Random Forest for Classification:
 We use the Iris dataset, which is a commonly used dataset for classification tasks. It has
multiple class labels (species of flowers).
 The dataset is split into training and test sets (70% training and 30% testing) using
train_test_split.
 The features are standardized using StandardScaler (optional for Random Forest, but
useful for algorithms that are sensitive to scale).
 A RandomForestClassifier with 100 trees is initialized and trained on the training set.
 After training, the model is evaluated on the test set using accuracy.
 Random Forest for Regression:
 For the regression task, we create a synthetic dataset using numpy. The features (X_reg)
are random numbers between 0 and 10, and the target (y_reg) has a linear relationship
with added Gaussian noise.
 The dataset is again split into training and test sets.
 A RandomForestRegressor is initialized and trained with 100 trees.
 The model is then evaluated using Mean Squared Error (MSE) and R-squared (R²) score.
 Visualizing the Results:
 For the regression task, we plot the true vs. predicted values to visualize how well the
Random Forest model performs.
 The plot shows true values in blue and predicted values in red.
Program:
import numpy as np
import pandas as pd
from [Link] import load_iris, make_regression
from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier, RandomForestRegressor
from [Link] import accuracy_score, mean_squared_error, r2_score
from [Link] import StandardScaler
import [Link] as plt
data=pd.read_csv("/content/iris (2).csv")
X=data[['sepal length','sepal width','petal length','petal width']]
y=data['class']
X_train_class, X_test_class, y_train_class, y_test_class = train_test_split(X, y, test_size=0.3,
random_state=42)
scaler_class = StandardScaler()
X_train_class = scaler_class.fit_transform(X_train_class)
X_test_class = scaler_class.transform(X_test_class)
rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)
rf_classifier.fit(X_train_class, y_train_class)
y_pred_class = rf_classifier.predict(X_test_class)
accuracy = accuracy_score(y_test_class, y_pred_class)
print(f"Random Forest Classification Accuracy: {accuracy * 100:.2f}%")
data=pd.read_csv("/content/housing_large_data.csv")
X=data[['housing_median_age','total_rooms','total_bedrooms','population','households','median_i
ncome']]
y=data['median_house_value']
X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(X, y, test_size=0.3,
random_state=42)
rf_regressor = RandomForestRegressor(n_estimators=100, random_state=42)
rf_regressor.fit(X_train_reg, y_train_reg.ravel()) # Flatten y_train_reg since it's a 2D array
y_pred_reg = rf_regressor.predict(X_test_reg)
mse = mean_squared_error(y_test_reg, y_pred_reg)
r2 = r2_score(y_test_reg, y_pred_reg)
print(f"Random Forest Regression Mean Squared Error (MSE): {mse:.2f}")
print(f"Random Forest Regression R-squared (R²) Score: {r2:.2f}")
[Link](figsize=(8, 6))
for feature in X_test_reg.columns:
[Link](X_test_reg[feature].values, y_test_reg.values, color='blue', label='True values',
alpha=0.5)
[Link](X_test_reg[feature].values, y_pred_reg, color='red', label='Predicted values',
alpha=0.5)
[Link](feature)
[Link]('Target')
[Link](f'Random Forest Regression: True vs Predicted Values ({feature})')
[Link]()
[Link]()
7. Demonstrate Naïve Bayes Classification algorithm
Description:
 Data Preprocessing:
 The data is split into training and test sets using train_test_split with 30% of the data
reserved for testing (test_size=0.3).
 We standardize the features using StandardScaler. Although Naïve Bayes does not require
scaling, it is useful for visualizations and other algorithms.
 Model Initialization:
 We initialize the Gaussian Naïve Bayes model using GaussianNB() from scikit-learn.
This variant assumes the features follow a Gaussian (normal) distribution, which is
reasonable for continuous data like the Iris dataset.
 Model Training:
 We train the model using nb_classifier.fit(X_train, y_train), where X_train is the training
features, and y_train is the target labels.
 Model Prediction:
 We make predictions on the test set using nb_classifier.predict(X_test).
 Model Evaluation:
 We calculate the accuracy of the model using accuracy_score, which compares the
predicted labels with the actual labels.
 We also display a classification report using classification_report, which includes metrics
such as precision, recall, and F1-score.
 A confusion matrix is displayed using confusion_matrix, which shows how many
samples were correctly and incorrectly classified.
 Visualization:
 We plot a scatter plot of the test data points, where the true labels are shown in one color,
and the predicted labels are shown in another color.
Program:
import numpy as np
import pandas as pd
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score, classification_report, confusion_matrix
from [Link] import StandardScaler
import [Link] as plt

data = pd.read_csv("/content/[Link]")
X = data[['sepal length', 'sepal width', 'petal length', 'petal width']]
y = data['class']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
nb_classifier = GaussianNB()
nb_classifier.fit(X_train, y_train)
y_pred = nb_classifier.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Naïve Bayes Classification Accuracy: {accuracy * 100:.2f}%")
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
[Link](figsize=(8, 6))

# Convert string labels to numerical representation for color mapping


from [Link] import LabelEncoder
le = LabelEncoder()
y_test_numeric = le.fit_transform(y_test)
y_pred_numeric = [Link](y_pred)

[Link](X_test[:, 0], X_test[:, 1], c=y_test_numeric, cmap='viridis', marker='o', label="True


values")
[Link](X_test[:, 0], X_test[:, 1], c=y_pred_numeric, cmap='coolwarm', marker='x',
label="Predicted values")
[Link]('Sepal Length')
[Link]('Sepal Width')
[Link]('Naïve Bayes Classification: True vs Predicted')
[Link]()
[Link]()
Output:
Naïve Bayes Classification Accuracy: 97.78%

Classification Report:
precision recall f1-score support

Iris-setosa 1.00 1.00 1.00 19


Iris-versicolor 1.00 0.92 0.96 13
Iris-virginica 0.93 1.00 0.96 13

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

Confusion Matrix:
[[19 0 0]
[ 0 12 1]
[ 0 0 13]]
8. Apply Support Vector algorithm for classification
Description:
The Support Vector Machine (SVM) is a powerful classification algorithm that works by finding
the hyperplane that best separates different classes in the feature space. In this code example, we
will apply Support Vector Classification (SVC) to a dataset, using the popular Iris dataset available
in scikit-learn.
 Data Preprocessing:
 The dataset is split into training and test sets using train_test_split with 30% of the data
reserved for testing.
 We use StandardScaler to standardize the features. This is an important preprocessing step
because SVMs perform better when the features are on a similar scale.
 Model Initialization:
 We initialize the Support Vector Classifier (SVC) from scikit-learn. Here, we use a linear
kernel, which is appropriate for linearly separable data. However, you can also experiment
with other kernels like polynomial or RBF (Radial Basis Function).
 Model Training:
 We train the model using [Link](X_train, y_train), where X_train is the training set of
features and y_train is the corresponding target labels.
 Prediction:
 After training the model, we use it to predict the target labels for the test set (X_test) using
[Link](X_test).
 Model Evaluation:
 We evaluate the model using accuracy, classification report, and confusion matrix:
o Accuracy: The proportion of correctly predicted samples.
o Classification Report: Displays precision, recall, F1-score for each class.
o Confusion Matrix: A matrix that shows the number of correct and incorrect
predictions for each class.
 Visualization:
 Since the Iris dataset has four features, we reduce the dimensionality by using only the first
two features (X_test[:, 0] and X_test[:, 1]) to create a 2D scatter plot.
 True labels are shown in one color, and predicted labels are shown in another color,
allowing us to visually compare the true vs. predicted values.

Program:
import numpy as np
import pandas as pd
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import SVC
from [Link] import accuracy_score, classification_report, confusion_matrix
from [Link] import StandardScaler
import [Link] as plt

data = pd.read_csv("/content/[Link]")
X = data[['sepal length', 'sepal width', 'petal length', 'petal width']]
y = data['class']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
svc = SVC(kernel='linear', random_state=42)
[Link](X_train, y_train)
y_pred = [Link](X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Support Vector Classification Accuracy: {accuracy * 100:.2f}%")
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
[Link](figsize=(8, 6))

from [Link] import LabelEncoder


le = LabelEncoder()
y_test_numeric = le.fit_transform(y_test)
y_pred_numeric = [Link](y_pred)

[Link](X_test[:, 0], X_test[:, 1], c=y_test_numeric, cmap='viridis', marker='o', label="True


values")
[Link](X_test[:, 0], X_test[:, 1], c=y_pred_numeric, cmap='coolwarm', marker='x',
label="Predicted values")
[Link]('Sepal Length')
[Link]('Sepal Width')
[Link]('Support Vector Machine Classification: True vs Predicted')
[Link]()
[Link]()
Output:
Support Vector Classification Accuracy: 97.78%

Classification Report:
precision recall f1-score support

Iris-setosa 1.00 1.00 1.00 19


Iris-versicolor 1.00 0.92 0.96 13
Iris-virginica 0.93 1.00 0.96 13

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

Confusion Matrix:
[[19 0 0]
[ 0 12 1]
[ 0 0 13]]
9. Demonstrate simple linear regression algorithm for a regression problem
Description:
 Train-Test Split:
 We use train_test_split from scikit-learn to split the data into training and testing sets. We
use 70% of the data for training and 30% for testing (test_size=0.3).
 Model Initialization and Training:
 We initialize the LinearRegression model from scikit-learn and train it using
[Link](X_train, y_train) on the training data.
 Prediction:
 After training, we use the model to predict the target variable for the test data (y_pred =
[Link](X_test)).
 Model Evaluation:
 Mean Squared Error (MSE): We calculate the Mean Squared Error using
mean_squared_error(). A lower MSE indicates a better fit of the model to the data.
 R-squared (R²): We also calculate the R-squared value using r2_score(). The R² value
explains the proportion of the variance in the dependent variable that is predictable from
the independent variable. A value closer to 1 indicates a good fit.
 Visualization:
 We plot the true values of the test set as blue dots and the regression line (predicted
values) as a red line.
 This allows us to visually compare how well the model fits the data.
Program:
import numpy as np
import pandas as pd
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
data=pd.read_csv("/content/housing_large_data.csv")
X=data[['housing_median_age','total_rooms','total_bedrooms','population','households','median_i
ncome']]
y=data['median_house_value']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
model = LinearRegression()
[Link](X_train, y_train)
y_pred = [Link](X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse:.2f}")
print(f"R-squared Value: {r2:.2f}")
[Link](figsize=(8,6))
[Link](X_test['housing_median_age'], y_test, color='blue', label='True Values')
[Link](X_test['housing_median_age'], y_pred, color='red', label='Predicted Values')
[Link]('Simple Linear Regression: True vs Predicted')
[Link]('Independent Variable (X)')
[Link]('Dependent Variable (y)')
[Link]()
[Link]()
10. Apply Logistic regression algorithm for a classification problem
Description:

Logistic Regression is a popular and straightforward algorithm used for classification problems
in machine learning. Despite its name, Logistic Regression is actually a classification algorithm,
not a regression algorithm. Here’s a brief description:

Purpose: Logistic Regression predicts the probability of an instance belonging to a specific class.
It is widely used for binary classification problems but can be extended to multiclass
classification using techniques like one-vs-rest or softmax regression.

Working Principle:

Logistic Regression models the relationship between the independent variables (features) and the
dependent variable (target) using a logistic/sigmoid function.

The sigmoid function maps any real-valued number into a range between 0 and 1, representing
probabilities.

The predicted probability is then used to classify the instance into a specific class (e.g., 0 or 1).
For example, if the probability is greater than 0.5, the instance is classified as 1; otherwise, it is
classified as 0.

Program:

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split

from [Link] import StandardScaler

from sklearn.linear_model import LogisticRegression

from [Link] import accuracy_score, confusion_matrix, classification_report

from [Link] import load_iris

data=pd.read_csv("/content/[Link]")

X=data[['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigree
Function','Age','Outcome']]
y=data['Outcome']

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

scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)

X_test = [Link](X_test)

model = LogisticRegression()

[Link](X_train, y_train)

y_pred = [Link](X_test)

accuracy = accuracy_score(y_test, y_pred)

conf_matrix = confusion_matrix(y_test, y_pred)

class_report = classification_report(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")

print(f"Confusion Matrix:\n{conf_matrix}")

print(f"Classification Report:\n{class_report}")
Output:

Accuracy: 1.00

Confusion Matrix:

[[99 0]

[ 0 55]]

Classification Report:

precision recall f1-score support

0 1.00 1.00 1.00 99

1 1.00 1.00 1.00 55

accuracy 1.00 154

macro avg 1.00 1.00 1.00 154

weighted avg 1.00 1.00 1.00 154


11. DemonstrateMulti-layerPerceptronalgorithmforaclassificationproblem
Description:
1. Dataset:
 This example uses the Iris dataset, a well-known multi-class classification
problem where we classify iris flowers into three species based on features like
sepal length, sepal width, petal length, and petal width.
2. Feature Scaling:
 Neural networks, including MLP, generally perform better when the data is
scaled. Hence, StandardScaler is used to scale the feature data (standardize it to
have mean = 0 and standard deviation = 1).
3. MLP Model:
 We initialize the MLPClassifier from sklearn.neural_network. The key
parameters:
 hidden_layer_sizes: A tuple representing the number of neurons in each
hidden layer. Here, we use one hidden layer with 10 neurons.
 max_iter: The maximum number of iterations for training the model
(default is 200, but here we use 1000).
 random_state: Used for reproducibility of the results.
4. Model Training:
 The model is trained using mlp_model.fit() on the training data (X_train, y_train).
5. Prediction and Evaluation:
 The model's performance is evaluated using accuracy, a confusion matrix, and a
classification report, which gives more detailed metrics such as precision, recall,
and F1-score.
Program:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from sklearn.neural_network import MLPClassifier
from [Link] import accuracy_score, confusion_matrix, classification_report
from [Link] import load_iris
data=pd.read_csv("/content/iris (2).csv")
X=data[['sepal length','sepal width','petal length','petal width']]
y=data['class']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
mlp_model = MLPClassifier(hidden_layer_sizes=(10,), max_iter=1000, random_state=42)
mlp_model.fit(X_train, y_train)
y_pred = mlp_model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
class_report = classification_report(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print(f"Confusion Matrix:\n{conf_matrix}")
print(f"Classification Report:\n{class_report}")
Output:
Accuracy: 1.00
Confusion Matrix:
[[10 0 0]
[ 0 9 0]
[ 0 0 11]]
Classification Report:
precision recall f1-score support

Iris-setosa 1.00 1.00 1.00 10


Iris-versicolor 1.00 1.00 1.00 9
Iris-virginica 1.00 1.00 1.00 11

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
12. Implement the K-means algorithm and apply it to the data you selected. Evaluate
performancebymeasuringthesumoftheEuclideandistanceofeachexamplefrom its class
center. Test the performance of the algorithm as a function of the parameters K

Description:
1. Dataset Loading:
 The load_iris() function from [Link] is used to load the Iris dataset.
 We use X (the features) for clustering, and y (the true labels) for evaluation (though K-
means is unsupervised, the labels are useful for performance comparison).
2. Feature Scaling:
 Since K-means is sensitive to the scale of the data, we apply StandardScaler to
standardize the features (zero mean and unit variance).
3. Sum of Euclidean Distances:
 After clustering, we compute the Euclidean distance between each data point and its
corresponding cluster center. The Euclidean distance for a data point xixi and its cluster
center ckck is given by:

 We compute this for each data point and sum the distances to get a measure of
clustering quality.
4. K-means Clustering:
 The K-means algorithm is applied for different values of KK (from 1 to 10).
 For each KK, we fit the K-means model on the scaled data and compute the sum of
Euclidean distances for each cluster configuration.
5. Evaluation:
 We evaluate the performance by calculating and printing the sum of Euclidean
distances for each value of KK.
 A plot is generated to show how the sum of Euclidean distances changes with the
number of clusters KK. This is useful for determining the optimal number of clusters
using the Elbow Method.
Program:
import numpy as np
import pandas as pd
from [Link] import load_iris
from [Link] import StandardScaler
from [Link] import KMeans
import [Link] as plt
iris = load_iris()
data=pd.read_csv("/content/[Link]")
X=data[['sepal length','sepal width','petal length','petal width']]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
def compute_sum_of_distances(X, kmeans):
distances = [Link](X - kmeans.cluster_centers_[kmeans.labels_], axis=1)
return [Link](distances)
k_values = range(1, 11)
distances = []
for k in k_values:
kmeans = KMeans(n_clusters=k, random_state=42)
[Link](X_scaled)
sum_distances = compute_sum_of_distances(X_scaled, kmeans)
[Link](sum_distances)
print(f"Sum of Euclidean distances for K={k}: {sum_distances:.2f}")
[Link](figsize=(8, 6))
[Link](k_values, distances, marker='o')
[Link]('Sum of Euclidean Distances vs. Number of Clusters (K)')
[Link]('Number of Clusters (K)')
[Link]('Sum of Euclidean Distances')
[Link](True)
[Link]()
[Link] the use of Fuzzy C-Means Clustering

Description:
Fuzzy C-Means (FCM) is a clustering algorithm similar to K-means, but it allows for partial
membership of data points in multiple clusters rather than assigning each point to exactly one
cluster. This makes it useful in situations where clusters are not well-separated.
In this implementation, we will use the Fuzzy C-Means clustering algorithm available in
the fcmeans library (a Python library for Fuzzy C-Means). If you haven't already installed it, you
can install it via pip:
pip install fcmeans
1. Standardization:
 The features are scaled using StandardScaler to make sure the Fuzzy C-Means
algorithm performs optimally, as it is sensitive to the scale of the data.
2. Fuzzy C-Means (FCM) Clustering:
 We initialize the FCM class from the fcmeans package and fit it to the data. The
number of clusters is set to 3 (since we generated data with 3 centers).
 After fitting, we get the cluster centers and the membership matrix, which indicates
the degree to which each point belongs to each cluster. Each point can belong to
multiple clusters with varying degrees of membership.
3. Prediction:
 We predict the cluster label for each point. The label is assigned based on the
maximum membership value (the cluster the point is most strongly associated
with).
4. Visualization:
 We use matplotlib to plot the data points and color them according to the predicted
cluster labels.
 The cluster centers are marked with red X markers for visualization.
5. Membership Matrix:
 The membership matrix is printed for the first 5 data points. This shows the degree
of membership for each point in each cluster. Each row of the matrix corresponds to
a data point, and each column corresponds to a cluster. The values are between 0
and 1, where higher values indicate stronger membership.
Program:
import numpy as np

import pandas as pd

import [Link] as plt

from fcmeans import FCM

from [Link] import make_blobs

from [Link] import StandardScaler

data=pd.read_csv("/content/iris (1).csv")

X=data[['sepal length','sepal width','petal length','petal width']]

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

fcm = FCM(n_clusters=3) # Number of clusters

[Link](X_scaled)

centers = [Link]

membership = fcm.u

labels = [Link](X_scaled)

[Link](figsize=(8, 6))

[Link](X_scaled[:, 0], X_scaled[:, 1], c=labels, cmap='viridis', alpha=0.6)

[Link](centers[:, 0], centers[:, 1], marker='X', s=200, c='red', label='Cluster Centers')

[Link]("Fuzzy C-Means Clustering")

[Link]("Feature 1")

[Link]("Feature 2")

[Link]()

[Link]()
[Link] the use of Expectation Maximization based clustering algorithm

Description:
EM-based Clustering (Gaussian Mixture Model):
1. Fit the Gaussian Mixture Model (GMM) on the data.
2. Predict cluster labels for each data point.
3. Visualize the clustering result.
4. Evaluate the performance by comparing the predicted labels with true labels (optional)
Gaussian Mixture Model (GMM):
1. We initialize and fit the GaussianMixture class from [Link] to
the standardized data. The n_components=3 parameter specifies that we
want to fit 3 Gaussian distributions (clusters).
2. The fit() method estimates the parameters of the Gaussians, including the
mean vectors and covariance matrices.
Prediction:
3. We use [Link]() to predict the most likely cluster for each data
point.
4. We also use gmm.predict_proba() to get the probability that each point
belongs to each cluster. This is a soft clustering result, where each point
has a probability of belonging to each cluster.
Visualization:
5. We plot the data points, color-coded according to the predicted cluster
labels using matplotlib.
6. The cluster centers are marked with red X markers. These are the means
of the Gaussian distributions estimated by GMM.
Cluster Parameters:
7. After fitting the GMM, we print the means (centers of the Gaussian
distributions) and the covariances of the distributions. These represent the
cluster parameters that GMM has learned.

Program:
import numpy as np
import pandas as pd
import [Link] as plt
from [Link] import make_blobs
from [Link] import GaussianMixture
from [Link] import StandardScaler
data=pd.read_csv("/content/iris (1).csv")
X=data[['sepal length','sepal width','petal length','petal width']]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
gmm = GaussianMixture(n_components=3, random_state=42)
[Link](X_scaled)
labels = [Link](X_scaled)
[Link](figsize=(8, 6))
[Link](X_scaled[:, 0], X_scaled[:, 1], c=labels, cmap='viridis', alpha=0.6)
centers = gmm.means_
[Link](centers[:, 0], centers[:, 1], marker='X', s=200, c='red', label='Cluster Centers')
[Link]("Expectation Maximization (GMM) Clustering")
[Link]("Feature 1")
[Link]("Feature 2")
[Link]()
[Link]()
print("Cluster Centers (Means of Gaussians):")
print(centers)
print("\nCovariances of the Gaussians:")
print(gmm.covariances_)
probs = gmm.predict_proba(X_scaled)
print("\nProbabilities of the first 5 points belonging to each cluster:")
print(probs[:5])
Output:
Cluster Centers (Means of Gaussians):
[[ 0.50728932 -0.42115345 0.65243901 0.62756416]
[-0.93419037 0.98506351 -1.30343641 -1.24712613]
[-1.58675099 -0.17377379 -1.31514142 -1.31208711]]

Covariances of the Gaussians:


[[[ 0.63858859 0.33908776 0.3092561 0.26362763]
[ 0.33908776 0.58696549 0.18602473 0.24103728]
[ 0.3092561 0.18602473 0.21820031 0.21370592]
[ 0.26362763 0.24103728 0.21370592 0.30875537]]

[[ 0.14543753 0.21013666 0.00761694 0.01286596]


[ 0.21013666 0.61862353 0.00605242 0.03446383]
[ 0.00761694 0.00605242 0.00806003 0.00435139]
[ 0.01286596 0.03446383 0.00435139 0.02091513]]

[[ 0.04264506 0.07846147 0.02755141 0.00491717]


[ 0.07846147 0.60380762 0.0673137 -0.03450835]
[ 0.02755141 0.0673137 0.01996776 0.00225095]
[ 0.00491717 -0.03450835 0.00225095 0.00549205]]]

Probabilities of the first 5 points belonging to each cluster:


[[1.95129123e-11 1.00000000e+00 9.29163538e-38]
[9.32061473e-08 9.99999907e-01 2.68745861e-12]
[6.76815686e-09 9.99999991e-01 2.71301757e-09]
[2.71503652e-08 1.37278505e-01 8.62721468e-01]
[6.98898409e-12 1.00000000e+00 1.13468595e-32]]

You might also like