forked from tzubertowski/ivan-clothing-predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (63 loc) · 2.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const { DecisionTreeClassifier } = require('ml-cart');
const fs = require('fs');
const readline = require('readline');
// Read the training data from a JSON file
const trainingDataPath = './training_data.json';
// Structure is like
// [
// [mondays data],
// [tuesdays data],
// [wednesday data],
// [thursday data],
// [friday data]
// ]
const trainingData = JSON.parse(fs.readFileSync(trainingDataPath, 'utf8'));
// Map clothing choices to integer labels
const labelMap = new Map([
[0, 0], // Casual top
[0.5, 1], // Polo
[1, 2] // Shirt
]);
const labelToClothesMap = {
"0.5": 'Polo',
"1": 'Shirt',
"0": 'Casual'
}
const trainingDataFormatted = trainingData.map(data => {
const mappedData = data.slice();
mappedData[mappedData.length - 1] = labelMap.get(mappedData[mappedData.length - 1]);
return mappedData;
});
// Prepare the training data
const trainingFeatures = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
const X = trainingDataFormatted.map(data => data.slice(0, -1));
const y = trainingDataFormatted.map(data => data[data.length - 1]);
// Train the decision tree classifier
const dt = new DecisionTreeClassifier();
dt.train(X, y, trainingFeatures);
// Predict function
function predictClothing(input) {
const prediction = dt.predict([input]);
console.log(prediction)
let predictedLabel = Array.from(labelMap.keys()).find(key => labelMap.get(key) === prediction[0]) + "";
return labelToClothesMap[predictedLabel]
}
// Create readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Ask user for the day to predict
rl.question('Enter the day (Monday, Tuesday, Wednesday, Thursday, Friday): ', (day) => {
rl.close();
// Convert user input to lowercase and get the corresponding index
const dayIndex = trainingFeatures.indexOf(day.trim());
if (dayIndex < 0) {
console.log('Invalid day input. Please select a valid day.');
return;
}
// Get the input for the selected day
const input = trainingData[dayIndex].slice(0, -1);
const predictedClothing = predictClothing(input);
console.log(`Based on the given data, Ivan should wear: ${predictedClothing}`);
});