鱼C论坛

 找回密码
 立即注册
查看: 1701|回复: 1

[学习笔记] 特征选择--PCA与互信息的使用与比较

[复制链接]
发表于 2021-12-16 17:53:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Handsome_zhou 于 2021-12-16 18:31 编辑
from sklearn.datasets import load_boston
import pandas as pd
import numpy as np
d = load_boston()
x = d.data
y = d.target
print(x[:10])
print('形状:', x.shape)

from sklearn.decomposition import PCA
pca = PCA(n_components=10)
x = pca.fit_transform(x)

%time
x = pd.DataFrame(d.data)
y = pd.DataFrame(d.target)
x = np.matrix(x)
y = np.matrix(y)

from sklearn.preprocessing import StandardScaler #标准化
scaler = StandardScaler()
X = scaler.fit_transform(X)
y = scaler.fit_transform(y)

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2)

from sklearn.svm import SVR
svr = SVR(gamma = 'scale')

svr.fit(X_train,y_train)
train_score = svr.score(X_train,y_train)
test_score = svr.score(X_test,y_test)
print('train score: {} ; test score: {}'.format(train_score,test_score))
结果:
01.jpg


from sklearn import datasets
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import mutual_info_regression
import pandas as pd
import numpy as np
from sklearn.datasets import load_boston
d = load_boston()

x = pd.DataFrame(d.data)
y = pd.DataFrame(d.target)
x = np.matrix(x)
y = np.matrix(y)
mi = mutual_info_regression(x,y)
X = SelectKBest(mutual_info_regression, k=10).fit_transform(x,y)

from sklearn.preprocessing import StandardScaler #标准化
scaler = StandardScaler()
X = scaler.fit_transform(X)
y = scaler.fit_transform(y)

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2)

from sklearn.svm import SVR
svr = SVR(gamma = 'scale')

svr.fit(X_train,y_train)
train_score = svr.score(X_train,y_train)
test_score = svr.score(X_test,y_test)
print('train score: {} ; test score: {}'.format(train_score,test_score))
结果:
02.png

在用boston数据集得到的结果来看,PCA和互信息两种特征选择方法得到的数据在支持向量机下并没有明显的性能差别。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-17 17:36:48 | 显示全部楼层
学习

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-29 09:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表