0% found this document useful (0 votes)
2 views3 pages

pr2

This document is a lab manual for a Machine Learning course, detailing the implementation of Random Forest and ensemble learning techniques using Python. It includes code snippets for loading the Iris dataset, preparing the data, splitting it into training and test sets, and training a Random Forest classifier. The output section mentions accuracy and confusion matrix metrics for evaluating the model's performance.
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)
2 views3 pages

pr2

This document is a lab manual for a Machine Learning course, detailing the implementation of Random Forest and ensemble learning techniques using Python. It includes code snippets for loading the Iris dataset, preparing the data, splitting it into training and test sets, and training a Random Forest classifier. The output section mentions accuracy and confusion matrix metrics for evaluating the model's performance.
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/ 3

FACULTY OF TECHNOLOGY

Department of Computer Engineering


Machine Learning Lab Manual

Practical 2: Implement Random Forest and ensemble learning techniques.

Code:

from sklearn import datasets

iris = datasets.load_iris()

print(iris.target_names)

print(iris.feature_names)

print(iris.data[0:5])

print(iris.target)

import pandas as pd

data = pd.DataFrame({

'sepal length': iris.data[:, 0],

'sepal width': iris.data[:, 1],

'petal length': iris.data[:, 2],

'petal width': iris.data[:, 3],

'species': iris.target

})

data.head()

from sklearn.model_selection import train_test_split

X = data[['sepal length', 'sepal width',

'petal length', 'petal width']] # Features

y = data['species'] # Labels

# Split dataset into training set and test set


<92200103148 > |1
FACULTY OF TECHNOLOGY
Department of Computer Engineering
Machine Learning Lab Manual

X_train, X_test, y_train, y_test = train_test_split(

X, y, test_size=0.3, random_state=42) # 70% training and 30% test

# Import Random Forest Model

from sklearn.ensemble import RandomForestClassifier

# Create a Gaussian Classifier

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

clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)

from sklearn import metrics

print("Accuracy : ", metrics.accuracy_score(y_test, y_pred))

cm = metrics.confusion_matrix(y_test, y_pred)

print(cm)

<92200103148 > |1
FACULTY OF TECHNOLOGY
Department of Computer Engineering
Machine Learning Lab Manual

Output:

<92200103148 > |1

You might also like