[go: up one dir, main page]

0% found this document useful (0 votes)
20 views6 pages

Aiml Lab

The document provides code examples for generating association rules from weather data using Python's mlxtend library, achieving frequent itemsets and association rules. It also explores various machine learning models, including Random Forest, K-Nearest Neighbors, Support Vector Machine, and Logistic Regression, using the Iris dataset, all achieving 100% accuracy. Additionally, it demonstrates building a Neural Network Classifier with MLPClassifier, calculating accuracy, and plotting the loss curve.

Uploaded by

G Ravi Kumar
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)
20 views6 pages

Aiml Lab

The document provides code examples for generating association rules from weather data using Python's mlxtend library, achieving frequent itemsets and association rules. It also explores various machine learning models, including Random Forest, K-Nearest Neighbors, Support Vector Machine, and Logistic Regression, using the Iris dataset, all achieving 100% accuracy. Additionally, it demonstrates building a Neural Network Classifier with MLPClassifier, calculating accuracy, and plotting the loss curve.

Uploaded by

G Ravi Kumar
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/ 6

3.

Generating association rules on Weather data using Weka or Python


import pandas as pd

from mlxtend.frequent_patterns import apriori, association_ rules

# Sample weather data

data = {

'Sunny': [1, 0, 1, 0],

'Rainy': [0, 1, 0, 1],

'Snowy': [0, 0, 1, 0],

'Hot': [1, 0, 0, 0],

'Cold': [0, 1, 1, 1]

df = pd.DataFrame(data)

frequent_itemsets = apriori(df, min_support=0.5, use_colnames=True)

rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.0)

print("Frequent Itemsets:")

print(frequent_itemsets)

print("\nAssociation Rules:")

print(rules)

output:

support itemsets

0 0.75 (Sunny)

1 0.50 (Cold)

2 0.75 (Hot)

3 0.50 (Rainy)

4 0.50 (Snowy)

5 0.50 (Sunny, Hot)

6 0.50 (Rainy, Cold)


antecedents consequents support confidence lift

0 (Sunny) (Hot) 0.50 0.6667 1.3333

1 (Rainy) (Cold) 0.50 0.6667 1.3333

4.Exploring machine learning models including classification and clustering


using scikitlearn or Weka or Python

Program:

import pandas as pd

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score

from sklearn.neighbors import KNeighborsClassifier

from sklearn.svm import SVC

from sklearn.linear_model import LogisticRegression

# Load the Iris dataset

iris = load_iris()

X = iris.data # Features

y = iris.target # Target variable

# Split data into training and testing sets


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

# Random Forest Classifier

clf = RandomForestClassifier(n_estimators=100, random_state=42)

clf.fit(X_train, y_train)

y_pred_rf = clf.predict(X_test)

accuracy_rf = accuracy_score(y_test, y_pred_rf)

# K-Nearest Neighbors

knn = KNeighborsClassifier(n_neighbors=3)

knn.fit(X_train, y_train)

accuracy_knn = accuracy_score(y_test, knn.predict(X_test))

# Support Vector Machine

svm = SVC(random_state=42)

svm.fit(X_train, y_train)

accuracy_svm = accuracy_score(y_test, svm.predict(X_test))

# Logistic Regression

lr = LogisticRegression(max_iter=200)

lr.fit(X_train, y_train)

accuracy_lr = accuracy_score(y_test, lr.predict(X_test))

print(f"Accuracy of Random Forest Classifier: {accuracy_rf:.2f}")


print(f"Accuracy of KNN Classifier: {accuracy_knn:.2f}")

print(f"Accuracy of SVM Classifier: {accuracy_svm:.2f}")

print(f"Accuracy of Logistic Regression: {accuracy_lr:.2f}")

output:
Accuracy of Random Forest Classifier: 1.00
Accuracy of KNN Classifier: 1.00
Accuracy of SVM Classifier: 1.00
Accuracy of Logistic Regression: 1.00

5.Build Neural Network Classifier using Weka or Python

Program:

import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.datasets import load_iris

from sklearn.neural_network import MLPClassifier

from sklearn.metrics import accuracy_score, classification_report

import matplotlib.pyplot as plt

# Load the Iris dataset

iris = load_iris()

X = iris.data # Features

y = iris.target # Target variable (class labels)

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,


random_state=42)
# Initialize and train the neural network classifier (MLPClassifier)

mlp = MLPClassifier(hidden_layer_sizes=(10, 10), max_iter=1000,


random_state=42)

mlp.fit(X_train, y_train)

# Predict on the test set

y_pred = mlp.predict(X_test)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy of Neural Network Classifier: {accuracy * 100:.2f}%")

# Print classification report

print("\nClassification Report:")

print(classification_report(y_test, y_pred))

# Plot loss curve

plt.plot(mlp.loss_curve_)

plt.title("Neural Network Loss Curve")

plt.xlabel("Iterations")

plt.ylabel("Loss")

plt.show()
output:

You might also like