├── .gitattributes
├── .gitignore
├── .npmignore
├── .travis.yml
├── .vscode
└── launch.json
├── LICENSE
├── README.md
├── assets
├── DT.png
├── knngraph.png
├── logistic.png
├── logistic2.png
├── test.csv
├── testSet.txt
└── train.csv
├── docs
├── Matrix.md
├── README.md
├── Vector.md
├── algorithm.md
├── features.md
├── fileParser.md
└── graph.md
├── gulpfile.js
├── package-lock.json
├── package.json
├── src
├── algorithm
│ ├── AdaBoost
│ │ ├── index.ts
│ │ └── test.ts
│ ├── DT
│ │ ├── dt.txt
│ │ ├── index.ts
│ │ └── test.ts
│ ├── index.ts
│ ├── kMeans
│ │ ├── index.ts
│ │ ├── test.ts
│ │ └── testSet.txt
│ ├── kNN
│ │ ├── index.ts
│ │ └── test.ts
│ └── logistic
│ │ ├── index.ts
│ │ └── test.ts
├── index.ts
└── utils
│ ├── .DS_Store
│ ├── charts
│ ├── .DS_Store
│ ├── DT
│ │ └── tpl.html
│ ├── index.ts
│ ├── kNN
│ │ └── tpl.html
│ ├── logistic
│ │ └── tpl.html
│ └── server.ts
│ ├── features
│ ├── index.ts
│ ├── preprocessing.ts
│ └── test.ts
│ ├── fileParser
│ ├── index.ts
│ ├── result.csv
│ └── test.ts
│ ├── index.ts
│ ├── matrix
│ └── index.ts
│ └── vector
│ └── index.ts
├── test
└── utils
│ ├── Matrix.js
│ ├── Vector.js
│ └── fileParser.js
├── tsconfig.json
└── tslint.json
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.html linguist-language=TypeScript
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | lib/
3 | .DS_Store
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | assets/
3 | tslint.json
4 | gulpfile.js
5 | tsconfig.json
6 | dist/maps/
7 | src/
8 | docs/
9 | test/
10 | .DS_Store
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "8.9.1"
4 |
5 | cache:
6 | directories:
7 | - node_modules
8 | script: npm run test
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // 使用 IntelliSense 了解相关属性。
3 | // 悬停以查看现有属性的描述。
4 | // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "type": "node",
9 | "request": "launch",
10 | "name": "Launch Program",
11 | "program": "${file}",
12 | "outFiles": [
13 | "${workspaceRoot}/dist/**/*.js"
14 | ],
15 | "cwd": "${workspaceRoot}",
16 | "sourceMaps": true,
17 | "console": "integratedTerminal"
18 | }
19 | ]
20 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Xia Luo
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # mlhelper
2 | [](https://github.com/laoqiren/mlhelper)
3 | [](https://github.com/laoqiren/mlhelper)
4 |
5 | Algorithms and utils for Machine Learning in JavaScript based on Node.js. while implementing commonly used machine learning algorithms, This library attempts to provide more abundant ecology, such as matrix and vector operations, file parsing, feature engineering, data visualization, and so on.
6 |
7 | *`QQ Group`: 485305514*
8 | ## Installation
9 | ```
10 | $ npm install mlhelper
11 | ```
12 |
13 | ## Documention
14 |
15 | * [algorithm](docs/algorithm.md)
16 | * [Matrix](docs/Matrix.md)
17 | * [Vector](docs/Vector.md)
18 | * [file Parser](docs/fileParser.md)
19 | * [graph tools](docs/graph.md)
20 | * [feature Engineering](docs/features.md)
21 |
22 | ## Example
23 |
24 | ### Algorithm
25 |
26 | ```js
27 | const AdaBoost = require('mlhelper/lib/algorithm').AdaBoost;
28 | //or const AdaBoost = require('mlhelper').algorithm.AdaBoost;
29 |
30 | const dataSet = [
31 | [1.0,2.1],
32 | [2.0,1.1],
33 | [1.3,1.0],
34 | [1.0,1.0],
35 | [2.0,1.0]
36 | ]
37 | const labels = [1.0,1.0,-1.0,-1.0,1.0];
38 | let ada = new AdaBoost(dataSet,labels,40);
39 | let result = ada.classify([[1.0,2.1],
40 | [2.0,1.1],
41 | [1.3,1.0],
42 | [1.0,1.0],
43 | [2.0,1.0]]);
44 | console.log(result); // [ 1, 1, -1, -1, -1 ]
45 | ```
46 |
47 | ### Utils
48 |
49 | **Matrix:**
50 | ```js
51 | const Matrix = require('mlhelper/lib/utils').Matrix;
52 |
53 | let m1 = new Matrix([
54 | [1,2,3],
55 | [3,4,5]
56 | ]);
57 |
58 | let m2 = new Matrix([
59 | [2,2,6],
60 | [3,1,5]
61 | ]);
62 |
63 | console.log(m2.sub(m1)) // Matrix { arr: [ [ 1, 0, 3 ], [ 0, -3, 0 ] ] }
64 | console.log(m1.mult(m2)) // Matrix { arr: [ [ 2, 4, 18 ], [ 9, 4, 25 ] ] }
65 | ```
66 |
67 | **Vector:**
68 | ```js
69 | const Vector = require('mlhelper/lib/utils').Vector;
70 |
71 | let v = new Vector([5,10,7,1]);
72 | console.log(v.argSort()) // [ 3, 0, 2, 1 ]
73 | ```
74 |
75 | **fileParser:**
76 | ```js
77 | const parser = require('mlhelper/lib/utils').fileParser;
78 |
79 | let dt = parser.read_csv(path.join(__dirname,'./train.csv'),{
80 | index_col: 0,
81 | delimiter: ',',
82 | header: 0,
83 | dataType: 'number'
84 | });
85 | let labels = dt.getClasses();
86 | let dataSet =dt.drop('quality').values;
87 | ```
88 |
89 | **Feature Engineering**
90 | ```js
91 | // preprocessing features
92 | const preprocessing = require('mlhelper/lib/utils').features.preprocessing;
93 |
94 | // make the features obey the standard normal distribution(Standardization)
95 | let testStandardScaler = preprocessing.standardScaler(dataSet);
96 |
97 | let testNormalize = preprocessing.normalize(dataSet);
98 |
99 | let testBinarizer = preprocessing.binarizer(dataSet);
100 |
101 | // ...
102 | ```
103 |
104 | **graph tools:**
105 |
106 | Decision Tree:
107 | ```js
108 | charts.drawDT(dt.getTree(),{
109 | width:600,
110 | height:400
111 | });
112 | ```
113 | 
114 |
115 | **logistic regression**
116 | ```js
117 | charts.drawLogistic(dataSet,labels,weights);
118 | ```
119 |
120 |
121 |
122 | ## Contribute
123 |
124 | The original purpose of this project is to learn, and now I need more people to participate in this project, and any issue and good advice is welcome.
125 | ### git clone
126 | ```
127 | git clone https://github.com/laoqiren/mlhelper.git
128 | ```
129 | ### install dependencies&&devdependecies
130 | ```
131 | npm install
132 | ```
133 |
134 | ### development
135 | ```
136 | npm run dev
137 | ```
138 |
139 | ### test
140 | ```
141 | npm run test
142 | ```
143 |
144 | ### build
145 | ```
146 | npm run build
147 | ```
148 | ## LICENSE
149 | MIT.
150 |
151 | *You can use the project for any purpose, except for illegal activities.*
152 |
--------------------------------------------------------------------------------
/assets/DT.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/laoqiren/mlhelper/381bb85f866a1da2cbeda0d59910584e9a68e6c5/assets/DT.png
--------------------------------------------------------------------------------
/assets/knngraph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/laoqiren/mlhelper/381bb85f866a1da2cbeda0d59910584e9a68e6c5/assets/knngraph.png
--------------------------------------------------------------------------------
/assets/logistic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/laoqiren/mlhelper/381bb85f866a1da2cbeda0d59910584e9a68e6c5/assets/logistic.png
--------------------------------------------------------------------------------
/assets/logistic2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/laoqiren/mlhelper/381bb85f866a1da2cbeda0d59910584e9a68e6c5/assets/logistic2.png
--------------------------------------------------------------------------------
/assets/test.csv:
--------------------------------------------------------------------------------
1 | ,fixed acidity,volatile acidity,citric acid,residual sugar,chlorides,free sulfur dioxide,total sulfur dioxide,density,pH,sulphates,alcohol
2 | 0,6.4,0.32,0.27,4.9,0.034,18.0,122.0,0.9916,3.36,0.71,12.5
3 | 1,7.1,0.18,0.39,14.5,0.051,48.0,156.0,0.99947,3.35,0.78,9.1
4 | 2,7.1,0.17,0.4,14.55,0.047,47.0,156.0,0.99945,3.34,0.78,9.1
5 | 3,7.1,0.18,0.39,15.25,0.047,45.0,158.0,0.99946,3.34,0.77,9.1
6 | 4,7.8,0.29,0.29,3.15,0.044,41.0,117.0,0.99153,3.24,0.35,11.5
7 | 5,6.2,0.255,0.27,1.3,0.037,30.0,86.0,0.98834,3.05,0.59,12.9
8 | 6,8.2,0.34,0.29,5.2,0.076,19.0,92.0,0.99138,2.95,0.39,12.5
9 | 7,6.5,0.24,0.28,1.1,0.034,26.0,83.0,0.98928,3.25,0.33,12.3
10 | 8,6.9,0.24,0.23,7.1,0.041,20.0,97.0,0.99246,3.1,0.85,11.4
11 | 9,6.7,0.4,0.22,8.8,0.052,24.0,113.0,0.99576,3.22,0.45,9.4
12 | 10,6.7,0.3,0.44,18.5,0.057,65.0,224.0,0.99956,3.11,0.53,9.1
13 | 11,6.7,0.4,0.22,8.8,0.052,24.0,113.0,0.99576,3.22,0.45,9.4
14 | 12,6.8,0.17,0.32,1.4,0.04,35.0,106.0,0.99026,3.16,0.66,12.0
15 | 13,7.1,0.25,0.28,1.2,0.04,31.0,111.0,0.99174,3.18,0.53,11.1
16 | 14,5.9,0.27,0.27,5.0,0.035,14.0,97.0,0.99058,3.1,0.33,11.8
17 | 15,6.0,0.16,0.22,1.6,0.042,36.0,106.0,0.9905,3.24,0.32,11.4
18 | 16,6.7,0.3,0.44,18.75,0.057,65.0,224.0,0.99956,3.11,0.53,9.1
19 | 17,6.6,0.15,0.32,6.0,0.033,59.0,128.0,0.99192,3.19,0.71,12.1
20 | 18,7.3,0.34,0.3,9.4,0.057,34.0,178.0,0.99554,3.15,0.44,10.4
21 | 19,6.0,0.17,0.29,9.7,0.044,33.0,98.0,0.99536,3.12,0.36,9.2
22 | 20,6.7,0.47,0.29,4.75,0.034,29.0,134.0,0.99056,3.29,0.46,13.0
23 | 21,6.6,0.15,0.32,6.0,0.033,59.0,128.0,0.99192,3.19,0.71,12.1
--------------------------------------------------------------------------------
/assets/testSet.txt:
--------------------------------------------------------------------------------
1 | 5.1,3.5,1.4,0.2,Iris-setosa
2 | 4.9,3.0,1.4,0.2,Iris-setosa
3 | 4.7,3.2,1.3,0.2,Iris-setosa
4 | 4.6,3.1,1.5,0.2,Iris-setosa
5 | 5.0,3.6,1.4,0.2,Iris-setosa
6 | 5.4,3.9,1.7,0.4,Iris-setosa
7 | 4.6,3.4,1.4,0.3,Iris-setosa
8 | 5.0,3.4,1.5,0.2,Iris-setosa
9 | 4.4,2.9,1.4,0.2,Iris-setosa
10 | 4.9,3.1,1.5,0.1,Iris-setosa
11 | 5.4,3.7,1.5,0.2,Iris-setosa
12 | 4.8,3.4,1.6,0.2,Iris-setosa
13 | 4.8,3.0,1.4,0.1,Iris-setosa
14 | 4.3,3.0,1.1,0.1,Iris-setosa
15 | 5.8,4.0,1.2,0.2,Iris-setosa
16 | 5.7,4.4,1.5,0.4,Iris-setosa
17 | 5.4,3.9,1.3,0.4,Iris-setosa
18 | 5.1,3.5,1.4,0.3,Iris-setosa
19 | 5.7,3.8,1.7,0.3,Iris-setosa
20 | 5.1,3.8,1.5,0.3,Iris-setosa
21 | 5.4,3.4,1.7,0.2,Iris-setosa
22 | 5.1,3.7,1.5,0.4,Iris-setosa
23 | 4.6,3.6,1.0,0.2,Iris-setosa
24 | 5.1,3.3,1.7,0.5,Iris-setosa
25 | 4.8,3.4,1.9,0.2,Iris-setosa
26 | 5.0,3.0,1.6,0.2,Iris-setosa
27 | 5.0,3.4,1.6,0.4,Iris-setosa
28 | 5.2,3.5,1.5,0.2,Iris-setosa
29 | 5.2,3.4,1.4,0.2,Iris-setosa
30 | 4.7,3.2,1.6,0.2,Iris-setosa
31 | 4.8,3.1,1.6,0.2,Iris-setosa
32 | 5.4,3.4,1.5,0.4,Iris-setosa
33 | 5.2,4.1,1.5,0.1,Iris-setosa
34 | 5.5,4.2,1.4,0.2,Iris-setosa
35 | 4.9,3.1,1.5,0.1,Iris-setosa
36 | 5.0,3.2,1.2,0.2,Iris-setosa
37 | 5.5,3.5,1.3,0.2,Iris-setosa
38 | 4.9,3.1,1.5,0.1,Iris-setosa
39 | 4.4,3.0,1.3,0.2,Iris-setosa
40 | 5.1,3.4,1.5,0.2,Iris-setosa
41 | 5.0,3.5,1.3,0.3,Iris-setosa
42 | 4.5,2.3,1.3,0.3,Iris-setosa
43 | 4.4,3.2,1.3,0.2,Iris-setosa
44 | 5.0,3.5,1.6,0.6,Iris-setosa
45 | 5.1,3.8,1.9,0.4,Iris-setosa
46 | 4.8,3.0,1.4,0.3,Iris-setosa
47 | 5.1,3.8,1.6,0.2,Iris-setosa
48 | 4.6,3.2,1.4,0.2,Iris-setosa
49 | 5.3,3.7,1.5,0.2,Iris-setosa
50 | 5.0,3.3,1.4,0.2,Iris-setosa
51 | 7.0,3.2,4.7,1.4,Iris-versicolor
52 | 6.4,3.2,4.5,1.5,Iris-versicolor
53 | 6.9,3.1,4.9,1.5,Iris-versicolor
54 | 5.5,2.3,4.0,1.3,Iris-versicolor
55 | 6.5,2.8,4.6,1.5,Iris-versicolor
56 | 5.7,2.8,4.5,1.3,Iris-versicolor
57 | 6.3,3.3,4.7,1.6,Iris-versicolor
58 | 4.9,2.4,3.3,1.0,Iris-versicolor
59 | 6.6,2.9,4.6,1.3,Iris-versicolor
60 | 5.2,2.7,3.9,1.4,Iris-versicolor
61 | 5.0,2.0,3.5,1.0,Iris-versicolor
62 | 5.9,3.0,4.2,1.5,Iris-versicolor
63 | 6.0,2.2,4.0,1.0,Iris-versicolor
64 | 6.1,2.9,4.7,1.4,Iris-versicolor
65 | 5.6,2.9,3.6,1.3,Iris-versicolor
66 | 6.7,3.1,4.4,1.4,Iris-versicolor
67 | 5.6,3.0,4.5,1.5,Iris-versicolor
68 | 5.8,2.7,4.1,1.0,Iris-versicolor
69 | 6.2,2.2,4.5,1.5,Iris-versicolor
70 | 5.6,2.5,3.9,1.1,Iris-versicolor
71 | 5.9,3.2,4.8,1.8,Iris-versicolor
72 | 6.1,2.8,4.0,1.3,Iris-versicolor
73 | 6.3,2.5,4.9,1.5,Iris-versicolor
74 | 6.1,2.8,4.7,1.2,Iris-versicolor
75 | 6.4,2.9,4.3,1.3,Iris-versicolor
76 | 6.6,3.0,4.4,1.4,Iris-versicolor
77 | 6.8,2.8,4.8,1.4,Iris-versicolor
78 | 6.7,3.0,5.0,1.7,Iris-versicolor
79 | 6.0,2.9,4.5,1.5,Iris-versicolor
80 | 5.7,2.6,3.5,1.0,Iris-versicolor
81 | 5.5,2.4,3.8,1.1,Iris-versicolor
82 | 5.5,2.4,3.7,1.0,Iris-versicolor
83 | 5.8,2.7,3.9,1.2,Iris-versicolor
84 | 6.0,2.7,5.1,1.6,Iris-versicolor
85 | 5.4,3.0,4.5,1.5,Iris-versicolor
86 | 6.0,3.4,4.5,1.6,Iris-versicolor
87 | 6.7,3.1,4.7,1.5,Iris-versicolor
88 | 6.3,2.3,4.4,1.3,Iris-versicolor
89 | 5.6,3.0,4.1,1.3,Iris-versicolor
90 | 5.5,2.5,4.0,1.3,Iris-versicolor
91 | 5.5,2.6,4.4,1.2,Iris-versicolor
92 | 6.1,3.0,4.6,1.4,Iris-versicolor
93 | 5.8,2.6,4.0,1.2,Iris-versicolor
94 | 5.0,2.3,3.3,1.0,Iris-versicolor
95 | 5.6,2.7,4.2,1.3,Iris-versicolor
96 | 5.7,3.0,4.2,1.2,Iris-versicolor
97 | 5.7,2.9,4.2,1.3,Iris-versicolor
98 | 6.2,2.9,4.3,1.3,Iris-versicolor
99 | 5.1,2.5,3.0,1.1,Iris-versicolor
100 | 5.7,2.8,4.1,1.3,Iris-versicolor
--------------------------------------------------------------------------------
/assets/train.csv:
--------------------------------------------------------------------------------
1 | ,fixed acidity,volatile acidity,citric acid,residual sugar,chlorides,free sulfur dioxide,total sulfur dioxide,density,pH,sulphates,alcohol,quality
2 | 0,7.0,0.27,0.36,20.7,0.045,45.0,170.0,1.001,3.0,0.45,8.8,6
3 | 1,6.3,0.3,0.34,1.6,0.049,14.0,132.0,0.994,3.3,0.49,9.5,6
4 | 2,8.1,0.28,0.4,6.9,0.05,30.0,97.0,0.9951,3.26,0.44,10.1,6
5 | 3,7.2,0.23,0.32,8.5,0.058,47.0,186.0,0.9956,3.19,0.4,9.9,6
6 | 4,7.2,0.23,0.32,8.5,0.058,47.0,186.0,0.9956,3.19,0.4,9.9,6
7 | 5,8.1,0.28,0.4,6.9,0.05,30.0,97.0,0.9951,3.26,0.44,10.1,6
8 | 6,6.2,0.32,0.16,7.0,0.045,30.0,136.0,0.9949,3.18,0.47,9.6,6
9 | 7,7.0,0.27,0.36,20.7,0.045,45.0,170.0,1.001,3.0,0.45,8.8,6
10 | 8,6.3,0.3,0.34,1.6,0.049,14.0,132.0,0.994,3.3,0.49,9.5,6
11 | 9,8.1,0.22,0.43,1.5,0.044,28.0,129.0,0.9938,3.22,0.45,11.0,6
12 | 10,8.1,0.27,0.41,1.45,0.033,11.0,63.0,0.9908,2.99,0.56,12.0,5
13 | 11,8.6,0.23,0.4,4.2,0.035,17.0,109.0,0.9947,3.14,0.53,9.7,5
14 | 12,7.9,0.18,0.37,1.2,0.04,16.0,75.0,0.992,3.18,0.63,10.8,5
15 | 13,6.6,0.16,0.4,1.5,0.044,48.0,143.0,0.9912,3.54,0.52,12.4,7
16 | 14,8.3,0.42,0.62,19.25,0.04,41.0,172.0,1.0002,2.98,0.67,9.7,5
17 | 15,6.6,0.17,0.38,1.5,0.032,28.0,112.0,0.9914,3.25,0.55,11.4,7
18 | 16,6.3,0.48,0.04,1.1,0.046,30.0,99.0,0.9928,3.24,0.36,9.6,6
19 | 17,6.2,0.66,0.48,1.2,0.029,29.0,75.0,0.9892,3.33,0.39,12.8,8
20 | 18,7.4,0.34,0.42,1.1,0.033,17.0,171.0,0.9917,3.12,0.53,11.3,6
21 | 19,6.5,0.31,0.14,7.5,0.044,34.0,133.0,0.9955,3.22,0.5,9.5,5
22 | 20,6.2,0.66,0.48,1.2,0.029,29.0,75.0,0.9892,3.33,0.39,12.8,8
23 | 21,6.4,0.31,0.38,2.9,0.038,19.0,102.0,0.9912,3.17,0.35,11.0,7
24 | 22,6.8,0.26,0.42,1.7,0.049,41.0,122.0,0.993,3.47,0.48,10.5,8
25 | 23,7.6,0.67,0.14,1.5,0.074,25.0,168.0,0.9937,3.05,0.51,9.3,5
26 | 24,6.6,0.27,0.41,1.3,0.052,16.0,142.0,0.9951,3.42,0.47,10.0,6
27 | 25,7.0,0.25,0.32,9.0,0.046,56.0,245.0,0.9955,3.25,0.5,10.4,6
28 | 26,6.9,0.24,0.35,1.0,0.052,35.0,146.0,0.993,3.45,0.44,10.0,6
29 | 27,7.0,0.28,0.39,8.7,0.051,32.0,141.0,0.9961,3.38,0.53,10.5,6
30 | 28,7.4,0.27,0.48,1.1,0.047,17.0,132.0,0.9914,3.19,0.49,11.6,6
31 | 29,7.2,0.32,0.36,2.0,0.033,37.0,114.0,0.9906,3.1,0.71,12.3,7
32 | 30,8.5,0.24,0.39,10.4,0.044,20.0,142.0,0.9974,3.2,0.53,10.0,6
33 | 31,8.3,0.14,0.34,1.1,0.042,7.0,47.0,0.9934,3.47,0.4,10.2,6
34 | 32,7.4,0.25,0.36,2.05,0.05,31.0,100.0,0.992,3.19,0.44,10.8,6
--------------------------------------------------------------------------------
/docs/Matrix.md:
--------------------------------------------------------------------------------
1 | # Matrix utils
2 |
3 | ## constructor(arr: Array>)
4 | *arr:* The Original 2D array data.
5 |
6 | ## toArray(): Array>
7 | Get the original array.
8 |
9 | ## size(): [number,number]
10 | Get the size of Matrix, including rows and columns.
11 |
12 | ## sum(axis=1 as number): Array
13 | The sum of data in the same row/column
14 |
15 | *axis:* If axis is set to 1, the sum of all the data of the same row is calculated, otherwise the column is computed. Default to 1.
16 |
17 | ## min(axis=0 as number): Array\
18 | The minimum value of data in the same row/column.
19 |
20 | *axis:* If axis is set to 1, the minimum value of all data in the same row is calculated, otherwise the column is computed. default to 0.
21 |
22 | ## max(axis=0 as number): Array\
23 | The maximum value of data in the same row/column.
24 |
25 | *axis:* If axis is set to 1, the maximum value of all data in the same row is calculated, otherwise the column is computed. default to 0.
26 |
27 | ## static mean(arr: Array>, axis=0 as number): Array\
28 | The average value of the same row/column of data
29 |
30 | *arr:* dataset to calculate.
31 | *axis:* If axis is set to 1, the average value of all the data in the same row is calculated, otherwise the column is computed
32 |
33 | ## transpose(): Array>
34 | Transpose the Matrix.
35 |
36 | ## static zeros(r: number,c?: number): Array>|Array\
37 |
38 | If there are two parameters, the zero matrix of the specified size is returned. If there is only one parameter, the one dimensional array is returned
39 |
40 | ## static ones(m: number,n?: number): Array>|Array\
41 |
42 | Be similar with `zeros()`.
43 | ## sub(toSub: Matrix): Matrix
44 | Matrix subtraction.
45 |
46 | *toSub:* Matrix to sub with.
47 |
48 |
49 | ## add(toAdd: Matrix): Matrix
50 | Matrix addition.
51 |
52 | *toAdd:* Matrix to add with.
53 |
54 | ## mult(toMult: Matrix): Matrix
55 | Matrix multiplication.
56 |
57 | *toMult:* Matrix to multiply with.
58 |
59 | ## divide(toDivide: Matrix): Matrix
60 | Matrix Division.
61 |
62 | *toDivide:* Matrix to divide with.
63 |
64 |
65 | ```js
66 | const Matrix = require('mlhelper').utils.Matrix;
67 |
68 | const dataSet = [
69 | [2,4,6],
70 | [5,7,1],
71 | [3,3,1]
72 | ];
73 | const dataSet2 = [
74 | [1,3,5],
75 | [2,4,7],
76 | [3,5,8]
77 | ];
78 | let matA = new Matrix(dataSet),
79 | matB = new Matrix(dataSet2);
80 |
81 |
82 | let result = matA.add(matB);
83 | expect(result.toArray()).to.eql([
84 | [3,7,11],
85 | [7,11,8],
86 | [6,8,9]
87 | ]); // true;
88 |
89 | expect(matA.max(1)).to.eql([6,7,3]); //true
90 | expect(matA.max(0)).to.eql([5,7,6]); //true
91 |
92 | expect(matA.transpose()).to.eql([[2,5,3],[4,7,3],[6,1,1]]); //true
93 |
94 | expect(Matrix.ones(2,2)).to.eql([[1,1],[1,1]]); //true
95 | expect(Matrix.ones(2,2)).to.eql([[1,1],[1,1]]); //true
96 | ```
--------------------------------------------------------------------------------
/docs/README.md:
--------------------------------------------------------------------------------
1 | # mlhelper documentation
2 |
3 | * [algorithm](algorithm.md)
4 | * [Matrix](Matrix.md)
5 | * [Vector](Vector.md)
6 | * [file Parser](fileParser.md)
7 | * [graph tools](graph.md)
8 | * [feature Engineering](features.md)
9 |
10 | *中文文档敬请期待!也欢迎参与文档翻译。*
--------------------------------------------------------------------------------
/docs/Vector.md:
--------------------------------------------------------------------------------
1 | # Vector utils
2 |
3 | ## constructor(arr: Array\)
4 | *arr:* One-dimensional array.
5 |
6 | ## argSort(): Array\
7 | The sorted index of array.
8 |
9 | ## static sign(arr: number|Array\): number|Array\
10 |
11 | For each element, when its value equals to 0 returns 0, else if it's larger than 0 returns 1 else returns -1. If arr is a number, just return a result number.
12 |
13 | ## static rand(m: number)
14 | Create specific number of random numbers between 0 and 1.
15 |
16 | ```js
17 | const Vector = require('mlhelper').utils.Vector;
18 |
19 | const arr = [4,7,1,8,2];
20 | const vect = new Vector(arr);
21 |
22 | expect(vect.argSort()).to.eql([2,4,0,1,3]); //true
23 | expect(Vector.sign([-2,2,0,4])).to.eql([-1,1,0,1]); //true
24 | expect(Vector.sign(-6)).to.eql(-1); // true
25 | ```
--------------------------------------------------------------------------------
/docs/algorithm.md:
--------------------------------------------------------------------------------
1 | # Algorithms
2 |
3 | ## kNN (k-nearest neighbors algorithm)
4 |
5 | wiki: [https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm)
6 | ### constructor(dataSet: Array>,labels: Array\)
7 |
8 | *dataSet:* The two dimensional array of data sets with known classifications.
9 |
10 | *labels:* Classification vector of dataset.
11 |
12 | ### classify(inx: Array\,k: number): any
13 | Classification based on feature vectors.
14 |
15 | *inx:* data for testing.
16 |
17 | *k:* Make decisions based on K nearest neighbors.
18 |
19 | ```js
20 | const kNN = require('mlhelper').algorithm.kNN;
21 |
22 | let knn = new kNN([
23 | [1.,1.1],
24 | [1.,1.],
25 | [0.,0.],
26 | [0.,0.1]
27 | ],['A','A','B','C']);
28 |
29 | let result = knn.classify([1.1,0.8],4);
30 |
31 | console.log(result) // 'A'
32 | ```
33 | ### autoNormalVector(inx: Array\): Array\
34 | Normalize the test data vectors so that each feature is concentrated between 0 and 1.
35 |
36 | ### static autoNormal(dataSet: Array>): Array>
37 | Normalize the dataset matrix so that each feature is concentrated between 0 and 1.
38 | ## DT(ID3) Decision tree
39 | wiki: [https://en.wikipedia.org/wiki/Decision_tree](https://en.wikipedia.org/wiki/Decision_tree)
40 | ### constructor(dataSet: Array>,labels: Array\,alg: string="ID3")
41 |
42 | *dataSet:* The two dimensional array of data sets with known classifications(every row including the class).
43 |
44 | *labels:* Classification vector of dataset.
45 |
46 | *alg:* Algorithm to create decision tree. Default is ID3. By now, only ID3 is supported.
47 |
48 | ### classify(featLabels: Array\,testVec: Array\): any
49 | *featLabels:* vector of feature names.
50 |
51 | *testVec:* vector of test data.
52 |
53 | ### getTree(): object
54 |
55 | return the created decision tree.
56 |
57 | ```js
58 | const DT = require('mlhelper').algorithm.DT;
59 |
60 | let dataSet = parser.parseFile(path.join(__dirname,'./dt.txt'));
61 |
62 | let labels = ['age','prescript','astigmatic','tearRate']
63 | let dt = new DT(dataSet,labels);
64 |
65 | let result = dt.classify(labels,["young","myope","no","reduced"]) //no lenses
66 |
67 | console.log(dt.getTree()); // { tearRate: { reduced: 'no lenses', normal: { astigmatic: [Object] } } }
68 | ```
69 |
70 | ### storeTree(filePath: string): Promise
71 |
72 | store the decision tree to file and returns Promise object.
73 |
74 | ### static classifyFromTree(inputTree: object,featLabels: Array\,testVec: Array\): any
75 |
76 | Classify the data according to the existing decision tree. The meaning of the parameter refers to the above explanation.
77 |
78 | ## Logistic regression
79 | wiki: [https://en.wikipedia.org/wiki/Logistic_regression](https://en.wikipedia.org/wiki/Logistic_regression)
80 |
81 | ### constructor(dataMatIn: Array>,classLabels: Array\,numIter: number)
82 | *dataMatIn* like dataset for training.
83 |
84 | *classLabels:* the classes of training datas.
85 |
86 | *numIter:* Maximum iterations
87 |
88 | ### classify(inX: Array\): number
89 | Claasify the test data.
90 |
91 | ### getWeights(): Array\
92 | Random gradient ascent method for optimal regression coefficients of each feature.
93 |
94 | ```js
95 | let logi = new Logistic(dataSet,labels,150);
96 | let result = logi.classify(dataSet[i]);
97 | let weights = logi.getWeights();
98 | console.log(weights); //[ 2.9301940437635965, -5.7803993740016555, 9.834929045066424 ]
99 | ```
100 |
101 | ### AdaBoost
102 | wiki: [https://en.wikipedia.org/wiki/AdaBoost](https://en.wikipedia.org/wiki/AdaBoost)
103 | ### constructor(dataSet: Array>,labels: Array\,numInt=40 as number)
104 |
105 | *dataSet:* matirx like datas for training.
106 |
107 | *labels:* vector of the training datas' classes.
108 |
109 | *maximum:* permission iterative number of times, default is 40.
110 |
111 | ### classify(inx: Array>): Array\
112 | *inx:* Matrix like for testing.
113 |
114 | ```js
115 | const AdaBoost = require('mlhelper').algorithm.AdaBoost;
116 | const dataSet = [
117 | [1.0,2.1],
118 | [2.0,1.1],
119 | [1.3,1.0],
120 | [1.0,1.0],
121 | [2.0,1.0]
122 | ]
123 | const labels = [1.0,1.0,-1.0,-1.0,1.0];
124 |
125 | let ada = new AdaBoost(dataSet,labels,40);
126 |
127 | let result = ada.classify([[1.0,2.1],
128 | [2.0,1.1],
129 | [1.3,1.0],
130 | [1.0,1.0],
131 | [2.0,1.0]]);
132 |
133 | console.log(result); //[ 1, 1, -1, -1, -1 ]
134 | ```
135 |
136 | ### k-means clustering
137 | wiki: [https://en.wikipedia.org/wiki/K-means_clustering](https://en.wikipedia.org/wiki/K-means_clustering)
138 | ### constructor(dataSet: Array\>,k: number)
139 | *dataSet:* Matrix like dataset to cluster.
140 |
141 | *k:* how many centroids.
142 |
143 | ### cluster(max=50 as number): [Array>,Array>]
144 |
145 | *max:* permission iterative number of times, default is 50.
146 |
147 | *return:* will return an array `[centroids,clusterAssment]`, the `centroids` is the coordinate matrix of all cluster centers and `clusterAssment` is an array `[centroidsIndex,minDist**2]`, `centroidsIndex` is the the index of the center to which the point belongs and `minDist` is the The distance between the point and its center.
148 |
149 | ```js
150 | const kMeans = require('mlhelper').algorithm.kMeans;
151 | let kmeans = new kMeans(dataSet,5);
152 |
153 | let result = kmeans.cluster(40);
154 | console.log(util.inspect(result))
155 | ```
156 |
157 | ## TODO
--------------------------------------------------------------------------------
/docs/features.md:
--------------------------------------------------------------------------------
1 | # Feature engineering
--------------------------------------------------------------------------------
/docs/fileParser.md:
--------------------------------------------------------------------------------
1 | # File Parser utils
2 |
3 | ## parseFile(filePath: string,options: object): Array>
4 | Parsing with simple files.
5 |
6 | ### filePath:
7 | the absolute file path to read.
8 | ### options: \