KEMBAR78
Example - LinearDiscriminantAnalysis - Ipynb Colaboratory | PDF
0% found this document useful (0 votes)
70 views2 pages

Example - LinearDiscriminantAnalysis - Ipynb Colaboratory

The document loads the Iris dataset and splits it into training and test sets. It applies linear discriminant analysis (LDA) to reduce the dimensionality of the features. A random forest classifier is trained on the LDA-transformed training set and predicts the classes of the test set, achieving 100% accuracy based on the confusion matrix and accuracy score.

Uploaded by

Manan Aggarawal
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)
70 views2 pages

Example - LinearDiscriminantAnalysis - Ipynb Colaboratory

The document loads the Iris dataset and splits it into training and test sets. It applies linear discriminant analysis (LDA) to reduce the dimensionality of the features. A random forest classifier is trained on the LDA-transformed training set and predicts the classes of the test set, achieving 100% accuracy based on the confusion matrix and accuracy score.

Uploaded by

Manan Aggarawal
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/ 2

Example_ Linear-Discriminant-Analysis.ipynb - Colaboratory https://colab.research.google.com/drive/19XFvpk9LV53c08HhACS1l9...

import numpy as np
import pandas as pd

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']
dataset = pd.read_csv(url, names=names)

X = dataset.iloc[:, 0:4].values
y = dataset.iloc[:, 4].values

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA

lda = LDA(n_components=1)
X_train = lda.fit_transform(X_train, y_train)
X_test = lda.transform(X_test)

from sklearn.ensemble import RandomForestClassifier

classifier = RandomForestClassifier(max_depth=2, random_state=0)

classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)

from sklearn.metrics import confusion_matrix


from sklearn.metrics import accuracy_score

cm = confusion_matrix(y_test, y_pred)
print(cm)

[[11 0 0]
[ 0 13 0]
[ 0 0 6]]

1 of 2 9/17/2022, 1:12 PM
Example_ Linear-Discriminant-Analysis.ipynb - Colaboratory https://colab.research.google.com/drive/19XFvpk9LV53c08HhACS1l9...

print('Accuracy=' + str(accuracy_score(y_test, y_pred)))

Accuracy=1.0

Colab paid products - Cancel contracts here

2 of 2 9/17/2022, 1:12 PM

You might also like