Skip to content

Commit 02ac923

Browse files
committed
Cleaning before handover 1
1 parent 71d6ebe commit 02ac923

File tree

7 files changed

+32
-36
lines changed

7 files changed

+32
-36
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ docs/_build/
5858

5959
# PyBuilder
6060
target/
61+
62+
.DS_Store

pyPLS/nopls.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ def __init__(self, X, Y, scaling=0,
7171
self.K = kernels[kernel](self.X, **kwargs)
7272
# Correction of the kernel matrix
7373
if penalization:
74-
75-
self.K = diagonal_correction(self.K, np.mean(self.Y, axis=1))
74+
self.K = self.K - np.diag(np.diag(self.K)) * penalization
75+
# self.K = diagonal_correction(self.K, np.mean(self.Y, axis=1))
7676

7777
assert not np.isnan(self.K).any(), "Kernel calculation lead to missing values!"
7878

@@ -116,7 +116,8 @@ def __init__(self, X, Y, scaling=0,
116116

117117
if statistics:
118118
#self.Yhat = self.predict(self.X, preprocessing=False, kernel=kernel)
119-
self.Yhat = self.T @ self.C.T
119+
# self.Yhat = self.T @ self.C.T
120+
self.Yhat = self.predict(self.X, preprocessing=False)
120121
self.R2Y, self.R2Ycol = self._calculateR2Y(self.Yhat)
121122
if kernel == "linear":
122123
self.R2X = np.sum(np.square(self.T @ self.P.T))/self.SSX

pyPLS/pls.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,41 +66,41 @@ def __init__(self, X, Y, ncp=1, cvfold=None, scaling=0):
6666

6767

6868
def predict(self, Xnew, preprocessing=True, statistics=False, **kwargs):
69-
70-
69+
70+
7171
Xnew, nnew, pxnew = isValid(Xnew, forPrediction=True)
7272
if preprocessing:
7373
Xnew = (Xnew - self.Xbar)
7474
Xnew /= np.power(self.Xstd, self.scaling)
75-
75+
7676
assert pxnew == self.px, "New observations do not have the same number of variables!!"
77-
77+
7878
if statistics:
7979
That = Xnew @ self.W
8080
Xpred = That @ self.P.T
8181
Xres = Xnew - Xpred
8282
Xnew2 = np.square(Xres)
83-
83+
8484
if np.isnan(Xnew2).any():
8585
ssrx = np.nansum(Xnew2, axis=0)
8686
else:
8787
ssrx = np.sum(Xnew2, axis=0)
8888
stats = {'That':That, 'ESS':ssrx}
89-
90-
89+
90+
9191
if self.B is not None:
9292
# Yhat = Xnew @ self.B
93-
93+
9494
if self.missingValuesInX:
9595
Yhat = nanmatprod(Xnew, self.B)
9696
else:
9797
Yhat = Xnew @ self.B
98-
98+
9999
if preprocessing:
100100
Yhat = Yhat * np.power(self.Ystd, self.scaling) + self.Ybar
101101
else:
102102
Yhat = None
103-
103+
104104
if statistics:
105105
return Yhat, stats
106106
else:

pyPLS/preprocessing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
def diagonal_correction(K, v):
5+
56
n = K.shape[0]
67
correction = np.zeros((n, n))
78
H = (np.eye(n) - np.ones((n, n), dtype=float)/n)

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
numpy
2+
scipy
3+
pandas

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
setup(
44
name='pyPLS',
5-
version='0.1.1',
5+
version='0.2.0',
66
packages=find_packages(),
77
url='',
88
license='MIT',
99
author='Olivier Cloarec',
10-
author_email='ocloarec@korrigan.co.uk',
10+
author_email='ocloarec@mac.com',
1111
description='A package for Projection on Latent Structures',
1212
requires=['numpy', 'pandas', 'scipy']
1313
)

test.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,25 @@
55

66

77
def test_pls():
8-
out = pyPLS.nopls(Xt, Yt[:, 0], cvfold=7, scaling=1., penalization=True)
9-
print("noPLS1 R2 = ", out.R2Y, out.ncp)
10-
print("noPLS1 Q2 = ", out.Q2Y, out.ncp)
8+
#out = pyPLS.nopls(Xt, Yt[:, 0], cvfold=7, scaling=1., penalization=True)
9+
# print("noPLS1 R2 = ", out.R2Y, out.ncp)
10+
# print("noPLS1 Q2 = ", out.Q2Y, out.ncp)
1111
out = pyPLS.pls(Xt, Yt[:, 0], ncp=2, scaling=1., cvfold=7)
1212
print("PLS1 R2 = ", out.R2Y, out.ncp)
1313
print("PLS1 Q2 = ", out.Q2Y, out.ncp)
14-
out = pyPLS.nopls(Xt, Yt[:, 0:2], cvfold=7, scaling=1., penalization=True)
15-
print("noPLS2 R2 = ", out.R2Ycol[0], out.R2Ycol[1], out.ncp)
16-
print("noPLS2 Q2 = ", out.Q2Ycol[0], out.Q2Ycol[1], out.ncp)
14+
# out = pyPLS.nopls(Xt, Yt[:, 0:2], cvfold=7, scaling=1., penalization=True)
15+
# print("noPLS2 R2 = ", out.R2Ycol[0], out.R2Ycol[1], out.ncp)
16+
# print("noPLS2 Q2 = ", out.Q2Ycol[0], out.Q2Ycol[1], out.ncp)
1717
out = pyPLS.pls(Xt, Yt[:, 0:2], ncp=2, cvfold=7, scaling=1.)
1818
print("PLS2 R2 = ", out.R2Ycol[0], out.R2Ycol[1], out.ncp)
1919
print("PLS2 Q2 = ", out.Q2Ycol[0], out.Q2Ycol[1], out.ncp)
2020

2121

22-
def test_kernel():
23-
start_time = time.time()
24-
K2 = pyPLS.linear(Xt, Y=Xt)
25-
print("Linear Kernel --- %s seconds ---" % (time.time() - start_time))
26-
start_time = time.time()
27-
K2 = pyPLS.gaussian(Xt, sigma=3.0)
28-
print("Gaussian Kernel --- %s seconds ---" % (time.time() - start_time))
29-
3022
def test_pca():
3123
start_time = time.time()
32-
# out = pyPLS.pca(Xt,4)
33-
# print("PCA using svd on the kernel --- %s seconds ---" % (time.time() - start_time))
34-
# start_time = time.time()
24+
out = pyPLS.pca(Xt,4)
25+
print("PCA using svd on the kernel --- %s seconds ---" % (time.time() - start_time))
26+
start_time = time.time()
3527
out = pyPLS.pca(Xt, 4, method='nipals')
3628
print("PCA using nipals --- %s seconds ---" % (time.time() - start_time))
3729
start_time = time.time()
@@ -42,20 +34,17 @@ def prediction():
4234

4335
out = pyPLS.pls(Xt, Yt[:, 0], ncp=2, scaling=1., cvfold=7)
4436
Yhat, stats = out.predict(Xt, statistics=True)
45-
print(stats['ESS'])
4637

4738
out = pyPLS.pca(Xt, 3, scaling=1)
4839
That = out.predict(Xt)
49-
print(That)
40+
5041

5142
if __name__ == '__main__':
5243
Xt, Z, Yt = pyPLS.simulateData(600, 2, 30, 5., signalToNoise=100.)
5344
# for i in np.arange(50):
5445
# Xt[i,i] = np.NaN
5546
if "nopls" in sys.argv[1:]:
5647
test_pls()
57-
if "kernel" in sys.argv[1:]:
58-
test_kernel()
5948
if "pca" in sys.argv[1:]:
6049
test_pca()
6150
if "prediction" in sys.argv[1:]:

0 commit comments

Comments
 (0)