Skip to content

Commit

Permalink
CSV input
Browse files Browse the repository at this point in the history
  • Loading branch information
Serhii Lytvyn authored and Serhii Lytvyn committed Dec 14, 2024
1 parent a532511 commit 0f4abbd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
6 changes: 6 additions & 0 deletions dataset.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
input1,input2,input3,input4,input5,output1,output2,output3
0.1,0.2,0.3,0.4,0.5,0.14,0.26,0.35
0.5,0.4,0.3,0.2,0.1,0.33,0.29,0.07
0.9,0.8,0.7,0.6,0.5,0.58,0.64,0.35
0.3,0.1,0.4,0.7,0.2,0.11,0.33,0.4
0.7,0.6,0.5,0.4,0.3,0.41,0.46,0.21
73 changes: 70 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const BUTTONS = [
{ x: 870, y: 0, width: 40, height: 20, color: "#FF9800", text: "LR -" },

{ x: 920, y: 0, width: 40, height: 20, color: "#555555", text: "DR +" },
{ x: 970, y: 0, width: 40, height: 20, color: "#FF5577", text: "DR -" }
{ x: 970, y: 0, width: 40, height: 20, color: "#FF5577", text: "DR -" },

{ x: 1020, y: 0, width: 70, height: 20, color: "#007700", text: "Load CSV" }
];


Expand Down Expand Up @@ -55,9 +57,9 @@ function restart() {
perceptron = new Perceptron(learningRate, 0.00001);

perceptron.createLayers([
{ size: 5, activation: Cell.LINEAR },
{ size: trainingData[0].inputs.length, activation: Cell.LINEAR },
{ size: 4, activation: Cell.SIGMOID },
{ size: 3, activation: Cell.LINEAR },
{ size: trainingData[0].outputs.length, activation: Cell.LINEAR },
]);

perceptron.setInputVector(trainingData[0].inputs);
Expand Down Expand Up @@ -118,6 +120,35 @@ canvas.addEventListener("click", function (event) {
case "DR -":
adjustDropoutRate(-0.01); // Dropout rate decrease
break;
case "Load CSV":
loadCSVData((data) => {
trainingData.length = 0;

const firstRow = data[0];
const inputColumns = [];
const outputColumns = [];

Object.keys(firstRow).forEach((key) => {
if (key.toLowerCase().startsWith("input")) {
inputColumns.push(key);
} else if (key.toLowerCase().startsWith("output")) {
outputColumns.push(key);
}
});

data.forEach((row) => {
const inputs = inputColumns.map((col) => parseFloat(row[col] || 0));
const outputs = outputColumns.map((col) => parseFloat(row[col] || 0));
trainingData.push({ inputs, outputs });
});

restart();

console.log("New training data loaded:", trainingData);
console.log("Input columns:", inputColumns);
console.log("Output columns:", outputColumns);
});
break;
}
}
});
Expand Down Expand Up @@ -340,3 +371,39 @@ function adjustDropoutRate(delta) {
dropoutRate = Math.max(0.0, Math.min(newRate, 1.0));
perceptron.setDropoutRate(dropoutRate);
}

function loadCSVData(callback) {
const inputElement = document.createElement("input");
inputElement.type = "file";
inputElement.accept = ".csv";

inputElement.addEventListener("change", (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const csvContent = e.target.result;
const parsedData = parseCSV(csvContent);
callback(parsedData);
};
reader.readAsText(file);
}
});

inputElement.click();
}


function parseCSV(csvString, delimiter = ",") {
const rows = csvString.trim().split("\n");
const headers = rows[0].split(delimiter);

return rows.slice(1).map((row) => {
const values = row.split(delimiter);
return headers.reduce((acc, header, index) => {
acc[header.trim()] = parseFloat(values[index].trim());
return acc;
}, {});
});
}

0 comments on commit 0f4abbd

Please sign in to comment.