0% found this document useful (0 votes)
8 views

ML Lab PT

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)
8 views

ML Lab PT

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

S.

NO TITLE DATE PAGE SIGN


NO
EXP NO: 1 Solving Regression & Classification using
DATE: Decision Trees

AIM:

To execute a python program for solving regression and classification using decision tree.

PROGRAM CODE:

from sklearn.datasets import load_iris


from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.metrics import accuracy_score, mean_squared_error

# Load the Iris dataset


iris = load_iris()
X = iris.data
y_classification = iris.target
y_regression = iris.target

# Classification
print("Classification Example:")
# Split the dataset for classification with 70% training and 30% testing
X_train_clf, X_test_clf, y_train_clf, y_test_clf = train_test_split(X, y_classification, test_size=0.14,
random_state=2)

# Create and train the decision tree classifier


clf = DecisionTreeClassifier()
clf.fit(X_train_clf, y_train_clf)

# Make predictions on the testing set for classification


y_pred_clf = clf.predict(X_test_clf)

# Calculate accuracy for classification


accuracy_clf = accuracy_score(y_test_clf, y_pred_clf)
print("Accuracy:", accuracy_clf)

# Regression
print("\nRegression Example:")
# Split the dataset for regression with 70% training and 30% testing
X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(X, y_regression, test_size=0.31,
random_state=42)

# Create and train the decision tree regressor


regressor = DecisionTreeRegressor()
regressor.fit(X_train_reg, y_train_reg)

# Make predictions on the testing set for regression


y_pred_reg = regressor.predict(X_test_reg)
# Calculate mean squared error for regression
mse_reg = mean_squared_error(y_test_reg, y_pred_reg)
print("Mean Squared Error:", mse_reg
OUTPUT:

RESULT:

Thus, the program was executed and the result was obtained successfully.
EXP NO: 2 Root Node Attribute Selection for Decision Trees
DATE: using Information Gain

AIM:

To implement a python program for root note attribute selection for decision trees using
information gain.

PROGRAM CODE:

# Calculate the unique values and their counts for the selected attribute
attribute_values, counts = np.unique(data[:, attribute_index], return_counts=True)

# Calculate the weighted entropy for each value of the attribute


weighted_entropy = 0
for value, count in zip(attribute_values, counts):
subset_indices = np.where(data[:, attribute_index] == value)[0]
subset_target = target[subset_indices]
subset_entropy = entropy(subset_target)
weighted_entropy += (count / len(data)) * subset_entropy

# Calculate information gain


information_gain = total_entropy - weighted_entropy
return information_gain

# Function to select the best attribute for the root node


def select_root_attribute(data, target):
num_attributes = data.shape[1]
gains = []
for i in range(num_attributes):
gain = information_gain(data, target, i)
gains.append(gain)
best_attribute_index = np.argmax(gains)
return best_attribute_index

# Example dataset
data = np.array([
[1, 'Sunny', 'Hot', 'High', 'Weak'],
[2, 'Sunny', 'Hot', 'High', 'Strong'],
[3, 'Overcast', 'Hot', 'High', 'Weak'],
[4, 'Rain', 'Mild', 'High', 'Weak'],
[5, 'Rain', 'Cool', 'Normal', 'Weak'],
[6, 'Rain', 'Cool', 'Normal', 'Strong'],
[7, 'Overcast', 'Cool', 'Normal', 'Strong'],
[8, 'Sunny', 'Mild', 'High', 'Weak'],
[9, 'Sunny', 'Cool', 'Normal', 'Weak'],
[10, 'Rain', 'Mild', 'Normal', 'Weak'],
[11, 'Sunny', 'Mild', 'Normal', 'Strong'],
[12, 'Overcast', 'Mild', 'High', 'Strong'],
[13, 'Overcast', 'Hot', 'Normal', 'Weak'],
[14, 'Rain', 'Mild', 'High', 'Strong']
])

target = np.array(['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No'])

# Select the best attribute for the root node


best_attribute_index = select_root_attribute(data[:, 1:], target)

print("Best attribute for the root node:", data[0, best_attribute_index + 1])


OUTPUT:

RESULT:

Thus, the program was executed and the result was obtained successfully
EXP NO : 3 Pattern Recognition Application using
DATE: Bayesian Inference

AIM:

To implement a python program for pattern recognition application using Bayesian Interface.

PROGRAM CODE:

import numpy as np

class BayesianClassifier:
def __init__(self):
self.class_priors = {}
self.class_means = {}
self.class_variances = {}

def train(self, X, y):


# Calculate class priors
total_samples = len(y)
for label in set(y):
self.class_priors[label] = sum(y == label) / total_samples

# Calculate class means and variances


for label in set(y):
label_indices = np.where(y == label)[0]
class_samples = X[label_indices]
self.class_means[label] = np.mean(class_samples, axis=0)
self.class_variances[label] = np.var(class_samples, axis=0)

def predict(self, X):


predictions = []
for sample in X:
posterior_probs = {}
for label, prior in self.class_priors.items():
mean = self.class_means[label]
variance = self.class_variances[label]
likelihood = np.prod(self.gaussian_pdf(sample, mean, variance))
posterior_probs[label] = prior * likelihood
prediction = max(posterior_probs, key=posterior_probs.get)
predictions.append(prediction)
return predictions

def gaussian_pdf(self, x, mean, variance):


return np.exp(-((x - mean)**2) / (2 * variance)) / np.sqrt(2 * np.pi * variance)
# Example training data
X_train = np.array([
[1.2, 0.5],
[1.5, 0.7],
[2.0, 1.8],
[2.2, 2.5],
[3.5, 4.0],
[4.0, 3.5],
[3.8, 4.2],
[4.5, 3.8]
])
y_train = np.array(['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'])

# Example test data


X_test = np.array([
[1.8, 1.9],
[4.2, 3.6]
])

# Train the Bayesian classifier


classifier = BayesianClassifier()
classifier.train(X_train, y_train)

# Make predictions
predictions = classifier.predict(X_test)
for i, pred in enumerate(predictions):
print(f"Sample {i+1}: Predicted class - {pred}")
OUTPUT:

RESULT:

Thus, the program was executed and the result was obtained successfully
EXP NO :4 Naïve Bayes Classification
DATE:

AIM:

To implement a python program for Naïve Bayes Classification problem.

PROGRAM CODE:

import numpy as np

class NaiveBayesClassifier:
def __init__(self):
self.class_priors = {}
self.feature_likelihoods = {}

def train(self, X, y):


# Calculate class priors
total_samples = len(y)
for label in set(y):
self.class_priors[label] = sum(y == label) / total_samples

# Calculate feature likelihoods


num_features = X.shape[1]
for label in set(y):
label_indices = np.where(y == label)[0]
class_samples = X[label_indices]
self.feature_likelihoods[label] = [self.calculate_likelihood(class_samples[:, i]) for i in
range(num_features)]

def calculate_likelihood(self, feature):


unique_values, counts = np.unique(feature, return_counts=True)
likelihood = {}
total_samples = len(feature)
for value, count in zip(unique_values, counts):
likelihood[value] = count / total_samples
return likelihood

def predict(self, X):


predictions = []
for sample in X:
posterior_probs = {}
for label, prior in self.class_priors.items():
posterior = np.log(prior)
for i, value in enumerate(sample):
likelihood = self.feature_likelihoods[label][i].get(value, 1e-6) # Laplace smoothing
posterior += np.log(likelihood)
posterior_probs[label] = posterior
prediction = max(posterior_probs, key=posterior_probs.get)
predictions.append(prediction)
return predictions

# Example dataset
X_train = np.array([
['<30', 'High', 'Yes'],
['<30', 'High', 'No'],
['30-40', 'High', 'Yes'],
['>40', 'Medium', 'Yes'],
['>40', 'Low', 'No'],
['>40', 'Low', 'Yes'],
['30-40', 'Low', 'Yes'],
['<30', 'Medium', 'No'],
['<30', 'Low', 'Yes'],
['>40', 'Medium', 'Yes']
])
y_train = np.array(['Yes', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes'])

X_test = np.array([
['<30', 'Low', 'No'],
['>40', 'Medium', 'No']
])

# Train the Naive Bayes classifier


classifier = NaiveBayesClassifier()
classifier.train(X_train, y_train)

# Make predictions
predictions = classifier.predict(X_test)
for i, pred in enumerate(predictions):
print(f"Sample {i+1}: Predicted class - {pred}")
OUTPUT:
EXP NO: 5 Bagging in Classification
DATE:

AIM:

To implement a python program for bagging in classification.

PROGRAM CODE:

from sklearn.datasets import load_iris


from sklearn.model_selection import train_test_split
from sklearn.ensemble import BaggingClassifier
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.4, random_state=42)

# Initialize a decision tree classifier


base_classifier = DecisionTreeClassifier()

# Initialize a bagging classifier with 10 base classifiers


bagging_classifier = BaggingClassifier(base_classifier, n_estimators=9, random_state=42)

# Train the bagging classifier


bagging_classifier.fit(X_train, y_train)

# Make predictions
y_pred = bagging_classifier.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
OUTPUT:

RESULT:

Thus, the program was executed and the result was obtained successfully
EXP NO: 6 Bagging Boosting Applications using
DATE: Regression Trees

AIM:

To implement a python program for bagging boosting applications using regression trees.

PROGRAM CODE:

import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.ensemble import BaggingRegressor, GradientBoostingRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error

# Generate synthetic regression data


X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=42)

# Split the data 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)

# Bagging with Regression Trees


bagging_regressor = BaggingRegressor(estimator=DecisionTreeRegressor(),
n_estimators=10, random_state=42)
bagging_regressor.fit(X_train, y_train)
bagging_pred = bagging_regressor.predict(X_test)
bagging_mse = mean_squared_error(y_test, bagging_pred)
print("Bagging Mean Squared Error:", bagging_mse)

# Boosting with Regression Trees


boosting_regressor = GradientBoostingRegressor(n_estimators=10, random_state=42)
boosting_regressor.fit(X_train, y_train)
boosting_pred = boosting_regressor.predict(X_test)
boosting_mse = mean_squared_error(y_test, boosting_pred)
print("Boosting Mean Squared Error:", boosting_mse)
OUTPUT:

RESULT:

Thus, the program was executed and the result was obtained successfully
EXP NO: 7 Data and Text classification using Neural Network
DATE:

AIM:

To implement a python program that classifies data and text using neural network.

PROGRAM CODE:
# Import necessary libraries
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

iris = load_iris()
X = iris.data
y = iris.target

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 = scaler.transform(X_test)

model = MLPClassifier(hidden_layer_sizes=(50,), activation='relu', solver='adam', max_iter=50)

model.fit(X_train, y_train)

y_pred = model.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)


print('Accuracy:', accuracy)

precision = precision_score(y_test, y_pred, average='weighted')


print('Precision:', precision)
recall = recall_score(y_test, y_pred, average='weighted')
print('Recall:', recall)

f1 = f1_score(y_test, y_pred, average='weighted')


print('F1 Score:', f1)
OUTPUT:

RESULT:

Thus, the program was executed and the result was obtained successfully.
EXP NO: 8 Data & Text Clustering using
DATE: K-means algorithm

AIM:

To implement Data & Text Clustering using K means algorithm

PROGRAM CODE:
import os
# Set OMP_NUM_THREADS environment variable to 2
os.environ['OMP_NUM_THREADS'] = '2'
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

# Generate synthetic data


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

# Perform K-means clustering


kmeans = KMeans(n_clusters=4, n_init=10)
kmeans.fit(X)
y_kmeans = kmeans.predict(X)

# Plot the clusters


plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')

# Plot the centroids of the clusters


centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75)

plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('K-means Clustering')
plt.show()
OUTPUT:

RESULT:

Thus, the program was executed and the result was obtained successfully.
EXP NO: 9 Application of CRFs in Natural
DATE: Language Processing

AIM:

To implement a python program for application of CRF’s in natural Language Processing.

PROGRAM CODE:
import pandas as pd
# Example sentences and corresponding POS tags
sentences = [
"I love to play soccer",
"She sings beautifully",
"They are going to the park",
"I like to watch sunset"
]
pos_tags = [
["PRON", "VERB", "PART", "VERB", "NOUN"],
["PRON", "VERB", "ADV"],
["PRON", "AUX", "VERB", "ADP", "DET", "NOUN"],
["PRON", "VERB", "NOUN"]
]# Create a DataFrame to store the sentences and POS tags
data = pd.DataFrame({'sentence': sentences, 'pos_tags': pos_tags})
# Save the DataFrame as a CSV file
data.to_csv('pos_tagging_dataset.csv', index=False)
from google.colab import files
files.download('pos_tagging_dataset.csv')
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
# Load your own dataset (replace 'path_to_dataset' with the actual path)
with open('/content/pos_tagging_dataset.csv', 'r') as file:
data = file.read()
# Tokenize the text into sentences
sentences = nltk.sent_tokenize(data)
# Tokenize each sentence into words and apply POS tagging
pos_tags = []
for sentence in sentences:
words = nltk.word_tokenize(sentence)
pos_tags.append(nltk.pos_tag(words))
# Display the sentences
for i, sentence in enumerate(sentences):
print(f"--- Sentence {i+1} ---")
print(sentence)
print()
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
# Function to perform POS tagging on a sentence
def pos_tagging(sentence):
# Tokenize the sentence into words
words = nltk.word_tokenize(sentence)
# Apply POS tagging
pos_tags = nltk.pos_tag(words)
return pos_tags
# Get input sentence from user
sentence = input("Enter a sentence:")
# Perform POS tagging on the sentence
tagged_words = pos_tagging(sentence)
# Display the POS tags for each word
for word, pos_tag in tagged_words:
print(f"{word} -> {pos_tag}")
OUTPUT:

RESULT:

Thus, the program was executed and the result was obtained successfully.

You might also like