Skip to content

Commit d7235c7

Browse files
committed
Added new example image for README.MD
1 parent c451bba commit d7235c7

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Loading
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
namespace NumSharp.UnitTest.Others
2+
{
3+
public class LinearRegression {
4+
private float alpha;
5+
private int n_iter;
6+
private int n_samples;
7+
private int n_features;
8+
private NDArray X;
9+
private NDArray y;
10+
private NDArray @params;
11+
private NDArray coef_;
12+
private float? intercept_;
13+
14+
public LinearRegression(NDArray X, NDArray y, float alpha = 0.03f, int n_iter = 1500) {
15+
this.alpha = alpha;
16+
this.n_iter = n_iter;
17+
this.n_samples = y.size;
18+
this.n_features = np.size(X, 1);
19+
this.X = np.hstack(np.ones(this.n_samples, 1),
20+
(X - np.mean(X, 0) / np.std(X, 0)));
21+
this.y = np.expand_dims(y, -1);
22+
this.@params = np.zeros((this.n_features + 1, 1), NPTypeCode.Single);
23+
this.coef_ = null;
24+
this.intercept_ = null;
25+
}
26+
27+
public LinearRegression fit() {
28+
for (int i = 0; i < this.n_iter; i++) {
29+
this.@params = this.@params - (this.alpha / this.n_samples) *
30+
np.matmul(this.X.T, np.matmul(this.X, this.@params) - this.y);
31+
}
32+
33+
this.intercept_ = @params.GetSingle(0);
34+
this.coef_ = @params["1:"];
35+
36+
return this;
37+
}
38+
39+
public float score(NDArray X = null, NDArray y = null) {
40+
if (X is null)
41+
X = this.X;
42+
else {
43+
n_samples = np.size(X, 0);
44+
this.X = np.hstack(np.ones(this.n_samples, 1),
45+
(X - np.mean(X, 0) / np.std(X, 0)));
46+
}
47+
48+
if (y is null) {
49+
y = this.y;
50+
} else
51+
y = np.expand_dims(y, -1);
52+
53+
var y_pred = np.matmul(X, @params);
54+
var score = 1 - ((np.power((y - y_pred), 2)).sum() / (np.power(y - y.mean(), 2)).sum());
55+
56+
return score;
57+
}
58+
59+
public NDArray predict(NDArray X) {
60+
n_samples = np.size(X, 0);
61+
y = np.matmul(
62+
np.hstack(np.ones(this.n_samples, 1), (X - np.mean(X, 0) / np.std(X, 0))),
63+
@params
64+
);
65+
66+
return y;
67+
}
68+
69+
public NDArray get_params()
70+
=> @params;
71+
}
72+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import numpy as np
2+
from sklearn.datasets import load_boston
3+
import matplotlib.pyplot as plt
4+
class LinearRegression():
5+
def __init__(self, X: np.ndarray, y, alpha=0.03, n_iter=1500):
6+
self.alpha = alpha
7+
self.n_iter = n_iter
8+
self.n_samples = len(y)
9+
self.n_features = np.size(X, 1)
10+
self.X = np.hstack((np.ones((self.n_samples, 1)),
11+
(X - np.mean(X, 0)) / np.std(X, 0)))
12+
self.y = np.expand_dims(y, -1)
13+
self.params = np.zeros((self.n_features + 1, 1))
14+
self.coef_ = None
15+
self.intercept_ = None
16+
17+
18+
def fit(self):
19+
for i in range(self.n_iter):
20+
self.params = self.params - (self.alpha/self.n_samples) * \
21+
self.X.T @ (self.X @ self.params - self.y)
22+
23+
24+
self.intercept_ = self.params[0]
25+
self.coef_ = self.params[1:]
26+
27+
return self
28+
29+
30+
31+
def score(self, X=None, y=None):
32+
if X is None:
33+
X = self.X
34+
else:
35+
n_samples = np.size(X, 0)
36+
X = np.hstack((np.ones((n_samples, 1)),
37+
(X - np.mean(X, 0)) / np.std(X, 0)))
38+
39+
40+
if y is None:
41+
y = self.y
42+
else:
43+
y = np.expand_dims(y, -1)
44+
45+
y_pred = X @ self.params
46+
score = 1 - (((y - y_pred)**2).sum() / ((y - y.mean())**2).sum())
47+
48+
return score
49+
50+
51+
52+
def predict(self, X):
53+
n_samples = np.size(X, 0)
54+
y = np.hstack((np.ones((n_samples, 1)), (X-np.mean(X, 0)) \
55+
/ np.std(X, 0))) @ self.params
56+
return y
57+
58+
59+
60+
61+
62+
def get_params(self):
63+
return self.params

0 commit comments

Comments
 (0)