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

时间:2021-05-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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章