lolamontes69

scatterplot_gnuplot.py for Ch2 Machine Learning in Action

Oct 9th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. """ Machine learning in Action; Chapter 2.
  2.    Seems as though I can't get matplotlib up and running I wrote one of these for gnuplot which is my  
  3.    grapher and plotter of choice. :)
  4.    -lolamontes69
  5. """
  6. import Gnuplot as Gnuplot
  7. import Gnuplot, Gnuplot.funcutils
  8. from numpy import *
  9. import knn as knn
  10.  
  11. def scatterplot(datingDataMat,datingLabels,x,y):
  12.     listm,listn,listo=[],[],[]
  13.     for a in range(len(datingLabels)):
  14.         if datingLabels[a]=='largeDoses':
  15.             dep=(datingDataMat[a][x],datingDataMat[a][y])
  16.             listm.append(dep)
  17.         elif datingLabels[a]=='smallDoses':
  18.             dep=(datingDataMat[a][x],datingDataMat[a][y])
  19.             listn.append(dep)
  20.         elif datingLabels[a]=='didntLike':
  21.             dep=(datingDataMat[a][x],datingDataMat[a][y])
  22.             listo.append(dep)
  23.     g = Gnuplot.Gnuplot(debug=1)
  24.     plot1 = Gnuplot.PlotItems.Data(listm, with_="points 3", title="Large doses") # blue
  25.     plot2 = Gnuplot.PlotItems.Data(listn, with_="points 1", title="Small doses") # red
  26.     plot3 = Gnuplot.PlotItems.Data(listo, with_="points 2", title="Didnt Like") # green
  27.     if x==0:
  28.         g.xlabel('Frequent flier miles earned per year')
  29.         g.ylabel('Percentage of time spent playing Video Games')
  30.     elif x==1:
  31.         g.xlabel('Percentage of time spent playing Video Games')
  32.         g.ylabel('Liters of Ice Cream consumed per week')
  33.     else:
  34.         g.xlabel('Frequent flier miles earned per year')
  35.         g.ylabel('Liters of Ice Cream consumed per week')
  36.  
  37.     g.title('Dating Test Set Scatterplot')
  38.     g.plot(plot1,plot2,plot3)
  39.     wait=raw_input('Press <ENTER> to continue')
  40.  
  41. if __name__ == "__main__":
  42.     datingDataMat, datingLabels = knn.file2matrix('datingTestSet.txt')
  43.     scatterplot(datingDataMat,datingLabels,1,2)
  44.     scatterplot(datingDataMat,datingLabels,0,1)
  45.     scatterplot(datingDataMat,datingLabels,0,2)
Add Comment
Please, Sign In to add comment