🌐 AI搜索 & 代理 主页
Skip to content

Commit a643b51

Browse files
committed
Updated docs
1 parent 42de904 commit a643b51

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

docs/docs/python.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import { LinearRegression } from 'scikitjs'
5252
let X = [[1], [2]]
5353
let y = [10, 20]
5454
let lr = new LinearRegression({ fitIntercept: false })
55-
lr.fit(X, y)
55+
await lr.fit(X, y)
5656
```
5757

5858
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'
8282
let X = [[1], [2]]
8383
let y = [10, 20]
8484
let lr = new LinearRegression({ fitIntercept: false })
85-
lr.fit(X, y)
85+
await lr.fit(X, y)
8686
console.log(lr.coef)
8787
```
8888

8989
In the code sample above, we see that `fit_intercept` turns into `fitIntercept` (and it's an object). And `coef` turns into `coef`.
9090

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**
9292

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.
9494

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.
9696

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.
9998

10099
#### python
101100

@@ -118,10 +117,6 @@ import { LogisticRegression } from 'scikitjs'
118117
let X = [[1], [-1]]
119118
let y = [1, 0]
120119
let lr = new LogisticRegression({ fitIntercept: false })
121-
await lr.fitAsync(X, y)
120+
await lr.fit(X, y)
122121
console.log(lr.coef)
123122
```
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

Comments
 (0)