代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
# @Time : 2020/12/12 17:10
# @Author : my_name_is_BUG
# @FileName: CARS.py
# @Software: PyCharm
# @Cnblogs :https://blog.csdn.net/qq2512446791
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager as fm, rcParams
from sklearn.cross_decomposition import PLSRegression
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
import copy
def PC_Cross_Validation(X, y, pc, cv):
'''
x :光谱矩阵 nxm
y :浓度阵 (化学值)
pc:最大主成分数
cv:交叉验证数量
return :
RMSECV:各主成分数对应的RMSECV
PRESS :各主成分数对应的PRESS
rindex:最佳主成分数
'''
kf = KFold(n_splits=cv)
RMSECV = []
for i in range(pc):
RMSE = []
for train_index, test_index in kf.split(X):
x_train, x_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
pls = PLSRegression(n_components=i + 1)
pls.fit(x_train, y_train)
y_predict = pls.predict(x_test)
RMSE.append(np.sqrt(mean_squared_error(y_test, y_predict)))
RMSE_mean = np.mean(RMSE)
RMSECV.append(RMSE_mean)
rindex = np.argmin(RMSECV)
return RMSECV, rindex
def Cross_Validation(X, y, pc, cv):
'''
x :光谱矩阵 nxm
y :浓度阵 (化学值)
pc:最大主成分数
cv:交叉验证数量
return :
RMSECV:各主成分数对应的RMSECV
'''
kf = KFold(n_splits=cv)
RMSE = []
for train_index, test_index in kf.split(X):
x_train, x_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
pls = PLSRegression(n_components=pc)
pls.fit(x_train, y_train)
y_predict = pls.predict(x_test)
RMSE.append(np.sqrt(mean_squared_error(y_test, y_predict)))
RMSE_mean = np.mean(RMSE)
return RMSE_mean
def CARS_Cloud(X, y, N=50, f=20, cv=10):
p = 0.8
m, n = X.shape
u = np.power((n/2), (1/(N-1)))
k = (1/(N-1)) * np.log(n/2)
cal_num = np.round(m * p)
# val_num = m - cal_num
b2 = np.arange(n)
x = copy.deepcopy(X)
D = np.vstack((np.array(b2).reshape(1, -1), X))
WaveData = []
# Coeff = []
WaveNum =[]
RMSECV = []
r = []
for i in range(1, N+1):
r.append(u*np.exp(-1*k*i))
wave_num = int(np.round(r[i-1]*n))
WaveNum = np.hstack((WaveNum, wave_num))
cal_index = np.random.choice \
(np.arange(m), size=int(cal_num), replace=False)
wave_index = b2[:wave_num].reshape(1, -1)[0]
xcal = x[np.ix_(list(cal_index), list(wave_index))]
#xcal = xcal[:,wave_index].reshape(-1,wave_num)
ycal = y[cal_index]
x = x[:, wave_index]
D = D[:, wave_index]
d = D[0, :].reshape(1,-1)
wnum = n - wave_num
if wnum > 0:
d = np.hstack((d, np.full((1, wnum), -1)))
if len(WaveData) == 0:
WaveData = d
else:
WaveData = np.vstack((WaveData, d.reshape(1, -1)))
if wave_num < f:
f = wave_num
pls = PLSRegression(n_components=f)
pls.fit(xcal, ycal)
beta = pls.coef_
b = np.abs(beta)
b2 = np.argsort(-b, axis=0)
coef = copy.deepcopy(beta)
coeff = coef[b2, :].reshape(len(b2), -1)
# cb = coeff[:wave_num]
#
# if wnum > 0:
# cb = np.vstack((cb, np.full((wnum, 1), -1)))
# if len(Coeff) == 0:
# Coeff = copy.deepcopy(cb)
# else:
# Coeff = np.hstack((Coeff, cb))
rmsecv, rindex = PC_Cross_Validation(xcal, ycal, f, cv)
RMSECV.append(Cross_Validation(xcal, ycal, rindex+1, cv))
# CoeffData = Coeff.T
WAVE = []
# COEFF = []
for i in range(WaveData.shape[0]):
wd = WaveData[i, :]
# cd = CoeffData[i, :]
WD = np.ones((len(wd)))
# CO = np.ones((len(wd)))
for j in range(len(wd)):
ind = np.where(wd == j)
if len(ind[0]) == 0:
WD[j] = 0
# CO[j] = 0
else:
WD[j] = wd[ind[0]]
# CO[j] = cd[ind[0]]
if len(WAVE) == 0:
WAVE = copy.deepcopy(WD)
else:
WAVE = np.vstack((WAVE, WD.reshape(1, -1)))
# if len(COEFF) == 0:
# COEFF = copy.deepcopy(CO)
# else:
# COEFF = np.vstack((WAVE, CO.reshape(1, -1)))
MinIndex = np.argmin(RMSECV)
Optimal = WAVE[MinIndex, :]
boindex = np.where(Optimal != 0)
OptWave = boindex[0]
fig = plt.figure()
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
fonts = 16
plt.subplot(211)
plt.xlabel('蒙特卡洛迭代次数', fontsize=fonts)
plt.ylabel('被选择的波长数量', fontsize=fonts)
plt.title('最佳迭代次数:' + str(MinIndex) + '次', fontsize=fonts)
plt.plot(np.arange(N), WaveNum)
plt.subplot(212)
plt.xlabel('蒙特卡洛迭代次数', fontsize=fonts)
plt.ylabel('RMSECV', fontsize=fonts)
plt.plot(np.arange(N), RMSECV)
# # plt.subplot(313)
# # plt.xlabel('蒙特卡洛迭代次数', fontsize=fonts)
# # plt.ylabel('各变量系数值', fontsize=fonts)
# # plt.plot(COEFF)
# #plt.vlines(MinIndex, -1e3, 1e3, colors='r')
plt.show()
return OptWave
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。