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

Experiment 6-7-8

The document outlines three experiments using different machine learning models: Random Forest for classification on the Iris dataset, Linear Regression for salary prediction, and Logistic Regression with confusion matrix visualization. Each experiment includes data loading, model training, prediction, and visualization of results. The Random Forest experiment specifically analyzes the effect of varying tree counts on accuracy.
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)
1 views

Experiment 6-7-8

The document outlines three experiments using different machine learning models: Random Forest for classification on the Iris dataset, Linear Regression for salary prediction, and Logistic Regression with confusion matrix visualization. Each experiment includes data loading, model training, prediction, and visualization of results. The Random Forest experiment specifically analyzes the effect of varying tree counts on accuracy.
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/ 5

Experiment 6

Consider a dataset use Random forest to predict the output class

from sklearn.ensemble import RandomForestClassifier


from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

# Load the iris dataset


data = load_iris()
X = data.data # Feature data
y = data.target # Target labels

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

# Different tree counts to compare


tree_counts = [20, 50, 100, 200, 500]
accuracies = []

# Train Random Forest models with varying tree counts and record accuracy
for n_trees in tree_counts:
model = RandomForestClassifier(n_estimators=n_trees, random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
accuracies.append(accuracy)

# Plot the results


plt.figure(figsize=(8, 6))
plt.plot(tree_counts, accuracies, marker='o', linestyle='-', color='b')
plt.title('Accuracy vs. Number of Trees in Random Forest')
plt.xlabel('Number of Trees')
plt.ylabel('Accuracy')
plt.grid(True)
plt.xticks(tree_counts)
plt.show()

Output :
Experiment 7 : Linear Regression
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression

dataset = pd.read_csv('Salary.csv')
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values

# x = x.reshape(-1,1)
liner = LinearRegression()
liner.fit(x,y)
y_pred = liner.predict(x)

plt.scatter(x,y)
plt.plot(x,y_pred,color='red')
plt.show()

Output:
Experiment 8 : Logistic Regression

from sklearn.datasets import make_classification


import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import pandas as pd

# Load the dataset (Iris CSV assumed to be in the working directory)


dataset = pd.read_csv('iris.csv')

# Feature and label selection


X = dataset.iloc[:, [0, 1, 2, 3]].values # Features
y = dataset.iloc[:, 4].values # Target

# Split into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# Create and train the Logistic Regression model


log_reg = LogisticRegression()
log_reg.fit(X_train, y_train)

# Make predictions
y_pred = log_reg.predict(X_test)

# Compute the confusion matrix


cm = confusion_matrix(y_test, y_pred)
print(cm)
# Plot confusion matrix using seaborn heatmap
import seaborn as sns

df_cm = pd.DataFrame(cm)
plt.figure(figsize=(5, 3))
sns.heatmap(df_cm, annot=True, annot_kws={"size": 30}, fmt='d', cmap='Blues')
plt.title('Confusion Matrix')
plt.show()

Output:

You might also like