Skip to content

Commit

Permalink
Add cross validation module to integrate all modules, including varia…
Browse files Browse the repository at this point in the history
…ble matrix pretreatment, PLS object, base module for implementing algorithm, plots of cross validation errors, score plots, S-plot and jack-knife interval for variable importance evaluations. Also, testing dataset is included.
  • Loading branch information
DongElkan committed Jun 29, 2020
1 parent 1887ea3 commit da99134
Show file tree
Hide file tree
Showing 7 changed files with 1,080 additions and 0 deletions.
57 changes: 57 additions & 0 deletions base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import numpy as np
import numpy.linalg as la
import typing


def nipals(x: np.ndarray, y: np.ndarray,
tol: float = 1e-10,
max_iter: int = 1000,
dot=np.dot) -> typing.Tuple:
"""
Non-linear Iterative Partial Least Squares
Parameters
----------
x: np.ndarray
Variable matrix with size n by p, where n number of samples,
p number of variables.
y: np.ndarray
Dependent variable with size n by 1.
tol: float
Tolerance for the convergence.
max_iter: int
Maximal number of iterations.
Returns
-------
w: np.ndarray
Weights with size p by 1.
u: np.ndarray
Y-scores with size n by 1.
c: float
Y-weight
t: np.ndarray
Scores with size n by 1
References
----------
[1] Wold S, et al. PLS-regression: a basic tool of chemometrics.
Chemometr Intell Lab Sys 2001, 58, 109–130.
[2] Bylesjo M, et al. Model Based Preprocessing and Background
Elimination: OSC, OPLS, and O2PLS. in Comprehensive Chemometrics.
"""
u = y
i = 0
d = tol * 10
while d > tol and i <= max_iter:
w = dot(u, x) / dot(u, u)
w /= la.norm(w)
t = dot(x, w)
c = dot(t, y) / dot(t, t)
u_new = y * c / (c * c)
d = la.norm(u_new - u) / la.norm(u_new)
u = u_new
i += 1

return w, u, c, t
Loading

0 comments on commit da99134

Please sign in to comment.