0% found this document useful (0 votes)
16 views10 pages

FDS Lab Manual

Uploaded by

bharathkatamneni
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)
16 views10 pages

FDS Lab Manual

Uploaded by

bharathkatamneni
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
You are on page 1/ 10

EXPERIMENT-4:

Write a python program to implement simple linear regression

import numpy as np

class SimpleLinearRegression:

def __init__(self):

self.intercept_ = None

self.coefficient_ = None

def fit(self, X, y):

# Check if X is a 1D array, if so, reshape to a 2D array

if len(X.shape) == 1:

X = X.reshape(-1, 1)

# Add a column of ones to X for the intercept term

X_with_intercept = np.c_[np.ones(X.shape[0]), X]

# Calculate coefficients using ordinary least squares

coefficients =
np.linalg.inv(X_with_intercept.T.dot(X_with_intercept)).dot(X_with_intercept.T).dot(y)

# Set intercept and coefficient

self.intercept_ = coefficients[0]

self.coefficient_ = coefficients[1]

def predict(self, X):

if self.intercept_ is None or self.coefficient_ is None:


raise Exception("Model not fitted yet. Call fit() method first.")

# Check if X is a 1D array, if so, reshape to a 2D array

if len(X.shape) == 1:

X = X.reshape(-1, 1)

return self.intercept_ + self.coefficient_ * X

# Example usage:

if __name__ == "__main__":

# Sample data

X = np.array([1, 2, 3, 4, 5])

y = np.array([2, 3, 4, 5, 6])

# Initialize and fit the model

model = SimpleLinearRegression()

model.fit(X, y)

# Predict for new data points

X_new = np.array([6, 7, 8])

predictions = model.predict(X_new)

print("Predictions:", predictions)

OUTPUT:

Predictions: [[7.]
[8.]
[9.]]
EXPERIMENT-6:

Implementation of decision tree using sklearn and its parameter tuning

import numpy as np

import pandas as pd

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split, GridSearchCV

from sklearn.tree import DecisionTreeClassifier

from sklearn.metrics import accuracy_score

# Load the Iris dataset

iris = load_iris()

X = iris.data

y = iris.target

# Split the dataset into training and testing sets

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

# Define the parameter grid for tuning

param_grid = {

'criterion': ['gini', 'entropy'],

'max_depth': [None, 5, 10, 15],

'min_samples_split': [2, 5, 10],

'min_samples_leaf': [1, 2, 4]

# Initialize the decision tree classifier

dtree = DecisionTreeClassifier()

# Initialize GridSearchCV for hyperparameter tuning

grid_search = GridSearchCV(estimator=dtree, param_grid=param_grid, cv=5)


# Fit the GridSearchCV to the training data

grid_search.fit(X_train, y_train)

# Print the best parameters found

print("Best Parameters:", grid_search.best_params_)

# Get the best estimator

best_dtree = grid_search.best_estimator_

# Make predictions on the testing set

y_pred = best_dtree.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print("\nAccuracy:", accuracy)

OUTPUT:

Best Parameters: {'criterion': 'entropy', 'max_depth': None, 'min_samples_leaf': 4,


'min_samples_split': 2}

Accuracy: 1.0
EXPERIMENT-7:

Implementation of KNN using sklearn

import numpy as np

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.neighbors import KNeighborsClassifier

from sklearn.metrics import accuracy_score

# Load the Iris dataset

iris = load_iris()

X = iris.data

y = iris.target

# Split the dataset into training and testing sets

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

# Initialize the kNN classifier with k=3

k=3

knn_classifier = KNeighborsClassifier(n_neighbors=k)

# Fit the classifier to the training data

knn_classifier.fit(X_train, y_train)

# Make predictions on the testing set

y_pred = knn_classifier.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

OUTPUT:

Accuracy: 1.0
EXPERIMENT-8:

Implementation of logistic regression using sklearn

import numpy as np

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score, classification_report

# Load the Iris dataset

iris = load_iris()

X = iris.data

y = iris.target

# Split the dataset into training and testing sets

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

# Initialize the logistic regression classifier

logistic_regression = LogisticRegression()

# Fit the classifier to the training data

logistic_regression.fit(X_train, y_train)

# Make predictions on the testing set

y_pred = logistic_regression.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

# Display classification report

print("\nClassification Report:")

print(classification_report(y_test, y_pred))
OUTPUT:

Accuracy: 1.0

Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 10


1 1.00 1.00 1.00 9
2 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

EXPERIMENT-10:

Performance analysis of classification algoritms on a specific dataset

import numpy as np

import pandas as pd

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

from sklearn.neighbors import KNeighborsClassifier

from sklearn.tree import DecisionTreeClassifier

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score, classification_report

# Load the Iris dataset

iris = load_iris()

X = iris.data

y = iris.target

# Split the dataset into training and testing sets

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

# Initialize classifiers

classifiers = {
"Logistic Regression": LogisticRegression(),

"k-Nearest Neighbors": KNeighborsClassifier(),

"Decision Tree": DecisionTreeClassifier(),

"Random Forest": RandomForestClassifier()

# Train and evaluate each classifier

results = {}

for name, clf in classifiers.items():

clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)

report = classification_report(y_test, y_pred, target_names=iris.target_names)

results[name] = {"Accuracy": accuracy, "Classification Report": report}

# Display results

for name, result in results.items():

print(f"Classifier: {name}")

print(f"Accuracy: {result['Accuracy']:.4f}")

print("Classification Report:")

print(result["Classification Report"])

print("\n")

OUTPUT:

Classifier: Logistic Regression


Accuracy: 1.0000
Classification Report:
precision recall f1-score support
setosa 1.00 1.00 1.00 10
versicolor 1.00 1.00 1.00 9
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

Classifier: k-Nearest Neighbors


Accuracy: 1.0000
Classification Report:
precision recall f1-score support

setosa 1.00 1.00 1.00 10


versicolor 1.00 1.00 1.00 9
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

Classifier: Decision Tree


Accuracy: 1.0000
Classification Report:
precision recall f1-score support

setosa 1.00 1.00 1.00 10


versicolor 1.00 1.00 1.00 9
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

Classifier: Random Forest


Accuracy: 1.0000
Classification Report:
precision recall f1-score support
setosa 1.00 1.00 1.00 10
versicolor 1.00 1.00 1.00 9
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
EXPERIMENT-9
Implementation of K-means clustering
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans

# Generate synthetic data


X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)

# Initialize and fit the k-Means model


kmeans = KMeans(n_clusters=4)
kmeans.fit(X)

# Get the cluster centers and labels


centers = kmeans.cluster_centers_
labels = kmeans.labels_

# Visualize the clusters


plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', s=50, alpha=0.5)
plt.scatter(centers[:, 0], centers[:, 1], c='red', marker='*', s=200, label='Centroids')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('k-Means Clustering')
plt.legend()
plt.show()
OUTPUT:

You might also like