数据挖掘与分析——特征选择

  1. 数据读取

wine葡萄酒数据集是来自UCI的公开数据集,也scikit-learn库自带的数据集,它是对意大利同一地区种植的葡萄酒进行化学分析的结果,这些葡萄酒来自三个不同的品种。该分析确定了三种葡萄酒中每种葡萄酒中含有的13种成分的数量。

每行代表一种酒的样本,共有178个样本;一共有14列,其中,第一个属性是类标识符,分别是1/2/3来表示,代表葡萄酒的三个分类。后面的13列为每个样本的对应属性的样本值,分别是酒精、苹果酸、灰、灰分的碱度、镁、总酚、黄酮类化合物、非黄烷类酚类、原花色素、颜色强度、色调、稀释葡萄酒的OD280/OD315、脯氨酸。其中第1类有59个样本,第2类有71个样本,第3类有48个样本。

数据读取方法:

  1. 特征选择

单变量过滤法:

  1. 请使用方差阈值法对wine数据集进行特征选择,阈值设置为1。
python 复制代码
import pandas as pd
from sklearn.datasets import load_wine
from sklearn.feature_selection import VarianceThreshold
# 设置pandas的显示选项,使输出不被省略
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
# 读取wine数据集
wine = load_wine()
X = pd.DataFrame(wine.data, columns=wine.feature_names)
# 使用方差阈值法进行特征选择
selector = VarianceThreshold(threshold=1)
X_selected = selector.fit_transform(X)
# 显示选择后的特征数量
print("方差阈值法特征选择后的特征数量:", X_selected.shape[1])
print(X_selected)
  1. 请使用卡方统计量法对wine数据集进行特征选择,选择的特征子集的大小为5。
python 复制代码
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
import pandas as pd
from sklearn.datasets import load_wine
# 设置pandas的显示选项,使输出不被省略
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
# 读取wine数据集
wine = load_wine()
X = pd.DataFrame(wine.data, columns=wine.feature_names)
y = wine.target

# 使用卡方统计量法进行特征选择
selector = SelectKBest(chi2, k=5)
X_selected = selector.fit_transform(X, y)

# 显示选择后的特征数量
print("卡方统计量法特征选择后的特征数量:", X_selected.shape[1])
  1. 请使用互信息法wine数据集进行特征选择,选择的特征子集的大小为5。
python 复制代码
from sklearn.feature_selection import mutual_info_classif
import pandas as pd
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import SelectKBest
# 设置pandas的显示选项,使输出不被省略
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
# 读取wine数据集
wine = load_wine()
X = pd.DataFrame(wine.data, columns=wine.feature_names)
y = wine.target
# 使用互信息法进行特征选择
selector = SelectKBest(mutual_info_classif, k=5)
X_selected = selector.fit_transform(X, y)
# 显示选择后的特征数量
print("互信息法特征选择后的特征数量:", X_selected.shape[1])
print(X_selected.shape)
print(X_selected)

嵌入法:

  1. 对wine数据集进行标准规范化。
  1. 对标准化后的数据集进行训练集和测试集的划分。
  1. 使用L1正则化的逻辑回归模型进行嵌入法特征选择。
python 复制代码
from sklearn.linear_model import LogisticRegression
import pandas as pd
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 设置pandas的显示选项,使输出不被省略
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

# 读取wine数据集
wine = load_wine()
X = pd.DataFrame(wine.data, columns=wine.feature_names)
y = wine.target

# 对数据集进行标准规范化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)

# 使用L1正则化的逻辑回归模型进行嵌入法特征选择
model = LogisticRegression(penalty='l1', solver='liblinear')
model.fit(X_train, y_train)

# 输出选择的特征数量
print("L1正则化逻辑回归模型选择的特征数量:", sum(model.coef_[0] != 0))
  1. 嵌入式特征选择模型,选择的特征子集的大小为5。
  1. 嵌入式特征选择模型。
python 复制代码
from sklearn.feature_selection import SelectFromModel
from sklearn.linear_model import LogisticRegression
import pandas as pd
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 设置pandas的显示选项,使输出不被省略
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

# 读取wine数据集
wine = load_wine()
X = pd.DataFrame(wine.data, columns=wine.feature_names)
y = wine.target

# 对数据集进行标准规范化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)

model = LogisticRegression(penalty='l1', solver='liblinear')
model.fit(X_train, y_train)
# 使用嵌入式特征选择模型,选择的特征子集的大小为5
selector = SelectFromModel(model, max_features=5)
X_selected = selector.fit_transform(X_train, y_train)

# 显示选择后的特征数量
print("嵌入式特征选择模型选择的特征数量:", X_selected.shape[1])
print(X_selected.shape)
print(model)
相关推荐
小超同学你好10 分钟前
面向 LLM 的程序设计 6:Tool Calling 的完整生命周期——从定义、决策、执行到观测回注
人工智能·语言模型
2501_9449347323 分钟前
直播运营需要哪些数据分析能力?场观、停留、成交和投流怎么联动分析
数据挖掘·数据分析
智星云算力29 分钟前
本地GPU与租用GPU混合部署:混合算力架构搭建指南
人工智能·架构·gpu算力·智星云·gpu租用
jinanwuhuaguo30 分钟前
截止到4月8日,OpenClaw 2026年4月更新深度解读剖析:从“能力回归”到“信任内建”的范式跃迁
android·开发语言·人工智能·深度学习·kotlin
xiaozhazha_34 分钟前
效率提升80%:2026年AI CRM与ERP深度集成的架构设计与实现
人工智能
枫叶林FYL35 分钟前
【自然语言处理 NLP】7.2.2 安全性评估与Constitutional AI
人工智能·自然语言处理
AI人工智能+42 分钟前
基于高精度身份证OCR识别、炫彩活体检测及人脸比对技术的人脸核身系统,为通信行业数字化转型提供了坚实的安全底座
人工智能·计算机视觉·人脸识别·ocr·人脸核身
小敬爱吃饭1 小时前
Ragflow Docker部署及问题解决方案(界面为Welcome to nginx,ragflow上传文件失败,Docker中的ragflow-cpu-1一直重启)
人工智能·python·nginx·docker·语言模型·容器·数据挖掘
宸津-代码粉碎机1 小时前
Spring Boot 4.0虚拟线程实战调优技巧,最大化发挥并发优势
java·人工智能·spring boot·后端·python
老兵发新帖1 小时前
Hermes:比openclaw更好用的智能体?
人工智能