Classification using Linear discriminant analysis (LDA)
Code: "classify_lda.py". Programming language: Python DMelt Version 1.4. Last modified: 03/11/1972. License: Pro
https://datamelt.org/code/cache/classify_lda_4706.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


"""
Linear Discriminant Analysis

Linear discriminant analysis (LDA) is based on the Bayes decision theory and assumes that the conditional probability density functions are normally distributed. 
LDA is closely related to ANOVA (analysis of variance) and linear regression analysis, which also attempt to express one dependent variable as a linear combination of other features or measurements. 
"""


from smile.data import AttributeDataset,NominalAttribute
from smile.data.parser import DelimitedTextParser,IOUtils
from java.io import File
from jhplot import *
from smile.classification import LDA 
from jarray import zeros,array
from java.awt import Color
import java


# this function extract data[][] and label[] array from datasets
def getJavaArrays(dataset):
    rows=dataset.size()
    lst = [0.0]*rows
    twoDimArr = array([lst,[]], java.lang.Class.forName('[D'))
    data = dataset.toArray(twoDimArr)
    label = dataset.toArray(zeros(rows, "i"))
    return data,label

http="http://datamelt.org/examples/data/classification/toy/"
print "Reading data from",http
datasource="toy-train.txt"  
datasetName="Toy"
print Web.get(http+datasource)
parser =DelimitedTextParser()
parser.setDelimiter("[\t ]+")
parser.setResponseIndex(NominalAttribute("class"), 0)
dataset=parser.parse(datasetName,File(datasource))
print dataset
print "Classification.."

data,label=getJavaArrays(dataset)
lda=LDA(data, label)

# now we will use another sample for prediction
print "Training finished. Get a new sample for predictions.."
datasource="toy-test.txt" #  (20k events) 
datasetName="Testing"
print Web.get(http+datasource)
dataset=parser.parse(datasetName,File(datasource))
data,label=getJavaArrays(dataset)

#print label
#print type(data)
prediction=lda.predict(data)
rows=dataset.size()


c1 = HPlot("Canvas",800,450,2,1)
c1.visible()
c1.setAutoRange()
c1.setGTitle("Linear Discriminant Analysis (LDA)")

def applyStyle(p1,color,t): 
      p1.setSymbol(t); p1.setColor(color); p1.setSymbolSize(1); 
 
p1=P1D("Input true"); applyStyle(p1,Color.blue, 11);
p2=P1D("Input false"); applyStyle(p2,Color.green, 4); 
p1p=P1D("Predicted true"); applyStyle(p1p,Color.blue, 11);
p2p=P1D("Predicted false"); applyStyle(p2p,Color.green, 4);   

e=0.0
for i in range(rows):
    minput=data[i].tolist()
    expected=label[i]
    predicted=prediction[i] 
    if (expected>0): p1.add(minput[0],minput[1]) 
    else:            p2.add(minput[0],minput[1])
    if (predicted>0): p1p.add(minput[0],minput[1])
    else:             p2p.add(minput[0],minput[1])
    if (i%100==0): print "Input=",minput, " outcome=",expected," prediction=",predicted
    if (expected != predicted): e=e+1.0
e=e/rows
print "Error rate=",e

c1.cd(1,1)
c1.draw([p1,p2])
c1.cd(2,1)
c1.setAutoRange()
c1.draw([p1p,p2p])


You see the box below because you did not login.