Classification using Quadratic Discriminant analysis (QDA)
Code: "classify_qda.py ". Programming language: Python
DMelt Version 2.2. Last modified: 03/11/1972. License: Pro
https://datamelt.org/code/cache/classify_qda_8653.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.
"""
Quadratic discriminant analysis (QDA) is closely related to LDA. Like LDA, QDA models the conditional probability density functions as a Gaussian distribution, then uses the posterior distributions to estimate the class for a given test data.
Unlike LDA, however, in QDA there is no assumption that the covariance of each of the classes is identical. Therefore, the resulting separating surface between the classes is quadratic.
"""
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 QDA
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)
rda = QDA(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=rda.predict(data)
rows=dataset.size()
c1 = HPlot("Canvas",800,450,2,1)
c1.visible()
c1.setAutoRange()
c1.setGTitle("Quadratic Discriminant analysis (QDA)")
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.