Decision tree for classification of weather data from Weka
Code: " classify_dt.py". Programming language: Python
DMelt Version 2.2. Last modified: 03/11/2018. License: Pro
https://datamelt.org/code/cache/ classify_dt_4958.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.
"""
Decision tree for classification. A decision tree can be learned by
splitting the training set into subsets based on an attribute value
test. This process is repeated on each derived subset in a recursive
manner called recursive partitioning. The recursion is completed when
the subset at a node all has the same value of the target variable,
or when splitting no longer adds value to the predictions.
"""
from smile.data.parser import ArffParser,IOUtils
from smile.classification import DecisionTree
from smile.math import Math
from smile.validation import LOOCV
from jhplot import Web
from java.io import File
from jarray import zeros,array
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/weka/"
print "Reading data from",http
datasource="weather.nominal.arff"
datasetName="Weka"
print Web.get(http+datasource)
arffParser = ArffParser()
arffParser.setResponseIndex(4)
weather=arffParser.parse(File(datasource))
print weather
print "arff training for classification.."
x,y=getJavaArrays(weather)
n=len(y)
loocv = LOOCV(n)
error = 0
for i in range(n):
trainx = Math.slice(x, loocv.train[i]);
trainy = Math.slice(y, loocv.train[i]);
tree = DecisionTree(weather.attributes(),trainx,trainy,3);
org=y[loocv.test[i]]
pred=tree.predict(x[loocv.test[i]])
if (org != pred): error +=1;
print org, " predicted=",pred
print "Decision Tree error = ", error
You see the box below because you did not login.