Classification using Multilayer Perceptron Neural Network
Code: "classify_neuralnet.py". Programming language: Python DMelt Version 2.2. Last modified: 03/11/1972. License: Pro
https://datamelt.org/code/cache/classify_neuralnet_8989.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.


"""

Multilayer Perceptron Neural Network

A multilayer perceptron neural network consists of several layers of nodes, interconnected through weighted acyclic arcs from each preceding layer to the following, without lateral or feedback connections. Each node calculates a transformed weighted linear combination of its inputs (output activations from the preceding layer), with one of the weights acting as a trainable bias connected to a constant input.

"""


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 NeuralNetwork 
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)


units = 10 # hiddent layers 
# input, hiden and output layers
arr = array([len(data[0]),10,1],"i")
net = NeuralNetwork(NeuralNetwork.ErrorFunction.CROSS_ENTROPY, NeuralNetwork.ActivationFunction.LOGISTIC_SIGMOID, arr);

epochs =1000
for i in range(epochs):
      if (i%20==0): print i, net.learn(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=net.predict(data)
rows=dataset.size()

c1 = HPlot("Canvas",800,450,2,1)
c1.visible()
c1.setAutoRange()
c1.setGTitle("Multilayer Perceptron Neural Network")

def applyStyle(p1,color,t): 
      p1.setSymbol(t); p1.setColor(color); p1.setSymbolSize(1); 
 
p1=P1D("Input true"); applyStyle(p1,Color.cyan, 1);
p2=P1D("Input false"); applyStyle(p2,Color.red, 4); 
p1p=P1D("Predicted true"); applyStyle(p1p,Color.cyan, 1);
p2p=P1D("Predicted false"); applyStyle(p2p,Color.red, 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.