Beispiel #1
0
class PCR:
    def __init__(self, X, Y):
        self.X = X
        self.Y = Y

    # 对X进行SVD分解,返回分解后的矩阵,可以使用compare,和cum来确定独立分组数目
    def confirmPCs(self):
        self.pca = PCA(self.X)
        compare, cum = self.pca.SVDdecompose()
        return compare, cum

    # PCs为独立分组数
    def fit(self, PCs):
        T, P = self.pca.PCAdecompose(PCs)
        self.P = P
        self.T = T
        self.mlr = MLR(T, self.Y, False)
        self.mlr.fit()
        self.A = self.mlr.getCoef()

    def predict(self, Xnew):
        T = np.dot(Xnew, self.P)
        ans = self.mlr.predict(T)
        return ans

    def fTest(self, arfa):
        return self.mlr.Ftest(arfa)
class PCR:
    def __init__(self, X, Y):
        self.X = X
        self.Y = Y

    def confirmPCs(self):
        self.pca = PCA(self.X)
        compare = self.pca.SVDdecompose()
        return compare

    def fit(self, PCs):
        T, P = self.pca.PCAdecompose(PCs)
        print('T=', T)
        self.P = P
        oneCol = np.ones(T.shape[0])
        T = np.c_[oneCol, T]
        self.mlr = MLR(T, self.Y)
        self.mlr.fit()
        self.A = self.mlr.getCoef()

    def predict(self, Xnew):
        T = np.dot(Xnew, self.P)
        oneCol = np.ones(T.shape[0])
        T = np.c_[oneCol, T]
        ans = self.mlr.predict(T)
        return ans

    def fTest(self, arfa):
        return self.mlr.Ftest(arfa)
Beispiel #3
0
 def model(self, k):
     pca = PCA(self.X)
     U, S, V, compare = pca.SVDdecompose()  # 不可去,会用到计算得到的结果
     # 得到得分矩阵和载荷矩阵
     T, P = pca.PCAdecompose(k)
     #print("得分矩阵T: ", T)
     #print("载荷矩阵P: ", P)
     mlr = MLR(T, self.Y)
     mlr.modelling()
     self.A = np.dot(P, mlr.A)
Beispiel #4
0
import numpy as np
import matplotlib.pyplot as plt

A = np.loadtxt(r".\wheat_train_PCA_X.txt")
B = np.loadtxt(r".\wheat_train_PCA_Y.txt")

from PCA import PCA
aver = A.mean(axis=0)
std = A.std(axis=0)
X = (A - aver) / std
pca = PCA(X)
print(pca.SVDdecompose())
T, P = pca.PCAdecompose(6)

cls1 = B == 1.0
cls2 = B != 1.0

plt.subplot(1, 2, 1)
plt.plot(T[cls1, 0], T[cls1, 1], 'ro')
plt.plot(T[cls2, 0], T[cls2, 1], 'b^')

#PLSR
from sklearn.cross_decomposition import PLSRegression
pls = PLSRegression(n_components=3, scale=True)
pls.fit(A, B)

T = pls.x_scores_

plt.subplot(1, 2, 2)
plt.plot(T[cls1, 0], T[cls1, 1], 'ro')
plt.plot(T[cls2, 0], T[cls2, 1], 'b^')