Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create sklearn hello world example for Issue #9 #17

Open
wants to merge 1 commit into
base: GPU
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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