## 2.2 方法

Filter(过滤式)：主要探究特征本身特点、特征与特征和目标值之间关联

Embedded (嵌入式)：算法自动选择特征（特征与目标值之间的关联）

## 2.3 低方差特征过滤

### 2.3.1 API

sklearn.feature_selection.VarianceThreshold(threshold = 0.0)

Variance.fit_transform(X)

X:numpy array格式的数据[n_samples,n_features]

### 2.3.2 数据计算

`pe_ratio,pb_ratio,market_cap,return_on_asset_net_profit,du_return_on_equity,ev,earnings_per_share,revenue,total_expense`

```index,pe_ratio,pb_ratio,market_cap,return_on_asset_net_profit,du_return_on_equity,ev,earnings_per_share,revenue,total_expense,date,return
0,000001.XSHE,5.9572,1.1818,85252550922.0,0.8008,14.9403,1211444855670.0,2.01,20701401000.0,10882540000.0,2012-01-31,0.027657228229937388
1,000002.XSHE,7.0289,1.588,84113358168.0,1.6463,7.8656,300252061695.0,0.326,29308369223.2,23783476901.2,2012-01-31,0.08235182370820669
2,000008.XSHE,-262.7461,7.0003,517045520.0,-0.5678,-0.5943,770517752.56,-0.006,11679829.03,12030080.04,2012-01-31,0.09978900335112327
3,000060.XSHE,16.476,3.7146,19680455995.0,5.6036,14.617,28009159184.6,0.35,9189386877.65,7935542726.05,2012-01-31,0.12159482758620697
4,000069.XSHE,12.5878,2.5616,41727214853.0,2.8729,10.9097,81247380359.0,0.271,8951453490.28,7091397989.13,2012-01-31,-0.0026808154146886697```

#### 分析：

1、初始化VarianceThreshold,指定阀值方差

2、调用fit_transform

```def variance_demo():
"""
删除低方差特征——特征选择
:return: None
"""
print(data)
# 1、实例化一个转换器类
transfer = VarianceThreshold(threshold=1)
# 2、调用fit_transform
data = transfer.fit_transform(data.iloc[:, 1:10])
print("删除低方差特征的结果：
", data)
print("形状：
", data.shape)
return None```

```index  pe_ratio  pb_ratio    market_cap  \
0     000001.XSHE    5.9572    1.1818  8.525255e+10
1     000002.XSHE    7.0289    1.5880  8.411336e+10
...           ...       ...       ...           ...
2316  601958.XSHG   52.5408    2.4646  3.287910e+10
2317  601989.XSHG   14.2203    1.4103  5.911086e+10
return_on_asset_net_profit  du_return_on_equity            ev  \
0                         0.8008              14.9403  1.211445e+12
1                         1.6463               7.8656  3.002521e+11
...                          ...                  ...           ...
2316                      2.7444               2.9202  3.883803e+10
2317                      2.0383               8.6179  2.020661e+11
earnings_per_share       revenue  total_expense        date    return
0                 2.0100  2.070140e+10   1.088254e+10  2012-01-31  0.027657
1                 0.3260  2.930837e+10   2.378348e+10  2012-01-31  0.082352
2                -0.0060  1.167983e+07   1.203008e+07  2012-01-31  0.099789
...                  ...           ...            ...         ...       ...
2315              0.2200  1.789082e+10   1.749295e+10  2012-11-30  0.137134
2316              0.1210  6.465392e+09   6.009007e+09  2012-11-30  0.149167
2317              0.2470  4.509872e+10   4.132842e+10  2012-11-30  0.183629
[2318 rows x 12 columns]

[[  5.95720000e+00   1.18180000e+00   8.52525509e+10 ...,   1.21144486e+12
2.07014010e+10   1.08825400e+10]
[  7.02890000e+00   1.58800000e+00   8.41133582e+10 ...,   3.00252062e+11
2.93083692e+10   2.37834769e+10]
[ -2.62746100e+02   7.00030000e+00   5.17045520e+08 ...,   7.70517753e+08
1.16798290e+07   1.20300800e+07]
...,
[  3.95523000e+01   4.00520000e+00   1.70243430e+10 ...,   2.42081699e+10
1.78908166e+10   1.74929478e+10]
[  5.25408000e+01   2.46460000e+00   3.28790988e+10 ...,   3.88380258e+10
6.46539204e+09   6.00900728e+09]
[  1.42203000e+01   1.41030000e+00   5.91108572e+10 ...,   2.02066110e+11
4.50987171e+10   4.13284212e+10]]

(2318, 8)```

## 2.4 相关系数

### 2.4.1 皮尔逊相关系数(Pearson Correlation Coefficient)

= 0.9942

#### 3. 特点

from scipy.stats import pearsonr

x : (N,) array_like
y : (N,) array_like Returns: (Pearson’s correlation coefficient, p-value)

#### 5. 案例

```from scipy.stats import pearsonr
x1 = [12.5, 15.3, 23.2, 26.4, 33.5, 34.4, 39.4, 45.2, 55.4, 60.9]
x2 = [21.2, 23.9, 32.9, 34.1, 42.5, 43.2, 49.0, 52.8, 59.4, 63.5]
pearsonr(x1, x2)```

`(0.9941983762371883, 4.9220899554573455e-09)`

### 2.4.2 斯皮尔曼相关系数(Rank IC)

#### 2.公式计算案例(了解，不用记忆)

n为等级个数，d为二列成对变量的等级差数

from scipy.stats import spearmanr

```from scipy.stats import spearmanr
x1 = [12.5, 15.3, 23.2, 26.4, 33.5, 34.4, 39.4, 45.2, 55.4, 60.9]
x2 = [21.2, 23.9, 32.9, 34.1, 42.5, 43.2, 49.0, 52.8, 59.4, 63.5]
spearmanr(x1, x2)```

`SpearmanrResult(correlation=0.9999999999999999, pvalue=6.646897422032013e-64)`

## 3.2 API

sklearn.decomposition.PCA(n_components=None)

n_components:

PCA.fit_transform(X) X:numpy array格式的数据[n_samples,n_features]

## 3.3 数据计算

```from sklearn.decomposition import PCA
def pca_demo():
"""
对数据进行PCA降维
:return: None
"""
data = [[2,8,4,5], [6,3,0,8], [5,4,9,1]]
# 1、实例化PCA, 小数——保留多少信息
transfer = PCA(n_components=0.9)
# 2、调用fit_transform
data1 = transfer.fit_transform(data)
print("保留90%的信息，降维结果为：
", data1)
# 1、实例化PCA, 整数——指定降维到的维数
transfer2 = PCA(n_components=3)
# 2、调用fit_transform
data2 = transfer2.fit_transform(data)
print("降维到3维的结果：
", data2)
return None```

```保留90%的信息，降维结果为：
[[ -3.13587302e-16   3.82970843e+00]
[ -5.74456265e+00  -1.91485422e+00]
[  5.74456265e+00  -1.91485422e+00]]

[[ -3.13587302e-16   3.82970843e+00   4.59544715e-16]
[ -5.74456265e+00  -1.91485422e+00   4.59544715e-16]
[  5.74456265e+00  -1.91485422e+00   4.59544715e-16]]```