机器学习—降维-特征选择6-1(过滤法)

使用过滤法对糖尿病数据集降维

主要步骤流程:

  • 1. 导入包
  • 2. 导入数据集
  • 3. 数据预处理
    • 3.1 检测缺失值
    • 3.2 生成自变量和因变量
  • 4. 使用不同的统计指标做特征选择
    • 4.1 使用 方差 指标
    • 4.2 使用 卡方检验 指标
  • 5. 构建逻辑回归模型并评估模型性能
    • 5.1 使用原始数据构建
      • 5.1.1 数据预处理
      • 5.1.2 构建模型
    • 5.2 使用特征选择(方差法)后的数据构建
      • 5.2.1 数据预处理
      • 5.2.2 构建模型
    • 5.3 使用特征选择(卡方检验)后的数据构建
      • 5.3.1 数据预处理
      • 5.3.2 构建模型
 

1. 导入包

In [1]:
# 导入包
import numpy as np
import pandas as pd

 

2. 导入数据集

In [2]:
# 导入数据集
dataset = pd.read_csv('pima-indians-diabetes.csv')
dataset
Out[2]:
 pregplaspresskintestmasspediageclass
0 6 148 72 35 0 33.6 0.627 50 1
1 1 85 66 29 0 26.6 0.351 31 0
2 8 183 64 0 0 23.3 0.672 32 1
3 1 89 66 23 94 28.1 0.167 21 0
4 0 137 40 35 168 43.1 2.288 33 1
... ... ... ... ... ... ... ... ... ...
763 10 101 76 48 180 32.9 0.171 63 0
764 2 122 70 27 0 36.8 0.340 27 0
765 5 121 72 23 112 26.2 0.245 30 0
766 1 126 60 0 0 30.1 0.349 47 1
767 1 93 70 31 0 30.4 0.315 23 0

768 rows × 9 columns

 

3. 数据预处理

3.1 检测缺失值

In [3]:
# 检测缺失值
null_df = dataset.isnull().sum()
null_df
Out[3]:
preg     0
plas     0
pres     0
skin     0
test     0
mass     0
pedi     0
age      0
class    0
dtype: int64

3.2 生成自变量和因变量

In [4]:
# 生成自变量和因变量
X = dataset.iloc[:,0:8].values
y = dataset.iloc[:,8].values

 

4. 使用不同的统计指标做特征选择

4.1 使用 方差 指标

In [6]:
# 使用不同的统计指标做特征选择
# 特征选择(方差法)
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
test = SelectKBest(score_func=f_classif, k=4) # 只选择4个特征
fit = test.fit(X, y)
In [8]:
# 得到每个特征的分数
print(fit.scores_)
[ 39.67022739 213.16175218   3.2569504    4.30438091  13.28110753
  71.7720721   23.8713002   46.14061124]
找 scores_ 值大的字段。前4个字段依次为:1 (plas), 5 (mass), 7 (age), 0 (preq)
In [7]:
# 得到筛选后的特征
features1 = fit.transform(X)
features1
Out[7]:
array([[  6. , 148. ,  33.6,  50. ],
       [  1. ,  85. ,  26.6,  31. ],
       [  8. , 183. ,  23.3,  32. ],
       ...,
       [  5. , 121. ,  26.2,  30. ],
       [  1. , 126. ,  30.1,  47. ],
       [  1. ,  93. ,  30.4,  23. ]])
 
features1 存储着特征选择后的特征。后面构建模型时,自变量变为 features1,而不是X。

4.2 使用 卡方检验 指标

In [9]:
# 特征选择(卡方检验)
from sklearn.feature_selection import chi2
test = SelectKBest(score_func=chi2, k=4)
fit = test.fit(X, y)
In [10]:
# 得到每个特征的分数
print(fit.scores_)
[ 111.51969064 1411.88704064   17.60537322   53.10803984 2175.56527292
  127.66934333    5.39268155  181.30368904]
 
找 scores_ 值大的字段。前4个字段依次为:4 (test), 1 (plas), 7 (age), 5 (mass)。这和用 方差法选取的字段不一致
In [11]:
# 得到筛选后的特征
features2 = fit.transform(X)
features2
Out[11]:
array([[148. ,   0. ,  33.6,  50. ],
       [ 85. ,   0. ,  26.6,  31. ],
       [183. ,   0. ,  23.3,  32. ],
       ...,
       [121. , 112. ,  26.2,  30. ],
       [126. ,   0. ,  30.1,  47. ],
       [ 93. ,   0. ,  30.4,  23. ]])
 

5. 构建逻辑回归模型并评估模型性能

5.1 使用原始数据构建

5.1.1 数据预处理

In [12]:
# 数据预处理
# 拆分数据集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) # 拆分数据集
In [13]:
from sklearn.preprocessing import StandardScaler
sc = StandardScaler() # 特征缩放
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

5.1.2 构建模型

In [14]:
# 构建模型
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(penalty='l2', C=1, class_weight='balanced', random_state = 0)
classifier.fit(X_train, y_train)
Out[14]:
LogisticRegression(C=1, class_weight='balanced', random_state=0)
In [15]:
# 预测测试集
y_pred = classifier.predict(X_test)
In [16]:
# 评估模型性能
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, y_pred))
0.78125

5.2 使用特征选择(方差法)后的数据构建

5.2.1 数据预处理

In [17]:
# 数据预处理
# 拆分数据集
X_train, X_test, y_train, y_test = train_test_split(features1, y, test_size = 0.25, random_state = 0) # 拆分数据集
In [18]:
# 特征缩放
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

5.2.2 构建模型

In [19]:
# 构建模型
classifier = LogisticRegression(penalty='l2', C=1, class_weight='balanced', random_state = 0)
classifier.fit(X_train, y_train)
Out[19]:
LogisticRegression(C=1, class_weight='balanced', random_state=0)
In [20]:
# 预测测试集
y_pred = classifier.predict(X_test)
In [21]:
# 评估模型性能
print(accuracy_score(y_test, y_pred))
0.7447916666666666

5.3 使用特征选择(卡方检验)后的数据构建

5.3.1 数据预处理

In [22]:
# 数据预处理
# 拆分数据集
X_train, X_test, y_train, y_test = train_test_split(features2, y, test_size = 0.25, random_state = 0) # 拆分数据集
In [23]:
# 特征缩放
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

5.3.2 构建模型

In [24]:
# 构建模型
classifier = LogisticRegression(penalty='l2', C=1, class_weight='balanced', random_state = 0)
classifier.fit(X_train, y_train)
Out[24]:
LogisticRegression(C=1, class_weight='balanced', random_state=0)
In [25]:
# 预测测试集
y_pred = classifier.predict(X_test)
In [26]:
# 评估模型性能
print(accuracy_score(y_test, y_pred))
0.734375
 
结论:
  1. 降维不一定会带来模型性能的提升;
  2. 不同的降维算法,降维后的数据不同;

 

posted @   Theext  阅读(274)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示