初始提交
This commit is contained in:
41
classification_model/WaveSelect/Lar.py
Normal file
41
classification_model/WaveSelect/Lar.py
Normal file
@ -0,0 +1,41 @@
|
||||
"""
|
||||
-*- coding: utf-8 -*-
|
||||
@Time :2022/04/12 17:10
|
||||
@Author : Pengyou FU
|
||||
@blogs : https://blog.csdn.net/Echo_Code?spm=1000.2115.3001.5343
|
||||
@github : https://github.com/FuSiry/OpenSA
|
||||
@WeChat : Fu_siry
|
||||
@License:Apache-2.0 license
|
||||
"""
|
||||
|
||||
from sklearn import linear_model
|
||||
import numpy as np
|
||||
|
||||
def Lar(X, y, nums=40):
|
||||
"""
|
||||
使用 LARS(Least Angle Regression)选择重要的特征波长。
|
||||
|
||||
参数:
|
||||
X : np.ndarray,预测变量矩阵(输入数据)
|
||||
y : np.ndarray,标签(目标值)
|
||||
nums : int,选择的特征点数量,默认为 40
|
||||
|
||||
返回:
|
||||
np.ndarray,选择的特征波长索引
|
||||
"""
|
||||
# 初始化 LARS 模型
|
||||
Lars = linear_model.Lars()
|
||||
|
||||
# 拟合模型
|
||||
Lars.fit(X, y)
|
||||
|
||||
# 获取回归系数的绝对值,表示特征的重要性
|
||||
corflist = np.abs(Lars.coef_)
|
||||
|
||||
# 将系数转换为数组并按重要性排序,选择前 nums 个最重要的特征
|
||||
SpectrumList = np.argsort(corflist)[-nums:][::-1]
|
||||
|
||||
# 对选择的特征索引进行排序,保证顺序一致
|
||||
SpectrumList = np.sort(SpectrumList)
|
||||
|
||||
return SpectrumList
|
||||
Reference in New Issue
Block a user