管道、Anova和SVM
先后运行带有anova的单变量特征选择,然后用所选特征训练一个SVM,最后使用子管道,可以将拟合系数映射回原始的特征空间。 输出: precision recall f1-score support
0 0.75 0.50 0.60 6
1 0.67 1.00 0.80 6
2 0.67 0.80 0.73 5
3 1.00 0.75 0.86 8
accuracy 0.76 25
macro avg 0.77 0.76 0.75 25
weighted avg 0.79 0.76 0.76 25
[[-0.23912131 0. 0. 0. -0.3236911 0.
0. 0. 0. 0. 0. 0.
0.10836648 0. 0. 0. 0. 0.
0. 0. ]
[ 0.43878747 0. 0. 0. -0.51415652 0.
0. 0. 0. 0. 0. 0.
0.04845652 0. 0. 0. 0. 0.
0. 0. ]
[-0.65382998 0. 0. 0. 0.57962856 0.
0. 0. 0. 0. 0. 0.
-0.04736524 0. 0. 0. 0. 0.
0. 0. ]
[ 0.54403412 0. 0. 0. 0.58478491 0.
0. 0. 0. 0. 0. 0.
-0.11344659 0. 0. 0. 0. 0.
0. 0. ]]
from sklearn import svm
from sklearn.datasets import make_classification
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
print(__doc__)
# 导入一些数据进行训练
X, y = make_classification(
n_features=20, n_informative=3, n_redundant=0, n_classes=4,
n_clusters_per_class=2)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# ANOVA SVM-C
# 1)方差分析滤镜,采用3种最佳特征
anova_filter = SelectKBest(f_regression, k=3)
# 2) svm
clf = svm.LinearSVC()
anova_svm = make_pipeline(anova_filter, clf)
anova_svm.fit(X_train, y_train)
y_pred = anova_svm.predict(X_test)
print(classification_report(y_test, y_pred))
coef = anova_svm[:-1].inverse_transform(anova_svm['linearsvc'].coef_)
print(coef)
脚本的总运行时间:(0分钟0.341秒)
估计的内存使用量: 8 MB
![052ddd663583833ea103259322c1d854.png](https://i-blog.csdnimg.cn/blog_migrate/380f8a182517cfdd0310ede00e2897c6.png)
下载Python源代码:plot_feature_selection_pipeline.py
下载Jupyter notebook源代码:plot_feature_selection_pipeline.ipynb
由Sphinx-Gallery生成的画廊
![ea5b327d607e63ef19685cab1fdb82f9.png](https://i-blog.csdnimg.cn/blog_migrate/0e333fca63bac3d7e3dfb1e934090811.png)
![efefe905a9dde494353b66dc2c6b7b2e.png](https://i-blog.csdnimg.cn/blog_migrate/6810e23ac5a5e1d4e395c1011e0f33d6.png)
欢迎大家和我一起沿着scikit-learn文档这条路线,一起巩固机器学习算法基础。(添加微信:mthler,备注:sklearn学习,一起进【sklearn机器学习进步群】开启打怪升级的学习之旅。)