 |
Weka classification using linear regressions and graph
Source code name: "weka_linear_regression.py"
Programming language: Python
Topic: Data mining/Classification
DMelt Version 2.29. Last modified: 03/03/2021. License: Pro
https://datamelt.org/code/cache/weka_linear_regression_2978.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.
from jhplot import Web
xf="bodyfat.arff"
url="https://datamelt.org/examples/data/weka/"+xf
print "Loading ",xf
print Web.get(url)
data = Instances( FileReader(xf) )
data.setClassIndex(data.numAttributes() - 1)
# configure classifier
cls = LinearRegression()
cls.setOptions(["-C", "-S", "1"])
# cross-validate classifier
evl = Evaluation(data)
evl.crossValidateModel(cls, data, 10, Random(1))
# collect predictions
act = []
prd = []
err = []
for i in range(evl.predictions().size()):
prediction = evl.predictions().get(i)
act.append(prediction.actual())
prd.append(prediction.predicted())
err.append(abs(prediction.actual() - prediction.predicted()))
# create plot
plotdata = DefaultXYZDataset()
plotdata.addSeries("LR on " + data.relationName(), [act, prd, err])
plot = ChartFactory.createScatterPlot(\
"Classifier errors", "Actual", "Predicted", \
plotdata, PlotOrientation.VERTICAL, True, True, True)
plot.getPlot().setRenderer(XYBubbleRenderer())
# display plot
frame = JFrame()
frame.setTitle("Weka")
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
frame.setSize(800, 800)
frame.setLocationRelativeTo(None)
frame.getContentPane().add(ChartPanel(plot))
frame.setVisible(True)
You see the box below because you did not login.