当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
6 Star 56 Fork 19

root / 竞争性自适应重加权算法-CARS-python版
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
CARS.py 5.49 KB
一键复制 编辑 原始数据 按行查看 历史
root 提交于 2022-06-07 03:00 . update CARS.py.
# -*- 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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/aBugsLife/CARS.git
git@gitee.com:aBugsLife/CARS.git
aBugsLife
CARS
竞争性自适应重加权算法-CARS-python版
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891