Skip to content

Commit f13910b

Browse files
committed
add calculation for single feature data type
1 parent 32a2af9 commit f13910b

File tree

2 files changed

+94
-20
lines changed

2 files changed

+94
-20
lines changed

src/cli/main.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import multiFeatureProcess from './multi-feature.js'
2323
const print = console.log;
2424

2525

26-
const DATA_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'data');
26+
const DATA_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'data');
2727

2828

2929
/////////////////////////////////////////////////////////////////////
@@ -138,18 +138,7 @@ function readFileAsync(filePath) {
138138
fs.createReadStream(filePath)
139139
.pipe(csv())
140140
.on('data', (row) => data.push(row))
141-
.on('end', () => {
142-
const table = new Table({
143-
head: Object.keys(data[0])
144-
})
145-
146-
data.forEach(row => {
147-
table.push(Object.values(row))
148-
})
149-
150-
console.log(table.toString())
151-
resolve()
152-
})
141+
.on('end', () => resolve(data))
153142
.on('error', (error) => reject(error))
154143
})
155144
}
@@ -162,17 +151,27 @@ async function previewData(filePath) {
162151
print('\n-----------------------------------------------------\n')
163152
print('Preview data: ', filePath)
164153

165-
await readFileAsync(path.join(DATA_DIR, filePath))
154+
const data = await readFileAsync(path.join(DATA_DIR, filePath))
155+
156+
const table = new Table({
157+
head: Object.keys(data[0])
158+
})
159+
160+
data.forEach(row => {
161+
table.push(Object.values(row))
162+
})
163+
164+
print(table.toString())
166165

167166
if (readlineSync.keyInYNStrict(chalk.green('\nContinue next process'))) {
168-
await dataIdentification()
167+
await dataIdentification(data)
169168
}
170169
}
171170

172171

173172

174173

175-
async function dataIdentification() {
174+
async function dataIdentification(data) {
176175
print('\n-----------------------------------------------------\n')
177176
print('Data identification')
178177

@@ -191,10 +190,10 @@ async function dataIdentification() {
191190

192191
switch (answers.type) {
193192
case '1. Single feature':
194-
singleFeatureProcess()
193+
await singleFeatureProcess(data)
195194
break;
196195
case '2. Multi feature':
197-
multiFeatureProcess()
196+
await multiFeatureProcess(data)
198197
break;
199198
default:
200199
print('Invalid input!')

src/cli/single-feature.js

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,95 @@
11
import chalk from 'chalk'
22
import readlineSync from 'readline-sync'
3+
import inquirer from 'inquirer'
4+
import { input, confirm } from '@inquirer/prompts'
35

46

57

68
const print = console.log;
79

810

9-
export default function singleFeatureProcess() {
10-
print('Single feature process')
11+
12+
/**
13+
* Process linear regression with single feature
14+
*
15+
* Formula
16+
* y = a + bx
17+
*
18+
* @param {Array} data - the raw data
19+
*/
20+
export default async function singleFeatureProcess(data) {
21+
console.clear()
22+
print('\n=====================================================\n')
23+
print(chalk.blue('SINGLE FEATURE PROCESS\n'))
24+
25+
const answers = await inquirer.prompt([
26+
{
27+
type: 'list',
28+
name: 'feature',
29+
message: 'Choose feature',
30+
choices: Object.keys(data[0])
31+
},
32+
{
33+
type: 'list',
34+
name: 'label',
35+
message: 'Choose label',
36+
choices: Object.keys(data[0])
37+
}
38+
])
39+
40+
41+
/**
42+
* Calculate the value a and b to get equation y = a + bx
43+
* Let's assume `feature` is `x` and `label` is `y`
44+
*/
45+
const sumX = data.reduce((sum, val) => sum + parseInt(val[answers.feature]), 0)
46+
const sumY = data.reduce((sum, val) => sum + parseInt(val[answers.label]), 0)
47+
const sumXY = data.reduce((sum, val) => sum + (parseInt(val[answers.feature]) * parseInt(val[answers.label])), 0)
48+
const sumXSquared = data.reduce((sum, val) => sum + parseInt(val[answers.feature]) ** 2, 0)
49+
const sumXTimesSumY = sumX * sumY
50+
51+
const b = (
52+
((data.length * sumXY) - sumXTimesSumY) /
53+
((data.length * sumXSquared) - (sumX ** 2))
54+
)
55+
56+
const a = (sumY / data.length) - (b * (sumX / data.length))
57+
58+
// Equation result
59+
const y = (x) => a + (b * x)
60+
61+
// Print the equation
62+
print(chalk.blueBright(`\nEquation result: ${a} + ${b}x`))
63+
64+
65+
66+
if (await confirm({ message: 'Do you want to test?'})) {
67+
do {
68+
await testing(y, answers.feature, answers.label)
69+
} while (await confirm({ message: 'Do you want to test again?'}));
70+
}
71+
72+
73+
evaluation()
1174

1275
if (readlineSync.keyInYNStrict(chalk.green('\nShow visualization'))) {
1376
visualization()
1477
}
1578
}
1679

1780

81+
async function testing(y, feature, label) {
82+
const inputFeature = await input({message: `Input ${feature}`})
83+
print(`Prediction ${label}: ${y(inputFeature)}`)
84+
}
85+
86+
87+
function evaluation() {
88+
print('Evaluation!')
89+
print('MSE, MAE, RMSE')
90+
}
91+
92+
1893

1994
function visualization() {
2095
print('\n-----------------------------------------------------\n')

0 commit comments

Comments
 (0)