Files
2026-02-25 09:42:51 +08:00

41 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
-*- 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
@LicenseApache-2.0 license
"""
from sklearn import linear_model
import numpy as np
def Lar(X, y, nums=40):
"""
使用 LARSLeast 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