# -*- coding: utf-8 -*-
from __future__ import division
from numpy import*
from xlwt import Workbook, Formula
import xlrd
import pandas as pd
import copy
import matplotlib.pyplot as plt
def regularization(z):
# 该函数将z正则化,并将其各维均值存入re【0】,各维标准差存入re【1】,输出标准化后矩阵和re矩阵
a1=len(z)
ag=len(z[0])
pc=[]
re=[[] for i in range(2)]
i = 0
while i<a1:
if linalg.norm(z[i])==0:
pc.append(i)
i+=1
u=0
z0=z[:]
for i in pc:
i=i-u
z0.pop(i)
u+=1
a1 = len(z0)
z1 = [[0]*ag for i in range(a1)]
i=0
while i < a1:
j = 0
s1 = sum(z0[i])
s2 = len(z0[i])
s3 = 0
while j < ag:
s3 = s3 + (z0[i][j] - s1 / s2) * (z0[i][j] - s1 / s2)
j += 1
s3 = pow(s3 / (s2 - 1), 0.5)
re[0].append(s1 / s2)
re[1].append(s3)
j = 0
while j < ag:
z1[i][j] = (z0[i][j] - s1 / s2) / s3
j += 1
i += 1
return [z1, i, re]
def yz(zx0,zy0,list,ix):
# 该函数可以根据zx0,zy0根据PLS理论求出对应的t r u等向量
l0 = len(zx0[0])
l10 = len(zx0)
l2 = len(zy0)
l3=len(list)
l5=len(list[0])
zx=[[] for i in range(l10)]
zy = [[] for i in range(l2)]
dss=[[] for i in range(l2)]
diss = [[] for i in range(l2)]
i=0
while i<l3:
if i!=ix:
for j in list[i]:
k=0
while k<l10:
zx[k].append(zx0[k][j])
k+=1
k=0
while k<l2:
zy[k].append(zy0[k][j])
k+=1
i+=1
zx1=mat(zx)
zy1 = mat(zy)
ej0 = zx1 * zy1.T * zy1 * zx1.T
ej2 = zy1 * zx1.T * zx1 * zy1.T
ej1 = array(ej0)
ej3 = array(ej2)
#求出特征向量和特征值
nmtf, l01f = linalg.eig((ej1+ej1.T)/2)
nmt1f, c0f = linalg.eig((ej3+ej3.T)/2)
nmt = nmtf.real
l01 = l01f.real
nmt1 = nmt1f.real
c0 = c0f.real
a0 = len(nmt)
a1 = len(nmt1)
i = 0
m0 = nmt[0].real
m1 = nmt1[0].real
n0 = 0
n1 = 0
while i < a0:
if m0 < nmt[i].real:
m0 = nmt[i].real
n0 = i
i += 1
i = 0
while i < a1:
if m1 < nmt1[i].real:
m1 = nmt1[i].real
n1 = i
i += 1
i = 0
#选出最大特征值计算对应的t,r,u
zx2 = array(zx1)
l11=array(l01)
l1 = l11[:,n0]/linalg.norm(l11[:,n0])
t = dot(zx2.T, l1)
t1 = t.tolist()
zy2 = array(zy1)
c11=array(c0)
c1 = c11[:,n1]/linalg.norm(c11[:,n1])
u = dot(zy2.T, c1)
u1 = u.tolist()
p = dot(zx2, t) / pow(linalg.norm(t), 2)
r = dot(zy2, t) / pow(linalg.norm(t), 2)
p1=p.tolist()
r1=r.tolist()
zcy3=[[] for i in range(l2)]
i=0
while i<l5:
j=0
while j<l2:
zcy3[j].append(t1[i]*r1[j])
dss[j].append(pow(zcy3[j][i]-zy[j][i],2))
diss[j].append(0)
j+=1
i+=1
li0=l1.tolist()
return [dss,t1,u1,p1,r1,li0,diss]
def yzh(zx0,zy0,list):
# 该函数可以根据zx0,zy0根据PLS理论求出不同主成分数对应的误差和
l0 = len(zx0[0])
l10 = len(zx0)
l2 = len(zy0)
l3=len(list)
l5=len(list[0])
nz=0
ssn=[[] for i in range(l3)]
#根据交叉验证法计算不同主成分数误差和
while nz<l3:
zx = [[] for i in range(l10)]
zy = [[] for i in range(l2)]
tz=[]
uz=[]
pz=[]
rz=[]
i=0
while i<l3:
if i!=nz:
for j in list[i]:
k=0
while k<l10:
zx[k].append(zx0[k][j])
k+=1
k=0
while k<l2:
zy[k].append(zy0[k][j])
k+=1
i+=1
zcx = [[] for i in range(l10)]
zcy = [[] for i in range(l2)]
for i in list[nz]:
j = 0
while j < l10:
zcx[j].append(zx0[j][i])
j += 1
j = 0
while j < l2:
zcy[j].append(zy0[j][i])
j += 1
h=0
zxi= mat(zx)
zyi = mat(zy)
zx1 = mat(zx)
zy1 = mat(zy)
while h<l10:
ej0 = zx1 * zy1.T * zy1 * zx1.T
ej2 = zy1 * zx1.T * zx1 * zy1.T
ej1 = array(ej0)
ej3 = array(ej2)
#求出特征向量和特征值
nmtf, l01f = linalg.eig((ej1+ej1.T)/2)
nmt1f, c0f = linalg.eig((ej3+ej3.T)/2)
nmt = nmtf.real
l01 = l01f.real
nmt1 = nmt1f.real
c0 = c0f.real
a0 = len(nmt)
a1 = len(nmt1)
i = 0
m0 = nmt[0].real
m1 = nmt1[0].real
n0 = 0
n1 = 0
while i < a0:
if m0 < nmt[i].real:
m0 = nmt[i].real
n0 = i
i += 1
i = 0
while i < a1:
if m1 < nmt1[i].real:
m1 = nmt1[i].real
n1 = i
i += 1
i = 0
#选出最大特征值计算对应的t,r,u
zx2 = array(zx1)
l11=array(l01)
l1 = l11[:,n0]/linalg.norm(l11[:,n0])
t = dot(zx2.T, l1)
t1 = t.tolist()
zy2 = array(zy1)
c11=array(c0)
c1 = c11[:,n1]/linalg.norm(c11[:,n1])
u = dot(zy2.T, c1)
u1 = u.tolist()
p = dot(zx2, t) / pow(linalg.norm(t), 2)
r = dot(zy2, t) / pow(linalg.norm(t), 2)
p1=p.tolist()
r1=r.tolist()
tz.append(t1)
uz.append(u1)
pz.append(p1)
rz.append(r1)
tz2=mat(tz)
uz2=mat(uz)
pz2=mat(pz)
rz2=mat(rz)
zcx1=mat(zcx)
zcy1=mat(zcy)
zcyc=zcx1.T*zxi*uz2.T*(tz2*zxi.T*zxi*uz2.T).I*tz2*zyi.T
dyc=zcyc.T-zcy1
dyc0=dyc.tolist()
dyc01=[[] for i in range(l2)]
i=0
while i<l2:
j=0
while j<len(dyc0[0]):
dyc01[i].append(pow(dyc0[i][j],2))
j+=1
i+=1
ssi=sum(sum(dyc01))
zx1=(zxi.T-tz2.T*pz2).T
zy1=(zyi.T-tz2.T*rz2).T
ssn[nz].append(ssi)
h+=1
nz+=1
return ssn
def yz2(zx0,list,ix,hi):
# 该函数可以根据zx0,zy0根据PCA理论求出对应的t l等向量
l0 = len(zx0[0])
l10 = len(zx0)
l3=len(list)
l5=len(list[0])
zx=[[] for i in range(l10)]
i=0
while i<l3:
if i!=ix:
for j in list[i]:
k=0
while k<l10:
zx[k].append(zx0[k][j])
k+=1
i+=1
zx1=mat(zx)
nn=eye(l10)
nn1=nn/(l5-1)
nn2=mat(nn1)
ej0 =nn2* zx1* zx1.T
ej1 = array(ej0)
# 求出特征值和特征向量
nmtf, l01f = linalg.eig((ej1+ej1.T)/2)
nmt0 = nmtf.real
nmt=nmt0.tolist()
l01 = l01f.real
a0 = len(nmt)
i = 0
m0 = nmt[0].real
n0 = 0
n1 = 0
hj=0
hs=sum(nmt)
if hi==1:
nmt2=copy.deepcopy(nmt)
i=0
hsi=0
while i<l5:
nmax=max(nmt2)
hsi=hsi+nmax
nmt2.remove(nmax)
a=hsi/hs
if a>=0.85:
评论1