You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/python.md
+7-12Lines changed: 7 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,7 @@ import { LinearRegression } from 'scikitjs'
52
52
letX= [[1], [2]]
53
53
let y = [10, 20]
54
54
let lr =newLinearRegression({ fitIntercept:false })
55
-
lr.fit(X, y)
55
+
awaitlr.fit(X, y)
56
56
```
57
57
58
58
You'll also notice in the code above, these are actual classes in JS, so you'll need to `new` them.
@@ -82,20 +82,19 @@ import { LinearRegression } from 'scikitjs'
82
82
letX= [[1], [2]]
83
83
let y = [10, 20]
84
84
let lr =newLinearRegression({ fitIntercept:false })
85
-
lr.fit(X, y)
85
+
awaitlr.fit(X, y)
86
86
console.log(lr.coef)
87
87
```
88
88
89
89
In the code sample above, we see that `fit_intercept` turns into `fitIntercept` (and it's an object). And `coef` turns into `coef`.
90
90
91
-
**3. The `fit` function for some estimators will be asynchronous, and so it will be called `fitAsync`**
91
+
**3. Always await calls to .fit or .fitPredict**
92
92
93
-
In Javascript there are many cases where you can't tie up the main thread. In those cases it's best to use async functions.
93
+
It's common practice in Javascript to not tie up the main thread. Many libraries, including tensorflow.js only give an async "fit" function.
94
94
95
-
Moreover some underlying libraries (tensorflow.js) only provide a `fit` function that is asynchronous. So if you wish to use those libraries, your `fit` function will also be async. But I didn't want to ship a library where users didn't easily know whether a function was asynchronous or not, so I opted to with the following convention:
95
+
So if we build on top of them our fit functions will be asynchronous. But what happens if we make our own estimator that has a synchronous fit function? Should we burden the user with finding out if their fit function is async or not, and then "awaiting" the proper one? I think not.
96
96
97
-
- If your `fit` is synchronous, it is called `fit`.
98
-
- If it is async, then it is called `fitAsync`.
97
+
I think we should simply await all calls to fit. If you await a synchronous function, it resolves immediately and you are on your merry way. So I literally await all calls to .fit and you should too.
99
98
100
99
#### python
101
100
@@ -118,10 +117,6 @@ import { LogisticRegression } from 'scikitjs'
118
117
letX= [[1], [-1]]
119
118
let y = [1, 0]
120
119
let lr =newLogisticRegression({ fitIntercept:false })
121
-
awaitlr.fitAsync(X, y)
120
+
awaitlr.fit(X, y)
122
121
console.log(lr.coef)
123
122
```
124
-
125
-
Why exactly is a `LinearRegression` synchronous, while a `LogisticRegression` is not? Well in the case of a `LinearRegression`, there exist [closed form](https://eli.thegreenplace.net/2014/derivation-of-the-normal-equation-for-linear-regression/) solutions that compute the proper coefficients. That is not the case in a `LogisticRegression`.
126
-
127
-
In the case of a `LogisticRegression` I opt for a SGD solution using the underlying `tensorflow.js` library for speed. The `fit` function that `tensorflow.js` gives me is async, and so therefore, mine is as well.
0 commit comments