Classification using Fisher's Linear Discriminant (FLD)
Code: "classify_flq.py ". Programming language: Python
DMelt Version 2.2. Last modified: 03/11/1972. License: Pro
https://datamelt.org/code/cache/classify_flq_7029.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.
"""
Fisher's Linear Discriminant
Fisher's linear discriminant (FLD) is another popular linear classifier. Fisher defined the separation between two distributions to be the ratio of the variance between the classes to the variance within the classes, which is, in some sense, a measure of the signal-to-noise ratio for the class labeling. FLD finds a linear combination of features which maximizes the separation after the projection. The resulting combination may be used for dimensionality reduction before later classification.
"""
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 FLD
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 "RDA training for classification.."
alpha=1
data,label=getJavaArrays(dataset)
fisher = FLD(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=fisher.predict(data)
rows=dataset.size()
c1 = HPlot("Canvas",800,450,2,1)
c1.visible()
c1.setAutoRange()
c1.setGTitle("Fisher's Linear Disiminant (FLD)")
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.gray, 4)
p1p=P1D("Predicted true"); applyStyle(p1p,Color.blue, 11)
p2p=P1D("Predicted false"); applyStyle(p2p,Color.gray, 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.