-
Notifications
You must be signed in to change notification settings - Fork 20
/
feature_engineer.py
90 lines (77 loc) · 2.77 KB
/
feature_engineer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 14 19:36:29 2018
@author: Administrator
#乳腺癌数据测试中,卡方检验效果不如随机森林,卡方筛选的2个最好因子在随机森林中权重不是最大
"""
from sklearn import preprocessing
from sklearn.preprocessing import Imputer
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.svm import LinearSVC
from sklearn.ensemble import RandomForestClassifier
trees=100
#读取文件
readFileName="data1.xlsx"
#读取excel
df=pd.read_excel(readFileName)
list_columns=list(df.columns[:-1])
x=df.ix[:,:-1]
y=df.ix[:,-1]
#把dataframe 格式转换为阵列
X=np.array(x)
#数据预处理
imp = Imputer(missing_values='NaN', strategy='mean', axis=0)
imp.fit(X)
X=imp.transform(X)
#X_dummy=pd.get_dummies(X)
#特征工程-卡方筛选K个最佳特征
#乳腺癌数据测试中,卡方检验效果不如随机森林,卡方筛选的2个最好因子在随机森林中权重不是最大
#we can perform a \chi^2 test to the samples to retrieve only the two best features as follows:
X_chi2 = SelectKBest(chi2, k=2).fit_transform(X, y)
print("X_chi2 shapes:",X_chi2.shape)
##特征工程-支持向量l1
#根据模型选择最佳特征,线性svc选出5个最佳特征
lsvc = LinearSVC(C=0.01, penalty="l1", dual=False).fit(X, y)
model = SelectFromModel(lsvc, prefit=True)
X_lsvc1 = model.transform(X)
print("LinearSVC l1 shapes:",X_lsvc1.shape)
#特征工程-支持向量l2
lsvc = LinearSVC(C=0.01, penalty="l2", dual=False).fit(X, y)
model = SelectFromModel(lsvc, prefit=True)
X_lsvc2 = model.transform(X)
print("LinearSVC l2 shapes:",X_lsvc2.shape)
#特征工程-forest of trees 随机森林
trees = ExtraTreesClassifier(n_estimators=10000)
trees = trees.fit(X, y)
trees.feature_importances_
model = SelectFromModel(trees, prefit=True)
X_trees = model.transform(X)
print("forest trees shapes:",X_trees.shape)
'''
测试结果随机森林特征工程效果最好,其次是支持向量l2模式
X_chi2 shapes: (569, 2)
LinearSVC l1 shapes: (569, 5)
LinearSVC l2 shapes: (569, 9)
forest trees shapes: (569, 10)
'''
#主成分分析
pca =PCA(n_components=0.98)
#pca= PCA(n_components='mle')
#scaled_x=preprocessing.scale(X_digits)
# Plot the PCA spectrum
#pca.fit(scaled_x)
pca.fit(X)
print("PCA analysis:")
print (pca.explained_variance_ratio_)
print (pca.explained_variance_)
print (pca.n_components_)
#计算协方差
pca.get_covariance()
#Estimated precision of data.计算数据估计的准确性
pca.get_precision()