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

AIML Lab 7 8 9 10

The document contains multiple Python programs demonstrating various machine learning techniques, including Hierarchical Clustering, Support Vector Machine (SVM), Random Forest classification, and ARIMA for time series forecasting. Each program loads the Iris dataset or another dataset, preprocesses the data, applies the respective algorithm, visualizes the results, and evaluates the model's performance. The document provides code snippets and explanations for each step in the implementation process.
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)
4 views10 pages

AIML Lab 7 8 9 10

The document contains multiple Python programs demonstrating various machine learning techniques, including Hierarchical Clustering, Support Vector Machine (SVM), Random Forest classification, and ARIMA for time series forecasting. Each program loads the Iris dataset or another dataset, preprocesses the data, applies the respective algorithm, visualizes the results, and evaluates the model's performance. The document provides code snippets and explanations for each step in the implementation process.
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/ 10

7.

Write a program to implement Hierarchical clustering

import numpy as np

import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

from sklearn.cluster import AgglomerativeClustering

from sklearn.preprocessing import StandardScaler

from scipy.cluster.hierarchy import dendrogram, linkage

# Step 1: Load the Iris dataset

data = load_iris()

X = data.data # Features

# Step 2: Standardize the features

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

# Step 3: Apply Agglomerative Hierarchical Clustering

agg_clust = AgglomerativeClustering(n_clusters=3, affinity='euclidean', linkage='ward')

labels = agg_clust.fit_predict(X_scaled)

# Step 4: Visualize the results using a dendrogram

linked = linkage(X_scaled, method='ward')

plt.figure(figsize=(10, 7))

dendrogram(linked, labels=data.target_names[labels])

plt.title('Hierarchical Clustering Dendrogram (Iris Dataset)')


plt.xlabel('Sample index')

plt.ylabel('Distance')

plt.show()

# Step 5: Output the cluster labels

print("Cluster Labels: ", labels)

Output :
8. Write a program to implement Support Vector Machine algorithm
import numpy as np

import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.svm import SVC

from sklearn.metrics import accuracy_score, classification_report

# Step 1: Load the Iris dataset

data = load_iris()

X = data.data # Features

y = data.target # Target (class labels)

# Step 2: 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)


# Step 3: Create and train the SVM model

model = SVC(kernel='linear') # Using a linear kernel

model.fit(X_train, y_train)

# Step 4: Make predictions on the test set

y_pred = model.predict(X_test)

# Step 5: Evaluate the model

accuracy = accuracy_score(y_test, y_pred)

class_report = classification_report(y_test, y_pred, target_names=data.target_names)

# Step 6: Print the evaluation results

print("Accuracy: ", accuracy)

print("\nClassification Report:\n", class_report)

# Step 7: Visualize the decision boundary (Only using the first two features for simplicity)

plt.figure(figsize=(8, 6))

# Plot the data points

plt.scatter(X_test[:, 0], X_test[:, 1], c=y_pred, cmap='coolwarm', edgecolors='k', marker='o', s=100,


alpha=0.7)

# Plot decision boundary

h = 0.02 # Step size in the mesh

x_min, x_max = X_test[:, 0].min() - 1, X_test[:, 0].max() + 1

y_min, y_max = X_test[:, 1].min() - 1, X_test[:, 1].max() + 1


xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

Z = model.predict(np.c_[xx.ravel(), yy.ravel()])

Z = Z.reshape(xx.shape)

plt.contourf(xx, yy, Z, alpha=0.3)

plt.xlabel("Feature 1")

plt.ylabel("Feature 2")

plt.title("SVM Classifier (Iris Dataset)")

plt.colorbar()

plt.show()

Output:
9. Write a program to implement clustering using random forest algorithm using
an appropriate dataset.
import numpy as np

import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score

# Step 1: Load the Iris dataset

data = load_iris()

X = data.data # Features

y = data.target # Target (class labels)

# Step 2: 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)

# Step 3: Create and train the Random Forest classifier

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

rf_model.fit(X_train, y_train)
# Step 4: Make predictions on the test set

y_pred = rf_model.predict(X_test)

# Step 5: Evaluate the model

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy: ", accuracy)

# Step 6: Visualize the clusters (use the first two features for simplicity)

plt.figure(figsize=(8, 6))

plt.scatter(X_test[:, 0], X_test[:, 1], c=y_pred, cmap='viridis', marker='o', edgecolor='k', s=100,


alpha=0.6)

plt.title('Random Forest Classifier "Clustering" (Iris Dataset)')

plt.xlabel('Feature 1')

plt.ylabel('Feature 2')

plt.colorbar(label='Predicted Cluster')

plt.show()

# Step 7: Output the predicted labels (cluster-like grouping)

print("Predicted Cluster Labels: ", y_pred)

OUTPUT:

10. Write a program to implement Forecasting using ARIMA Model


import numpy as np
import pandas as pd

import matplotlib.pyplot as plt

from statsmodels.tsa.arima.model import ARIMA

from sklearn.model_selection import train_test_split

from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

# Step 1: Load the dataset (Airline Passengers dataset)

url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv'

data = pd.read_csv(url, header=0, parse_dates=[0], index_col=0, squeeze=True)

# Step 2: Visualize the dataset

plt.figure(figsize=(10,6))

plt.plot(data)

plt.title('Monthly Number of Airline Passengers')

plt.xlabel('Date')

plt.ylabel('Number of Passengers')

plt.show()

# Step 3: Split the data into training and testing sets

train_size = int(len(data) * 0.8)

train, test = data[0:train_size], data[train_size:]

# Step 4: Visualize ACF and PACF plots for choosing p, d, q values

plot_acf(train, lags=40)

plot_pacf(train, lags=40)

plt.show()
# Step 5: Fit the ARIMA model

# ARIMA(p, d, q) where p is the number of lag observations included in the model (AR), d is the number
of times the raw observations are differenced,

# and q is the size of the moving average window.

model = ARIMA(train, order=(5, 1, 0)) # (p=5, d=1, q=0) - Adjust these parameters based on your ACF
and PACF plots

model_fit = model.fit()

# Step 6: Make predictions on the test set

forecast = model_fit.forecast(steps=len(test))

# Step 7: Visualize the actual vs predicted values

plt.figure(figsize=(10,6))

plt.plot(train, label='Train')

plt.plot(test, label='Test')

plt.plot(test.index, forecast, label='Forecast', color='red')

plt.title('ARIMA Model Forecasting')

plt.xlabel('Date')

plt.ylabel('Number of Passengers')

plt.legend()

plt.show()

# Step 8: Evaluate the model

# Calculate Mean Squared Error (MSE) or other evaluation metrics

from sklearn.metrics import mean_squared_error

mse = mean_squared_error(test, forecast)

print(f'Mean Squared Error: {mse}')


OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy