Created
December 5, 2015 15:59
-
-
Save ipeluffo/3caacbd854f6bc6837ee to your computer and use it in GitHub Desktop.
Exercise to visualize Iris dataset using Processing (https://www.processing.org/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int w = 1000; | |
int h = 600; | |
void setup() { | |
size(1000, 600); | |
background(0); | |
stroke(255); | |
float[] linesX = {}; | |
for (int i = 0; i < 4; i++) { | |
linesX = append(linesX, map(i, 0, 3, 0 + 10, w -10)); | |
line(linesX[i], 10, linesX[i], h-10); | |
} | |
// Load data | |
String url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"; | |
Table table = loadTable(url, "header,csv"); | |
// Calculate MAX and MIN for each column | |
float[][] columnsMinMax = new float[table.getColumnCount()-1][2]; | |
for (int i = 0; i < table.getColumnCount()-1; i++) { | |
float[] columnValues = {}; | |
for (int j = 0; j < table.getRowCount(); j++) { | |
columnValues = append(columnValues, table.getFloat(j, i)); | |
} | |
columnsMinMax[i][0] = min(columnValues); | |
columnsMinMax[i][1] = max(columnValues); | |
} | |
// Draw lines | |
for (int r = 0; r < table.getRowCount(); r++) { | |
TableRow row = table.getRow(r); | |
if (row.getString(0) == null || row.getString(0).length() == 0) { | |
continue; | |
} | |
setStrokeColor(row.getString(4)); | |
for (int i = 0; i < 3; i++) { | |
line( | |
linesX[i], | |
map(row.getFloat(i), columnsMinMax[i][0],columnsMinMax[i][1],h-10,10), | |
linesX[i+1], | |
map(row.getFloat(i+1),columnsMinMax[i+1][0],columnsMinMax[i+1][1],h-10,10) | |
); | |
} | |
} | |
} | |
void setStrokeColor(String clazz) { | |
println(clazz); | |
if (clazz.equals("Iris-setosa")) { | |
stroke(255,0,0); | |
} else if (clazz.equals("Iris-versicolor")) { | |
stroke(0,255,0); | |
} else if (clazz.equals("Iris-virginica")) { | |
stroke(0,0,255); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment