机器学习(2)—— matplotlib

  python34 中绘图基本是matplotlib库,基于matplotlib库可以绘制基本的图形。

  1、基本绘图

  (1)点图(data:http://pan.baidu.com/s/1i3L0UDB)

 1 import matplotlib.pyplot as plt
 2 import math
 3 from numpy import *
 4 def file2matrix(filename):
 5     fr = open(filename)
 6     numberOfLines = len(fr.readlines())         #get the number of lines in the file
 7     returnMat = zeros((numberOfLines,3))        #prepare matrix to return
 8     classLabelVector = []                       #prepare labels return   
 9     fr = open(filename)
10     index = 0
11     for line in fr.readlines():
12         line = line.strip()
13         listFromLine = line.split('\t')
14         returnMat[index,:] = listFromLine[0:3]
15         classLabelVector.append(int(listFromLine[-1]))
16         index += 1
17     return returnMat,classLabelVector
18 #    return returnMat
19 
20 
21 
22 datingDataMat,datingLabels = file2matrix('datingTestSet2.txt')
23 fig = plt.figure()
24 ax = fig.add_subplot(111)
25 ax.scatter(datingDataMat[:,1],datingDataMat[:,2],15.0*array(datingLabels),15.0*array(datingLabels))
26 plt.show()

 

  

posted @ 2014-11-19 14:38  andrew.elec90  阅读(157)  评论(0编辑  收藏  举报