0% found this document useful (0 votes)
12 views8 pages

ML&DAP Lab

The document contains various machine learning algorithms and their implementations, including the Find-S algorithm, Backpropagation for neural networks, ID3 decision tree, and K-Nearest Neighbors. Each section provides code examples using Python libraries like pandas and scikit-learn, along with sample datasets for classification tasks. The results demonstrate the effectiveness of these algorithms in predicting outcomes with high accuracy.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

ML&DAP Lab

The document contains various machine learning algorithms and their implementations, including the Find-S algorithm, Backpropagation for neural networks, ID3 decision tree, and K-Nearest Neighbors. Each section provides code examples using Python libraries like pandas and scikit-learn, along with sample datasets for classification tasks. The results demonstrate the effectiveness of these algorithms in predicting outcomes with high accuracy.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

111111111111111111111111111111111111111111111111111

=======================================================================

Sky,Temperature,Humidity,Wind,Water,Forecast,EnjoySport
Sunny,Warm,Normal,Strong,Warm,Same,Yes
Sunny,Warm,High,Strong,Warm,Same,Yes
Rainy,Cold,High,Strong,Warm,Change,No
Sunny,Warm,High,Strong,Cool,Change,Yes
Overcast,Hot,Normal,Weak,Cool,Same,Yes
Rainy,Cold,High,Weak,Cool,Change,No

import pandas as pd
import numpy as np

data=pd.read_csv("data.csv")

# Separate features and target


concepts = data.iloc[:, :-1].values
target = data.iloc[:, -1].values

# Initialize the hypothesis


hypothesis = ['0'] * len(concepts[0])

# Apply Find-S algorithm


for i, val in enumerate(target):
if val.lower() == 'yes':
for j in range(len(hypothesis)):
if hypothesis[j] == '0':
hypothesis[j] = concepts[i][j]
elif hypothesis[j] != concepts[i][j]:
hypothesis[j] = '?'

# Output the result


print("Final Hypothesis:", hypothesis)

=============================================================

output

Final Hypothesis: ['?', '?', '?', '?', '?', '?']

===================================================================================
================
6) Build an ANN by implementing Back propagation algorithem and test it with
suitable dataset
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size, learning_rate=0.01):
# Initialize parameters
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.learning_rate = learning_rate
# Initialize weights and biases
self.weights_input_hidden = np.random.randn(input_size, hidden_size)
self.weights_hidden_output = np.random.randn(hidden_size, output_size)
self.bias_hidden = np.zeros((1, hidden_size))
self.bias_output = np.zeros((1, output_size))
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def forward(self, X):
# Forward propagation
self.hidden_input = np.dot(X, self.weights_input_hidden) + self.bias_hidden
self.hidden_output = self.sigmoid(self.hidden_input)
self.output_input = np.dot(self.hidden_output, self.weights_hidden_output)
+ self.bias_output
self.output = self.sigmoid(self.output_input)
return self.output
def backward(self, X, y, output):
# Calculate the error
output_error = y - output
output_delta = output_error * self.sigmoid_derivative(output)
# Backpropagate the error
hidden_error = output_delta.dot(self.weights_hidden_output.T)
hidden_delta = hidden_error * self.sigmoid_derivative(self.hidden_output)
# Update weights and biases
self.weights_input_hidden += X.T.dot(hidden_delta) * self.learning_rate
self.weights_hidden_output += self.hidden_output.T.dot(output_delta) *
self.learning_rate
self.bias_hidden += np.sum(hidden_delta, axis=0, keepdims=True) *
self.learning_rate
self.bias_output += np.sum(output_delta, axis=0, keepdims=True) *
self.learning_rate

def train(self, X_train, y_train, epochs=10000):


# Training loop
for epoch in range(epochs):
output = self.forward(X_train)
self.backward(X_train, y_train, output)
# Print error every 1000 epochs for monitoring
if epoch % 1000 == 0:
loss = np.mean(np.square(y_train - output))
print(f"Epoch {epoch}/{epochs}, Loss: {loss:.4f}")
def predict(self, X):
# Make predictions on unseen data
output = self.forward(X)
return np.round(output) # Convert the output to 0 or 1
# Load Iris dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target
# For simplicity, we'll use only two classes (Setosa and Versicolor) for binary
classification
X = X[y != 2] # Only take Setosa and Versicolor
y = y[y != 2]
y = y.reshape(-1, 1)
# Normalize the features
scaler = StandardScaler()
X = scaler.fit_transform(X)
# Train-test split (80%-20%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Define the neural network parameters
input_size = X_train.shape[1] # Number of features (4 for Iris dataset)
hidden_size = 5 # Number of hidden neurons
output_size = 1 # Binary classification (0 or 1)
# Initialize the neural network
nn = NeuralNetwork(input_size, hidden_size, output_size)
# Train the neural network
nn.train(X_train, y_train, epochs=10000)
# Test the neural network
predictions = nn.predict(X_test)
accuracy = np.mean(predictions == y_test)
print(f"\nTest Accuracy: {accuracy * 100:.2f}%")

===================================================================================
=====
output
===================================================================================
=======
Epoch 0/10000, Loss: 0.4759
Epoch 1000/10000, Loss: 0.0011
Epoch 2000/10000, Loss: 0.0005
Epoch 3000/10000, Loss: 0.0003
Epoch 4000/10000, Loss: 0.0002
Epoch 5000/10000, Loss: 0.0002
Epoch 6000/10000, Loss: 0.0001
Epoch 7000/10000, Loss: 0.0001
Epoch 8000/10000, Loss: 0.0001
Epoch 9000/10000, Loss: 0.0001
Test Accuracy: 100.00%

===================================================================================
=====================================
222222222(((((()))
===================================================================================
=====================================
sky,air,temp,humidity,wind,water,forecast
sunny,warm,humidity,wind,water,forecast,yes
sunny,warm,normal,strong,warm,same,yes
sunny,warm,high,strong,warm,same,no
rainy,cold,high,strong,warm,change,yes

Code
import numpy as np
import pandas as pd

data = pd.read_csv('2.csv')
concepts = np.array(data.iloc[:,0:-1])
target = np.array(data.iloc[:,-1])
def learn(concepts, target):
specific_h = concepts[0].copy()
print("initialization of specific_h \n",specific_h)
general_h = [["?" for i in range(len(specific_h))] for i in
range(len(specific_h))]
print("initialization of general_h \n", general_h)

for i, h in enumerate(concepts):
if target[i] == "yes":
print("If instance is Positive ")
for x in range(len(specific_h)):
if h[x]!= specific_h[x]:
specific_h[x] ='?'
general_h[x][x] ='?'

if target[i] == "no":
print("If instance is Negative ")
for x in range(len(specific_h)):
if h[x]!= specific_h[x]:
general_h[x][x] = specific_h[x]
else:
general_h[x][x] = '?'

print(" step {}".format(i+1))


print(specific_h)
print(general_h)
print("\n")
print("\n")

indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?',


'?', '?']]
for i in indices:
general_h.remove(['?', '?', '?', '?', '?', '?'])
return specific_h, general_h

s_final, g_final = learn(concepts, target)

print("Final Specific_h:", s_final, sep="\n")


print("Final General_h:", g_final, sep="\n")
Output
initialization of specific_h
['sunny' 'warm' 'normal' 'strong' 'warm' 'same']
initialization of general_h
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?']]

If instance is Positive
step 1
['sunny' 'warm' 'normal' 'strong' 'warm' 'same']
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?']]

If instance is Positive
step 2
['sunny' 'warm' '?' 'strong' 'warm' 'same']
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?']]

If instance is Negative
step 3
['sunny' 'warm' '?' 'strong' 'warm' 'same']
[['sunny', '?', '?', '?', '?', '?'], ['?', 'warm', '?', '?', '?', '?'], ['?', '?',
'?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', 'same']]

If instance is Positive
step 4
['sunny' 'warm' '?' 'strong' '?' '?']
[['sunny', '?', '?', '?', '?', '?'], ['?', 'warm', '?', '?', '?', '?'], ['?', '?',
'?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?',
'?', '?', '?']]

Final Specific_h:
['sunny' 'warm' '?' 'strong' '?' '?']
Final General_h:
[['sunny', '?', '?', '?', '?', '?'], ['?', 'warm', '?', '?', '?', '?']]

===================(===============================================================
================================
3)Write a program to demonstrate the ID3 decision tree algorithem using
appropriate dataset for classification
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier, export_text
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split

# Sample dataset: Play Tennis


data = {
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast',
'Sunny', 'Sunny', 'Rain',
'Sunny', 'Overcast', 'Overcast', 'Rain'],
'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild',
'Cool', 'Mild',
'Mild', 'Mild', 'Hot', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal',
'High', 'Normal', 'Normal',
'Normal', 'High', 'Normal', 'High'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak',
'Weak', 'Weak',
'Strong', 'Strong', 'Weak', 'Strong'],
'PlayTennis': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes',
'Yes',
'Yes', 'Yes', 'Yes', 'No']
}
# Convert to DataFrame
df = pd.DataFrame(data)
# Encode categorical data into numerical values
label_encoders = {}
for column in df.columns:
le = LabelEncoder()
df[column] = le.fit_transform(df[column])
label_encoders[column] = le # Store label encoders for decoding later if
needed
# Split features and target
X = df.drop(columns=['PlayTennis']) # Features
y = df['PlayTennis'] # Target
# Split into training and test data (80%-20%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Create and train the ID3 Decision Tree Classifier
tree = DecisionTreeClassifier(criterion='entropy') # ID3 uses entropy
tree.fit(X_train, y_train)
# Display the decision tree structure
tree_rules = export_text(tree, feature_names=list(X.columns))
print("\nDecision Tree Rules:\n")
print(tree_rules)
# Evaluate the model
accuracy = tree.score(X_test, y_test)
print(f"\nModel Accuracy: {accuracy * 100:.2f}%")
output
Decision Tree Rules:

|--- Outlook <= 0.50


| |--- class: 1
|--- Outlook > 0.50
| |--- Humidity <= 0.50
| | |--- Outlook <= 1.50
| | | |--- Wind <= 0.50
| | | | |--- class: 0
| | | |--- Wind > 0.50
| | | | |--- class: 1
| | |--- Outlook > 1.50
| | | |--- class: 0
| |--- Humidity > 0.50
| | |--- Wind <= 0.50
| | | |--- Outlook <= 1.50
| | | | |--- class: 0
| | | |--- Outlook > 1.50
| | | | |--- class: 1
| | |--- Wind > 0.50
| | | |--- class: 1
Model Accuracy: 100.00

==================================

5) Implement K-Nearest neighbor algorithemto classify the Iris dataset printing


both Correct and Incorrect Predictions
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
class KNNClassifier:
def __init__(self, k=3):
self.k = k
def fit(self, X_train, y_train):
self.X_train = X_train
self.y_train = y_train
def predict(self, X_test):
y_pred = []
for test_point in X_test:
# Calculate distances from the test point to all points in the training
set
distances = np.linalg.norm(self.X_train - test_point, axis=1)
# Get indices of the k nearest neighbors
neighbor_indices = np.argsort(distances)[:self.k]
# Get the target values of the k nearest neighbors
neighbor_values = self.y_train[neighbor_indices]
# Predict by majority voting
prediction = np.bincount(neighbor_values).argmax()
y_pred.append(prediction)
return np.array(y_pred)
# Load Iris dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Split into training and testing sets (80% train, 20% test)
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


knn = KNNClassifier(k=3)
# Fit the model on the training data
knn.fit(X_train, y_train)
# Make predictions on the test set
y_pred = knn.predict(X_test)
# Print correct and incorrect predictions
correct_count = 0
incorrect_count = 0
for i in range(len(y_test)):
if y_pred[i] == y_test[i]:
correct_count += 1
print(f"Correct Prediction: Test Point {X_test[i]} predicted as
{iris.target_names[y_pred[i]]}")
else:
incorrect_count += 1
print(f"Incorrect Prediction: Test Point {X_test[i]} predicted as
{iris.target_names[y_pred[i]]} (True label: {iris.target_names[y_test[i]]})")
# Calculate the accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"\nTotal Correct Predictions: {correct_count}")
print(f"Total Incorrect Predictions: {incorrect_count}")
print(f"Accuracy: {accuracy * 100:.2f}%")

===================================================================================
===================
output
===================================================================================
========================
Correct Prediction: Test Point [6.1 2.8 4.7 1.2] predicted as versicolor
Correct Prediction: Test Point [5.7 3.8 1.7 0.3] predicted as setosa
Correct Prediction: Test Point [7.7 2.6 6.9 2.3] predicted as virginica
Correct Prediction: Test Point [6. 2.9 4.5 1.5] predicted as versicolor
Correct Prediction: Test Point [6.8 2.8 4.8 1.4] predicted as versicolor
Correct Prediction: Test Point [5.4 3.4 1.5 0.4] predicted as setosa
Correct Prediction: Test Point [5.6 2.9 3.6 1.3] predicted as versicolor
Correct Prediction: Test Point [6.9 3.1 5.1 2.3] predicted as virginica
Correct Prediction: Test Point [6.2 2.2 4.5 1.5] predicted as versicolor
Correct Prediction: Test Point [5.8 2.7 3.9 1.2] predicted as versicolor
Correct Prediction: Test Point [6.5 3.2 5.1 2. ] predicted as virginica
Correct Prediction: Test Point [4.8 3. 1.4 0.1] predicted as setosa
Correct Prediction: Test Point [5.5 3.5 1.3 0.2] predicted as setosa
Correct Prediction: Test Point [4.9 3.1 1.5 0.1] predicted as setosa
Correct Prediction: Test Point [5.1 3.8 1.5 0.3] predicted as setosa
Correct Prediction: Test Point [6.3 3.3 4.7 1.6] predicted as versicolor
Correct Prediction: Test Point [6.5 3. 5.8 2.2] predicted as virginica
Correct Prediction: Test Point [5.6 2.5 3.9 1.1] predicted as versicolor
Correct Prediction: Test Point [5.7 2.8 4.5 1.3] predicted as versicolor
Correct Prediction: Test Point [6.4 2.8 5.6 2.2] predicted as virginica
Correct Prediction: Test Point [4.7 3.2 1.6 0.2] predicted as setosa
Correct Prediction: Test Point [6.1 3. 4.9 1.8] predicted as virginica
Correct Prediction: Test Point [5. 3.4 1.6 0.4] predicted as setosa
Correct Prediction: Test Point [6.4 2.8 5.6 2.1] predicted as virginica
Correct Prediction: Test Point [7.9 3.8 6.4 2. ] predicted as virginica
Correct Prediction: Test Point [6.7 3. 5.2 2.3] predicted as virginica
Correct Prediction: Test Point [6.7 2.5 5.8 1.8] predicted as virginica
Correct Prediction: Test Point [6.8 3.2 5.9 2.3] predicted as virginica
Correct Prediction: Test Point [4.8 3. 1.4 0.3] predicted as setosa
Correct Prediction: Test Point [4.8 3.1 1.6 0.2] predicted as setosa
Total Correct Predictions: 30
Total Incorrect Predictions: 0
Accuracy: 100.00%

==================================(=====(===

You might also like