55import densite_fonction
66
77
8-
98class Densite1D :
109 def __init__ (self ):
1110 self .iris = numpy .loadtxt ('iris.txt' )
1211 self .oneDimData = self ._getOneDimDate ()
1312
14-
1513 def _getOneDimDate (self ):
1614 chosenClass = 1
1715 param = 0
@@ -21,40 +19,42 @@ def _getOneDimDate(self):
2119 oneDimData .append (l [param ])
2220 return numpy .array (oneDimData )
2321
24-
2522 def getOneDimGraph (self ):
26- trainSetPlot , = plt .plot (self .oneDimData ,numpy .array ([i / 1000. for i in range (len (self .oneDimData ))]), 'bo' )
23+ trainSetPlot , = plt .plot (self .oneDimData , numpy .array ([i / 1000. for i in range (len (self .oneDimData ))]), 'bo' )
2724
2825 dg = densite_fonction .DensiteGaussienne (1 )
2926 dg .train (self .oneDimData )
30- x = numpy .linspace (3 ,7 ,1000 )
31- densParamPlot , = plt .plot (x ,[dg .p (i ) for i in x ], 'red' , label = "Densite parametrique" )
32-
27+ x = numpy .linspace (3 , 7 , 1000 )
28+ densParamPlot , = plt .plot (x , [dg .p (i ) for i in x ], 'red' , label = "Densite parametrique" )
3329
3430 sigma = 0.05
35- dg = densite_fonction .DensiteParzen (1 ,sigma )
31+ dg = densite_fonction .DensiteParzen (1 , sigma )
3632 dg .train (self .oneDimData )
37- x = numpy .linspace (3 ,7 , 1000 )
38- parzenParamPlotGrand , = plt .plot (x ,[dg .p (i ) for i in x ], 'yellow' , label = "Parzen sigma petit" )
33+ x = numpy .linspace (3 , 7 , 1000 )
34+ parzenParamPlotGrand , = plt .plot (x , [dg .p (i ) for i in x ], 'yellow' , label = "Parzen sigma petit" )
3935
4036 sigma = 1
41- dg = densite_fonction .DensiteParzen (1 ,sigma )
37+ dg = densite_fonction .DensiteParzen (1 , sigma )
4238 dg .train (self .oneDimData )
43- x = numpy .linspace (3 ,7 , 1000 )
44- parzenParamPlotPetit , = plt .plot (x ,[dg .p (i ) for i in x ], 'green' , label = "Parzen sigma grand" )
39+ x = numpy .linspace (3 , 7 , 1000 )
40+ parzenParamPlotPetit , = plt .plot (x , [dg .p (i ) for i in x ], 'green' , label = "Parzen sigma grand" )
4541
4642 sigma = 0.349
47- dg = densite_fonction .DensiteParzen (1 ,sigma )
43+ dg = densite_fonction .DensiteParzen (1 , sigma )
4844 dg .train (self .oneDimData )
49- x = numpy .linspace (3 ,7 , 1000 )
50- parzenParamPlotAdequat , = plt .plot (x ,[dg .p (i ) for i in x ], 'blue' , label = "Parzen sigma adequat" )
45+ x = numpy .linspace (3 , 7 , 1000 )
46+ parzenParamPlotAdequat , = plt .plot (x , [dg .p (i ) for i in x ], 'blue' , label = "Parzen sigma adequat" )
5147
52- plt .legend ([trainSetPlot , densParamPlot , parzenParamPlotGrand ,parzenParamPlotPetit ,parzenParamPlotAdequat ], ["Ensemble entrainement" , "Densite parametrique" ,"Parzen sigma petit" ,"Parzen sigma grand" ,"Parzen sigma adequat" ])
48+ plt .legend ([trainSetPlot , densParamPlot , parzenParamPlotGrand , parzenParamPlotPetit , parzenParamPlotAdequat ],
49+ ["Ensemble entrainement" , "Densite parametrique" , "Parzen sigma petit" , "Parzen sigma grand" ,
50+ "Parzen sigma adequat" ])
5351 plt .axis ([3 , 7 , 0 , 1.7 ])
54- plt .title ('1D Gaussian and Parzen probability density' )
52+ title = '1D Gaussian and Parzen probability density'
53+ plt .title (title )
5554 plt .xlabel ('x' )
5655 plt .ylabel ('Probability density' )
57- plt .show ()
56+ plt .savefig (title .replace (" " , "_" ) + ".png" )
57+ plt .close ()
5858
5959
6060class Densite2D :
@@ -78,71 +78,66 @@ def getParamDensityGraph(self):
7878 min_x2 = min ([i [1 ] for i in self .twoDimData ])
7979 max_x2 = max (([i [1 ] for i in self .twoDimData ]))
8080 nDots = 100
81- plt .plot (numpy .array ([i [0 ] for i in self .twoDimData ]),numpy .array ([i [1 ] for i in self .twoDimData ]), 'bo' )
82-
83-
84- xgrid = numpy .linspace (min_x1 ,max_x1 ,num = nDots )
85- ygrid = numpy .linspace (min_x2 ,max_x2 ,num = nDots )
86- #theGrid = numpy.array(utilitaires.combine(xgrid,ygrid))
81+ plt .plot (numpy .array ([i [0 ] for i in self .twoDimData ]), numpy .array ([i [1 ] for i in self .twoDimData ]), 'bo' )
8782
83+ xgrid = numpy .linspace (min_x1 , max_x1 , num = nDots )
84+ ygrid = numpy .linspace (min_x2 , max_x2 , num = nDots )
8885
8986 dg = densite_fonction .DensiteGaussienne (2 )
9087 dg .train (self .twoDimData )
9188
92- resZ = [[ 0 for i in range (nDots )] for j in range (nDots )]
89+ resZ = [[0 for i in range (nDots )] for j in range (nDots )]
9390 for ix , x in enumerate (xgrid ):
9491 for iy , y in enumerate (ygrid ):
95- resZ [ix ][iy ] = float (dg .p ([x ,y ]))
96-
97- plt .contour (xgrid ,ygrid ,numpy .array (resZ ))
92+ resZ [ix ][iy ] = float (dg .p ([x , y ]))
9893
94+ plt .contour (xgrid , ygrid , numpy .array (resZ ))
9995
10096 plt .axis ([min_x1 , max_x1 , min_x2 , max_x2 ])
101- plt .title ('2D Gaussian border' )
97+ title = '2D Gaussian border'
98+ plt .title (title )
10299 plt .xlabel ('x1' )
103100 plt .ylabel ('x2' )
104- plt .show ()
101+ plt .savefig (title .replace (" " , "_" ) + ".png" )
102+ plt .close ()
105103
106- def getParzenSmallSigma (self ):
104+ def getParzenGraph (self , sigma ):
107105 min_x1 = min ([i [0 ] for i in self .twoDimData ])
108106 max_x1 = max ([i [0 ] for i in self .twoDimData ])
109107 min_x2 = min ([i [1 ] for i in self .twoDimData ])
110108 max_x2 = max (([i [1 ] for i in self .twoDimData ]))
111109 nDots = 100
112- plt .plot (numpy .array ([i [0 ] for i in self .twoDimData ]),numpy .array ([i [1 ] for i in self .twoDimData ]), 'bo' )
113-
110+ plt .plot (numpy .array ([i [0 ] for i in self .twoDimData ]), numpy .array ([i [1 ] for i in self .twoDimData ]), 'bo' )
114111
115- xgrid = numpy .linspace (min_x1 ,max_x1 ,num = nDots )
116- ygrid = numpy .linspace (min_x2 ,max_x2 ,num = nDots )
112+ xgrid = numpy .linspace (min_x1 , max_x1 , num = nDots )
113+ ygrid = numpy .linspace (min_x2 , max_x2 , num = nDots )
117114
118- sigma = 0.5
119- dg = densite_fonction .DensiteParzen (2 ,sigma )
115+ dg = densite_fonction .DensiteParzen (2 , sigma )
120116 dg .train (self .twoDimData )
121117
122- resZ = [[ 0 for i in range (nDots )] for j in range (nDots )]
118+ resZ = [[0 for i in range (nDots )] for j in range (nDots )]
123119 for ix , x in enumerate (xgrid ):
124120 for iy , y in enumerate (ygrid ):
125- resZ [ix ][iy ] = float (dg .p ([x ,y ]))
126-
127- plt .contour (xgrid ,ygrid ,numpy .array (resZ ))
121+ resZ [ix ][iy ] = float (dg .p ([x , y ]))
128122
123+ plt .contour (xgrid , ygrid , numpy .array (resZ ))
129124
130125 plt .axis ([min_x1 , max_x1 , min_x2 , max_x2 ])
131- plt .title ('2D Gaussian border' )
126+ title = '2D Parzen sigma=' + str (sigma )
127+ plt .title (title )
132128 plt .xlabel ('x1' )
133129 plt .ylabel ('x2' )
134- plt .show ()
135-
136- def getParzenBigSigma (self ):
137- pass
130+ plt .savefig (title .replace (" " , "_" ) + ".png" )
131+ plt .close ()
132+ # plt.show()
138133
139- def getParzenGoodSigma (self ):
140- pass
141134
142135if __name__ == '__main__' :
143- # d1d = Densite1D()
144- # d1d.getOneDimGraph()
136+ d1d = Densite1D ()
137+ d1d .getOneDimGraph ()
145138
146139 d2d = Densite2D ()
147- #d2d.getParamDensityGraph()
148- d2d .getParzenSmallSigma ()
140+ d2d .getParamDensityGraph ()
141+ d2d .getParzenGraph (0.08 )
142+ d2d .getParzenGraph (0.40 )
143+ d2d .getParzenGraph (4 )
0 commit comments