Skip to content

Commit

Permalink
Create sklearn hello world example for Issue nogibjj#9
Browse files Browse the repository at this point in the history
  • Loading branch information
Vancool committed Nov 6, 2022
1 parent 7138e7d commit 1e35229
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
31 changes: 31 additions & 0 deletions mylib/sklearnlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
sk-learn hello world demo
"""
from sklearn import linear_model

def linearRegression(X, y, sample_weight=None):
"""
Lineaer Regresssion based on sklearn
Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
Training data.
y : array-like of shape (n_samples,) or (n_samples, n_targets)
Target values. Will be cast to X's dtype if necessary.
sample_weight : array-like of shape (n_samples,), default=None
Individual weights for each sample.
.. versionadded:: 0.17
parameter *sample_weight* support to LinearRegression.
Returns
-------
self : object
Fitted Estimator.
"""
reg = linear_model.LinearRegression()
reg.fit(X, y, sample_weight)
return reg
13 changes: 13 additions & 0 deletions test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
"""

from mylib.calculator import add
import numpy as np
from mylib.sklearnlib import linearRegression


def test_add():
assert add(1, 2) == 3


def test_sklearn_linear_reg():
x = np.arange(10)
k, b = 2, 1
y = k * x + b
X = x[:, np.newaxis]
reg = linearRegression(X, y)
assert abs(reg.coef_[0] - k) < 1e-6
assert abs(reg.intercept_ - b) < 1e-6

0 comments on commit 1e35229

Please sign in to comment.