python机器学习包mlxtend的安装和配置

来源:互联网 发布:px4飞控源码 百度云 编辑:程序博客网 时间:2024/06/05 19:22

今天看到了mlxtend的包,看了下example集成得非常简洁。还有一个吸引我的地方是自带了一些data直接可以用,省去了自己造数据或者找数据的处理过程,所以决定安装体验一下。

依赖环境

首先,sudo pip install mlxtend 得到基础环境。

然后开始看看系统依赖问题的解决。大致看了下基本都是python科学计算用的那几个经典的包,主要是numpy,scipy,matplotlib,sklearn这些。

LINUX环境下的话,一般这些都比较好装pip一般都能搞定。
这里要说的一点是matplotlib的话,pip装的时候提示我的几个问题是png和一个叫Freetype的包被需要,但是装的时候又出现问题。所以matplotlib最后选择用

sudo apt-get install python-matplotlib

直接解决依赖问题。

同样的情况对于scipy也是一样,用

sudo apt-get install python-scipy

解决。

示例代码

import numpy as npimport matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecimport itertoolsfrom sklearn.linear_model import LogisticRegressionfrom sklearn.svm import SVCfrom sklearn.ensemble import RandomForestClassifierfrom mlxtend.classifier import EnsembleVoteClassifierfrom mlxtend.data import iris_datafrom mlxtend.evaluate import plot_decision_regions# Initializing Classifiersclf1 = LogisticRegression(random_state=0)clf2 = RandomForestClassifier(random_state=0)clf3 = SVC(random_state=0, probability=True)eclf = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3], weights=[2, 1, 1], voting='soft')# Loading some example dataX, y = iris_data()X = X[:,[0, 2]]# Plotting Decision Regionsgs = gridspec.GridSpec(2, 2)fig = plt.figure(figsize=(10, 8))for clf, lab, grd in zip([clf1, clf2, clf3, eclf],                         ['Logistic Regression', 'Random Forest', 'Naive Bayes', 'Ensemble'],                         itertools.product([0, 1], repeat=2)):    clf.fit(X, y)    ax = plt.subplot(gs[grd[0], grd[1]])    fig = plot_decision_regions(X=X, y=y, clf=clf, legend=2)    plt.title(lab)plt.show()

之后就可以来跑一下这个示例代码。

matplot结果如图:
这里写图片描述

之后就可以开始玩了~!

附:linux下python科学计算的经典的包的一个总和的命令:

sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
1 0
原创粉丝点击