|
1 | 1 | import chalk from 'chalk' |
2 | 2 | import readlineSync from 'readline-sync' |
| 3 | +import inquirer from 'inquirer' |
| 4 | +import { input, confirm } from '@inquirer/prompts' |
3 | 5 |
|
4 | 6 |
|
5 | 7 |
|
6 | 8 | const print = console.log; |
7 | 9 |
|
8 | 10 |
|
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() |
11 | 74 |
|
12 | 75 | if (readlineSync.keyInYNStrict(chalk.green('\nShow visualization'))) { |
13 | 76 | visualization() |
14 | 77 | } |
15 | 78 | } |
16 | 79 |
|
17 | 80 |
|
| 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 | + |
18 | 93 |
|
19 | 94 | function visualization() { |
20 | 95 | print('\n-----------------------------------------------------\n') |
|
0 commit comments