@@ -4,7 +4,9 @@ import { input, confirm } from '@inquirer/prompts'
44
55
66
7- // Simplify std out from `console.log` into `print`
7+ /**
8+ * Simplify std out from `console.log` into `print`
9+ */
810const print = console . log ;
911
1012
@@ -13,7 +15,7 @@ const print = console.log;
1315 * Process linear regression with single feature
1416 *
1517 * Formula
16- * y = a + bx
18+ * ` y = β0 + β1x1`
1719 *
1820 * @param {Array } data - the raw data
1921 */
@@ -39,7 +41,8 @@ export default async function singleFeatureProcess(data) {
3941 if ( answers . feature == answers . label ) {
4042 print ( chalk . redBright ( '\n[!] Feature and label can not be same' ) )
4143
42- if ( await confirm ( { message : 'Back?' } ) ) return ;
44+ await confirm ( { message : 'Back?' } )
45+ return
4346 }
4447
4548
@@ -48,15 +51,35 @@ export default async function singleFeatureProcess(data) {
4851 if ( ! numberPattern . test ( item [ answers . feature ] ) || ! numberPattern . test ( item [ answers . label ] ) ) {
4952 print ( chalk . redBright ( '\n[!] Some data is not a number! Unable to process further!' ) )
5053 await confirm ( { message : 'Continue?' } )
51- return ;
54+ return
5255 }
5356 }
5457
5558
56- /**
57- * Calculate the value a and b to get equation y = a + bx
58- * Let's assume `feature` is `x` and `label` is `y`
59- */
59+ const equation = modelling ( data , answers )
60+
61+
62+ if ( await confirm ( { message : 'Do you want to test?' } ) ) {
63+ do {
64+ await testing ( equation , answers . feature , answers . label )
65+ } while ( await confirm ( { message : 'Do you want to test again?' } ) ) ;
66+ }
67+ }
68+
69+
70+
71+ /**
72+ * Calculate the value a and b to get equation y = a + bx
73+ * a : intercept
74+ * b : slope
75+ *
76+ * `feature` is `x` and `label` is `y`
77+ *
78+ * @param {Array } data - raw data
79+ * @param {Object } answers - the input of features and label from user
80+ * @returns {function(x): number } a function of linear equation
81+ */
82+ function modelling ( data , answers ) {
6083 const sumX = data . reduce ( ( sum , val ) => sum + parseInt ( val [ answers . feature ] ) , 0 )
6184 const sumY = data . reduce ( ( sum , val ) => sum + parseInt ( val [ answers . label ] ) , 0 )
6285 const sumXY = data . reduce ( ( sum , val ) => sum + ( parseInt ( val [ answers . feature ] ) * parseInt ( val [ answers . label ] ) ) , 0 )
@@ -70,32 +93,22 @@ export default async function singleFeatureProcess(data) {
7093
7194 const a = ( sumY / data . length ) - ( b * ( sumX / data . length ) )
7295
73- // Equation result
96+ /**
97+ * Linear equation result based on data processing
98+ *
99+ * @param {number } x - a feature data
100+ * @returns {number } a label/target/`y` value result
101+ */
74102 const y = ( x ) => a + ( b * x )
75103
76104 // Print the equation
77- print ( chalk . blueBright ( `\nEquation result: ${ a } + ${ b } x` ) )
78-
79-
80-
81- if ( await confirm ( { message : 'Do you want to test?' } ) ) {
82- do {
83- await testing ( y , answers . feature , answers . label )
84- } while ( await confirm ( { message : 'Do you want to test again?' } ) ) ;
85- }
86-
105+ print ( chalk . blueBright ( `\nEquation result: y = ${ a } + ${ b } x` ) )
87106
88- evaluation ( )
107+ return y
89108}
90109
91110
92- async function testing ( y , feature , label ) {
111+ async function testing ( equation , feature , label ) {
93112 const inputFeature = await input ( { message : `Input ${ feature } ` } )
94- print ( `Prediction ${ label } : ${ y ( inputFeature ) } ` )
95- }
96-
97-
98- function evaluation ( ) {
99- print ( 'Evaluation!' )
100- print ( 'MSE, MAE, RMSE' )
113+ print ( `Prediction ${ label } : ${ equation ( inputFeature ) } ` )
101114}
0 commit comments