序
最近要写几个数据挖掘的算法,可是由于Java和C#没有比较成熟和知名的可视化类库,所以想到了用Python的Numpy计算,用matplotlib来做结果的可视化。
Numpy与matplotlib
- Numpy
- NumPy系统是Python的一种开源的数字扩展。这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩阵matrix)。我用它来进行矩阵的计算,非常便捷。
- matplotlib
- Matplotlib 可能是 Python 2D- 绘图领域使用最广泛的套件。它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式。
在Windows上安装软件包
在Windows上安装Numpy和matplotlib是一个和痛苦的过程。总体来说,安装这两个包的官方安装程序是远远不够的。总会提示你缺这个缺那个。我的解决办法接单粗暴,提示缺少什么了我就谷歌然后安装这个包。所有的包都可以在UCI的网站上找到。
最后为了安装Nump和matplotlib,我一共安装了6个包:
- matplotlib-1.4.2.win-amd64-py2.7
- numpy-MKL-1.9.1.win-amd64-py2.7
- pyparsing-2.0.3.win-amd64-py2.7
- python-dateutil-2.2.win-amd64-py2.7
- scipy-0.15.0b1.win-amd64-py2.7
- six-1.8.0.win-amd64-py2.7
在Ubuntu Linux上安装
在Ubuntu上安装可就简单多了,软件源里就包括这两个包,一句话搞定:
sudo apt-get install python-numpy python-matplotlib
所有依赖的包都自动安装上了,给个赞!
真心说一句,Ubuntu除了网络问题更新软件不给力之外,装软件真心是很方便、很赞的!
通用方法
其实只要用Python都可以用Python包管理工具pip,两句命令即可搞定:
pip install numpy
pip install matplotlib
而安装pip依赖于setuptools,可以点击setuptools和pip来进行下载安装。
测试代码
安装完成后,可以使用引自此页面的代码进行测试。
import numpy as np
import matplotlib.pyplot as plt
N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)
womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)
# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )
ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
plt.show()
参考文献
查阅到了几篇不错的博客和网站可以用大家参考一下:
“Numpy和matplotlib的安装和使用”的一个回复