Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

lda from scratch implementation on iris python

import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris

class LDA:
    def __init__(self, n_components=None):
        self.n_components = n_components
        self.eig_vectors = None
    
    def transform(self,X,y):
        height, width = X.shape
        unique_classes = np.unique(y)
        num_classes = len(unique_classes)

        scatter_t = np.cov(X.T)*(height - 1)
        scatter_w = 0
        for i in range(num_classes):
            class_items = np.flatnonzero(y == unique_classes[i])
            scatter_w = scatter_w + np.cov(X[class_items].T) * (len(class_items)-1)
        
        scatter_b = scatter_t - scatter_w
        _, eig_vectors = np.linalg.eigh(np.linalg.pinv(scatter_w).dot(scatter_b))
        print(eig_vectors.shape)
        pc = X.dot(eig_vectors[:,::-1][:,:self.n_components])
        print(pc.shape)

        if self.n_components == 2:
            if y is None:
                plt.scatter(pc[:,0],pc[:,1])
            else:
                colors = ['r','g','b']
                labels = np.unique(y)
                for color, label in zip(colors, labels):
                    class_data = pc[np.flatnonzero(y==label)]
                    plt.scatter(class_data[:,0],class_data[:,1],c=color)
            plt.show()
        return pc

LDA_obj = LDA(n_components=2)
data = load_iris()
X, y = data.data, data.target
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.2)

LDA_object = LDA(n_components=2)
X_train_modified = LDA_object.transform(X_train, Y_train)

print("Original Data Size:",X_train.shape, "
Modified Data Size:", X_train_modified.shape)
Comment

PREVIOUS NEXT
Code Example
Python :: python raw strings 
Python :: gremlin python import 
Python :: Data Extraction in Python 
Python :: find smallest element not present in list python 
Python :: wpapi 
Python :: fetch inbox mail python 
Python :: how to make a half pyramid in python 
Python :: selsearch 
Python :: empty list 
Python :: object get in djangi 
Python :: Flask_SQLAlchemy is claiming my value is not boolean 
Python :: app.callback output is not defined 
Python :: Django, limit queryset without slicing 
Python :: tdlib python 
Python :: replace dataframe column element if element is within a specific list 
Python :: sns.kdeplot make line more detailed 
Python :: difflib get close matches 
Python :: nnumpy matrix count non negative values 
Python :: get length of list python 
Python :: circular ImportError: cannot import name 
Python :: any value in list will retrun true python 
Python :: global variable not accessible withing thread 
Python :: foreign key on delete arguments 
Python :: How printe word in python 
Python :: alterning format when reading from a text file 
Python :: modeltranslation 
Python :: pandas data frame from part of excel easy 
Python :: how to set time limit for receiving data in socket python 
Python :: device one list into 2 list python 
Python :: pandas crosstab function(counting) frequencies 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =