added w5+lab6

This commit is contained in:
Rushil Umaretiya 2021-10-15 12:56:05 -04:00
parent 2879c4af94
commit ca35acb05f
9 changed files with 9195 additions and 0 deletions

1
project Submodule

@ -0,0 +1 @@
Subproject commit edb97096a76c686bbe227a07dc01602f82ce0b9c

57
w5/iris-testing.arff Normal file
View File

@ -0,0 +1,57 @@
@relation iris-weka.filters.supervised.attribute.Discretize-Rfirst-last-precision6-weka.filters.supervised.instance.Resample-B0.0-S1-Z33.0
@attribute sepallength {'\'(-inf-5.55]\'','\'(5.55-6.15]\'','\'(6.15-inf)\''}
@attribute sepalwidth {'\'(-inf-2.95]\'','\'(2.95-3.35]\'','\'(3.35-inf)\''}
@attribute petallength {'\'(-inf-2.45]\'','\'(2.45-4.75]\'','\'(4.75-inf)\''}
@attribute petalwidth {'\'(-inf-0.8]\'','\'(0.8-1.75]\'','\'(1.75-inf)\''}
@attribute class {Iris-setosa,Iris-versicolor,Iris-virginica}
@data
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(-inf-5.55]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-virginica
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica

108
w5/iris-training.arff Normal file
View File

@ -0,0 +1,108 @@
@relation iris-weka.filters.supervised.attribute.Discretize-Rfirst-last-precision6-weka.filters.supervised.attribute.Discretize-Rfirst-last-precision6-weka.filters.supervised.attribute.Discretize-Rfirst-last-precision6-weka.filters.supervised.instance.Resample-B0.0-S1-Z66.0
@attribute sepallength {'\'(-inf-5.55]\'','\'(5.55-6.15]\'','\'(6.15-inf)\''}
@attribute sepalwidth {'\'(-inf-2.95]\'','\'(2.95-3.35]\'','\'(3.35-inf)\''}
@attribute petallength {'\'(-inf-2.45]\'','\'(2.45-4.75]\'','\'(4.75-inf)\''}
@attribute petalwidth {'\'(-inf-0.8]\'','\'(0.8-1.75]\'','\'(1.75-inf)\''}
@attribute class {Iris-setosa,Iris-versicolor,Iris-virginica}
@data
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(-inf-5.55]\'','\'(3.35-inf)\'','\'(-inf-2.45]\'','\'(-inf-0.8]\'',Iris-setosa
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(-inf-5.55]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(2.45-4.75]\'','\'(0.8-1.75]\'',Iris-versicolor
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(3.35-inf)\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(3.35-inf)\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(0.8-1.75]\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(3.35-inf)\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(3.35-inf)\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(2.95-3.35]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(5.55-6.15]\'','\'(-inf-2.95]\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica
'\'(6.15-inf)\'','\'(3.35-inf)\'','\'(4.75-inf)\'','\'(1.75-inf)\'',Iris-virginica

151
w5/iris.csv Normal file
View File

@ -0,0 +1,151 @@
sepallength,sepalwidth,petallength,petalwidth,class
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3,1.4,0.1,Iris-setosa
4.3,3,1.1,0.1,Iris-setosa
5.8,4,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5,3,1.6,0.2,Iris-setosa
5,3.4,1.6,0.4,Iris-setosa
5.2,3.5,1.5,0.2,Iris-setosa
5.2,3.4,1.4,0.2,Iris-setosa
4.7,3.2,1.6,0.2,Iris-setosa
4.8,3.1,1.6,0.2,Iris-setosa
5.4,3.4,1.5,0.4,Iris-setosa
5.2,4.1,1.5,0.1,Iris-setosa
5.5,4.2,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5,3.2,1.2,0.2,Iris-setosa
5.5,3.5,1.3,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
4.4,3,1.3,0.2,Iris-setosa
5.1,3.4,1.5,0.2,Iris-setosa
5,3.5,1.3,0.3,Iris-setosa
4.5,2.3,1.3,0.3,Iris-setosa
4.4,3.2,1.3,0.2,Iris-setosa
5,3.5,1.6,0.6,Iris-setosa
5.1,3.8,1.9,0.4,Iris-setosa
4.8,3,1.4,0.3,Iris-setosa
5.1,3.8,1.6,0.2,Iris-setosa
4.6,3.2,1.4,0.2,Iris-setosa
5.3,3.7,1.5,0.2,Iris-setosa
5,3.3,1.4,0.2,Iris-setosa
7,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
5.5,2.3,4,1.3,Iris-versicolor
6.5,2.8,4.6,1.5,Iris-versicolor
5.7,2.8,4.5,1.3,Iris-versicolor
6.3,3.3,4.7,1.6,Iris-versicolor
4.9,2.4,3.3,1,Iris-versicolor
6.6,2.9,4.6,1.3,Iris-versicolor
5.2,2.7,3.9,1.4,Iris-versicolor
5,2,3.5,1,Iris-versicolor
5.9,3,4.2,1.5,Iris-versicolor
6,2.2,4,1,Iris-versicolor
6.1,2.9,4.7,1.4,Iris-versicolor
5.6,2.9,3.6,1.3,Iris-versicolor
6.7,3.1,4.4,1.4,Iris-versicolor
5.6,3,4.5,1.5,Iris-versicolor
5.8,2.7,4.1,1,Iris-versicolor
6.2,2.2,4.5,1.5,Iris-versicolor
5.6,2.5,3.9,1.1,Iris-versicolor
5.9,3.2,4.8,1.8,Iris-versicolor
6.1,2.8,4,1.3,Iris-versicolor
6.3,2.5,4.9,1.5,Iris-versicolor
6.1,2.8,4.7,1.2,Iris-versicolor
6.4,2.9,4.3,1.3,Iris-versicolor
6.6,3,4.4,1.4,Iris-versicolor
6.8,2.8,4.8,1.4,Iris-versicolor
6.7,3,5,1.7,Iris-versicolor
6,2.9,4.5,1.5,Iris-versicolor
5.7,2.6,3.5,1,Iris-versicolor
5.5,2.4,3.8,1.1,Iris-versicolor
5.5,2.4,3.7,1,Iris-versicolor
5.8,2.7,3.9,1.2,Iris-versicolor
6,2.7,5.1,1.6,Iris-versicolor
5.4,3,4.5,1.5,Iris-versicolor
6,3.4,4.5,1.6,Iris-versicolor
6.7,3.1,4.7,1.5,Iris-versicolor
6.3,2.3,4.4,1.3,Iris-versicolor
5.6,3,4.1,1.3,Iris-versicolor
5.5,2.5,4,1.3,Iris-versicolor
5.5,2.6,4.4,1.2,Iris-versicolor
6.1,3,4.6,1.4,Iris-versicolor
5.8,2.6,4,1.2,Iris-versicolor
5,2.3,3.3,1,Iris-versicolor
5.6,2.7,4.2,1.3,Iris-versicolor
5.7,3,4.2,1.2,Iris-versicolor
5.7,2.9,4.2,1.3,Iris-versicolor
6.2,2.9,4.3,1.3,Iris-versicolor
5.1,2.5,3,1.1,Iris-versicolor
5.7,2.8,4.1,1.3,Iris-versicolor
6.3,3.3,6,2.5,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
7.1,3,5.9,2.1,Iris-virginica
6.3,2.9,5.6,1.8,Iris-virginica
6.5,3,5.8,2.2,Iris-virginica
7.6,3,6.6,2.1,Iris-virginica
4.9,2.5,4.5,1.7,Iris-virginica
7.3,2.9,6.3,1.8,Iris-virginica
6.7,2.5,5.8,1.8,Iris-virginica
7.2,3.6,6.1,2.5,Iris-virginica
6.5,3.2,5.1,2,Iris-virginica
6.4,2.7,5.3,1.9,Iris-virginica
6.8,3,5.5,2.1,Iris-virginica
5.7,2.5,5,2,Iris-virginica
5.8,2.8,5.1,2.4,Iris-virginica
6.4,3.2,5.3,2.3,Iris-virginica
6.5,3,5.5,1.8,Iris-virginica
7.7,3.8,6.7,2.2,Iris-virginica
7.7,2.6,6.9,2.3,Iris-virginica
6,2.2,5,1.5,Iris-virginica
6.9,3.2,5.7,2.3,Iris-virginica
5.6,2.8,4.9,2,Iris-virginica
7.7,2.8,6.7,2,Iris-virginica
6.3,2.7,4.9,1.8,Iris-virginica
6.7,3.3,5.7,2.1,Iris-virginica
7.2,3.2,6,1.8,Iris-virginica
6.2,2.8,4.8,1.8,Iris-virginica
6.1,3,4.9,1.8,Iris-virginica
6.4,2.8,5.6,2.1,Iris-virginica
7.2,3,5.8,1.6,Iris-virginica
7.4,2.8,6.1,1.9,Iris-virginica
7.9,3.8,6.4,2,Iris-virginica
6.4,2.8,5.6,2.2,Iris-virginica
6.3,2.8,5.1,1.5,Iris-virginica
6.1,2.6,5.6,1.4,Iris-virginica
7.7,3,6.1,2.3,Iris-virginica
6.3,3.4,5.6,2.4,Iris-virginica
6.4,3.1,5.5,1.8,Iris-virginica
6,3,4.8,1.8,Iris-virginica
6.9,3.1,5.4,2.1,Iris-virginica
6.7,3.1,5.6,2.4,Iris-virginica
6.9,3.1,5.1,2.3,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
6.8,3.2,5.9,2.3,Iris-virginica
6.7,3.3,5.7,2.5,Iris-virginica
6.7,3,5.2,2.3,Iris-virginica
6.3,2.5,5,1.9,Iris-virginica
6.5,3,5.2,2,Iris-virginica
6.2,3.4,5.4,2.3,Iris-virginica
5.9,3,5.1,1.8,Iris-virginica
1 sepallength sepalwidth petallength petalwidth class
2 5.1 3.5 1.4 0.2 Iris-setosa
3 4.9 3 1.4 0.2 Iris-setosa
4 4.7 3.2 1.3 0.2 Iris-setosa
5 4.6 3.1 1.5 0.2 Iris-setosa
6 5 3.6 1.4 0.2 Iris-setosa
7 5.4 3.9 1.7 0.4 Iris-setosa
8 4.6 3.4 1.4 0.3 Iris-setosa
9 5 3.4 1.5 0.2 Iris-setosa
10 4.4 2.9 1.4 0.2 Iris-setosa
11 4.9 3.1 1.5 0.1 Iris-setosa
12 5.4 3.7 1.5 0.2 Iris-setosa
13 4.8 3.4 1.6 0.2 Iris-setosa
14 4.8 3 1.4 0.1 Iris-setosa
15 4.3 3 1.1 0.1 Iris-setosa
16 5.8 4 1.2 0.2 Iris-setosa
17 5.7 4.4 1.5 0.4 Iris-setosa
18 5.4 3.9 1.3 0.4 Iris-setosa
19 5.1 3.5 1.4 0.3 Iris-setosa
20 5.7 3.8 1.7 0.3 Iris-setosa
21 5.1 3.8 1.5 0.3 Iris-setosa
22 5.4 3.4 1.7 0.2 Iris-setosa
23 5.1 3.7 1.5 0.4 Iris-setosa
24 4.6 3.6 1 0.2 Iris-setosa
25 5.1 3.3 1.7 0.5 Iris-setosa
26 4.8 3.4 1.9 0.2 Iris-setosa
27 5 3 1.6 0.2 Iris-setosa
28 5 3.4 1.6 0.4 Iris-setosa
29 5.2 3.5 1.5 0.2 Iris-setosa
30 5.2 3.4 1.4 0.2 Iris-setosa
31 4.7 3.2 1.6 0.2 Iris-setosa
32 4.8 3.1 1.6 0.2 Iris-setosa
33 5.4 3.4 1.5 0.4 Iris-setosa
34 5.2 4.1 1.5 0.1 Iris-setosa
35 5.5 4.2 1.4 0.2 Iris-setosa
36 4.9 3.1 1.5 0.1 Iris-setosa
37 5 3.2 1.2 0.2 Iris-setosa
38 5.5 3.5 1.3 0.2 Iris-setosa
39 4.9 3.1 1.5 0.1 Iris-setosa
40 4.4 3 1.3 0.2 Iris-setosa
41 5.1 3.4 1.5 0.2 Iris-setosa
42 5 3.5 1.3 0.3 Iris-setosa
43 4.5 2.3 1.3 0.3 Iris-setosa
44 4.4 3.2 1.3 0.2 Iris-setosa
45 5 3.5 1.6 0.6 Iris-setosa
46 5.1 3.8 1.9 0.4 Iris-setosa
47 4.8 3 1.4 0.3 Iris-setosa
48 5.1 3.8 1.6 0.2 Iris-setosa
49 4.6 3.2 1.4 0.2 Iris-setosa
50 5.3 3.7 1.5 0.2 Iris-setosa
51 5 3.3 1.4 0.2 Iris-setosa
52 7 3.2 4.7 1.4 Iris-versicolor
53 6.4 3.2 4.5 1.5 Iris-versicolor
54 6.9 3.1 4.9 1.5 Iris-versicolor
55 5.5 2.3 4 1.3 Iris-versicolor
56 6.5 2.8 4.6 1.5 Iris-versicolor
57 5.7 2.8 4.5 1.3 Iris-versicolor
58 6.3 3.3 4.7 1.6 Iris-versicolor
59 4.9 2.4 3.3 1 Iris-versicolor
60 6.6 2.9 4.6 1.3 Iris-versicolor
61 5.2 2.7 3.9 1.4 Iris-versicolor
62 5 2 3.5 1 Iris-versicolor
63 5.9 3 4.2 1.5 Iris-versicolor
64 6 2.2 4 1 Iris-versicolor
65 6.1 2.9 4.7 1.4 Iris-versicolor
66 5.6 2.9 3.6 1.3 Iris-versicolor
67 6.7 3.1 4.4 1.4 Iris-versicolor
68 5.6 3 4.5 1.5 Iris-versicolor
69 5.8 2.7 4.1 1 Iris-versicolor
70 6.2 2.2 4.5 1.5 Iris-versicolor
71 5.6 2.5 3.9 1.1 Iris-versicolor
72 5.9 3.2 4.8 1.8 Iris-versicolor
73 6.1 2.8 4 1.3 Iris-versicolor
74 6.3 2.5 4.9 1.5 Iris-versicolor
75 6.1 2.8 4.7 1.2 Iris-versicolor
76 6.4 2.9 4.3 1.3 Iris-versicolor
77 6.6 3 4.4 1.4 Iris-versicolor
78 6.8 2.8 4.8 1.4 Iris-versicolor
79 6.7 3 5 1.7 Iris-versicolor
80 6 2.9 4.5 1.5 Iris-versicolor
81 5.7 2.6 3.5 1 Iris-versicolor
82 5.5 2.4 3.8 1.1 Iris-versicolor
83 5.5 2.4 3.7 1 Iris-versicolor
84 5.8 2.7 3.9 1.2 Iris-versicolor
85 6 2.7 5.1 1.6 Iris-versicolor
86 5.4 3 4.5 1.5 Iris-versicolor
87 6 3.4 4.5 1.6 Iris-versicolor
88 6.7 3.1 4.7 1.5 Iris-versicolor
89 6.3 2.3 4.4 1.3 Iris-versicolor
90 5.6 3 4.1 1.3 Iris-versicolor
91 5.5 2.5 4 1.3 Iris-versicolor
92 5.5 2.6 4.4 1.2 Iris-versicolor
93 6.1 3 4.6 1.4 Iris-versicolor
94 5.8 2.6 4 1.2 Iris-versicolor
95 5 2.3 3.3 1 Iris-versicolor
96 5.6 2.7 4.2 1.3 Iris-versicolor
97 5.7 3 4.2 1.2 Iris-versicolor
98 5.7 2.9 4.2 1.3 Iris-versicolor
99 6.2 2.9 4.3 1.3 Iris-versicolor
100 5.1 2.5 3 1.1 Iris-versicolor
101 5.7 2.8 4.1 1.3 Iris-versicolor
102 6.3 3.3 6 2.5 Iris-virginica
103 5.8 2.7 5.1 1.9 Iris-virginica
104 7.1 3 5.9 2.1 Iris-virginica
105 6.3 2.9 5.6 1.8 Iris-virginica
106 6.5 3 5.8 2.2 Iris-virginica
107 7.6 3 6.6 2.1 Iris-virginica
108 4.9 2.5 4.5 1.7 Iris-virginica
109 7.3 2.9 6.3 1.8 Iris-virginica
110 6.7 2.5 5.8 1.8 Iris-virginica
111 7.2 3.6 6.1 2.5 Iris-virginica
112 6.5 3.2 5.1 2 Iris-virginica
113 6.4 2.7 5.3 1.9 Iris-virginica
114 6.8 3 5.5 2.1 Iris-virginica
115 5.7 2.5 5 2 Iris-virginica
116 5.8 2.8 5.1 2.4 Iris-virginica
117 6.4 3.2 5.3 2.3 Iris-virginica
118 6.5 3 5.5 1.8 Iris-virginica
119 7.7 3.8 6.7 2.2 Iris-virginica
120 7.7 2.6 6.9 2.3 Iris-virginica
121 6 2.2 5 1.5 Iris-virginica
122 6.9 3.2 5.7 2.3 Iris-virginica
123 5.6 2.8 4.9 2 Iris-virginica
124 7.7 2.8 6.7 2 Iris-virginica
125 6.3 2.7 4.9 1.8 Iris-virginica
126 6.7 3.3 5.7 2.1 Iris-virginica
127 7.2 3.2 6 1.8 Iris-virginica
128 6.2 2.8 4.8 1.8 Iris-virginica
129 6.1 3 4.9 1.8 Iris-virginica
130 6.4 2.8 5.6 2.1 Iris-virginica
131 7.2 3 5.8 1.6 Iris-virginica
132 7.4 2.8 6.1 1.9 Iris-virginica
133 7.9 3.8 6.4 2 Iris-virginica
134 6.4 2.8 5.6 2.2 Iris-virginica
135 6.3 2.8 5.1 1.5 Iris-virginica
136 6.1 2.6 5.6 1.4 Iris-virginica
137 7.7 3 6.1 2.3 Iris-virginica
138 6.3 3.4 5.6 2.4 Iris-virginica
139 6.4 3.1 5.5 1.8 Iris-virginica
140 6 3 4.8 1.8 Iris-virginica
141 6.9 3.1 5.4 2.1 Iris-virginica
142 6.7 3.1 5.6 2.4 Iris-virginica
143 6.9 3.1 5.1 2.3 Iris-virginica
144 5.8 2.7 5.1 1.9 Iris-virginica
145 6.8 3.2 5.9 2.3 Iris-virginica
146 6.7 3.3 5.7 2.5 Iris-virginica
147 6.7 3 5.2 2.3 Iris-virginica
148 6.3 2.5 5 1.9 Iris-virginica
149 6.5 3 5.2 2 Iris-virginica
150 6.2 3.4 5.4 2.3 Iris-virginica
151 5.9 3 5.1 1.8 Iris-virginica

241
w5/lab6.ipynb Normal file
View File

@ -0,0 +1,241 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Naive Bayes examined 100 samples\n",
"---\n",
"Confusion Matrix\n",
"{'Iris-versicolor': 16, 'Iris-virginica': 3, 'Iris-setosa': 0}\n",
"{'Iris-versicolor': 1, 'Iris-virginica': 15, 'Iris-setosa': 0}\n",
"{'Iris-versicolor': 0, 'Iris-virginica': 0, 'Iris-setosa': 15}\n",
"\n",
"Accuracy: 92.0%\n",
"skLearn accuracy: 96.0%\n"
]
}
],
"source": [
"import math\n",
"import pandas as pd\n",
"from sklearn import preprocessing, tree, model_selection\n",
"from sklearn.naive_bayes import GaussianNB\n",
"from sklearn.datasets import load_iris\n",
"\n",
"filename = 'iris.csv'\n",
"needs_discretized = True\n",
"class_attr = 'class'\n",
"split = .67\n",
"classifier = 3\n",
"\n",
"def main():\n",
" # Read CSV\n",
" df = pd.read_csv(filename)\n",
" \n",
" # Randomize Order\n",
" df = df.sample(frac=1)\n",
"\n",
" # Discretize\n",
" if needs_discretized:\n",
" for col in df:\n",
" if col != class_attr:\n",
" df[col] = pd.qcut(df[col], q=5)\n",
" \n",
" # Split Data\n",
" if split != 1:\n",
" testing = df.head(-math.floor(len(df)*split))\n",
" data = df.head(math.floor(len(df)*split))\n",
" else:\n",
" testing = data = df\n",
" \n",
" # Choose Classifier\n",
" if classifier == 1:\n",
" r1(data, testing)\n",
" elif classifier == 2:\n",
" decision_tree(data, testing)\n",
" else:\n",
" naive_bayes(data, testing)\n",
" \n",
"def r1(data, testing):\n",
" # Set up big dictionary\n",
" rules = dict()\n",
" \n",
" for attr in data:\n",
" if attr != class_attr:\n",
" rules[attr] = dict()\n",
"\n",
" # Loop thru data\n",
" for attr in data:\n",
" if attr != class_attr:\n",
" freq = {v:{c:0 for c in data[class_attr].unique()} for v in data[attr].unique()}\n",
" for i, sample in data.iterrows():\n",
" freq[sample[attr]][sample[class_attr]] += 1\n",
" \n",
" attr_rule = dict()\n",
" error = 0\n",
" for (k,v) in freq.items():\n",
" rule = max(v, key=v.get)\n",
" for c in v:\n",
" if c != rule:\n",
" error += v[c]\n",
" attr_rule[k] = rule\n",
" error /= len(data)\n",
" rules[attr] = (attr_rule, error)\n",
" \n",
" # Select best attr\n",
" best_attr = min(rules, key=lambda x: rules[x][1])\n",
" rule = rules[best_attr][0]\n",
" print(f'R1 chose {best_attr}')\n",
" print(print_tree(rule))\n",
" print('---')\n",
" \n",
" confusion = {v:{c:0 for c in data[class_attr].unique()} for v in data[class_attr].unique()}\n",
" \n",
" correct = 0\n",
" for i, row in testing.iterrows():\n",
" confusion[row[class_attr]][rule[row[best_attr]]] += 1\n",
" if row[class_attr] == rule[row[best_attr]]: correct += 1\n",
" \n",
" print(\"Confusion Matrix\")\n",
" \n",
" for (actual,guess) in confusion.items():\n",
" print(guess)\n",
" print()\n",
" print(f'Accuracy: {round((correct/len(testing))*100, 3)}%')\n",
"\n",
"\n",
"def decision_tree(data, testing):\n",
" print(f'Decision Tree examined {len(data)} samples and built the following tree:', end='')\n",
" rules = recur_tree(data)\n",
" print_tree(rules)\n",
" print('\\n---')\n",
" print(\"Confusion Matrix\")\n",
" confusion, correct = {v:{c:0 for c in data[class_attr].unique()} for v in data[class_attr].unique()}, 0\n",
" \n",
" for i, row in testing.iterrows():\n",
" guess = test_tree(row, rules)\n",
" confusion[row[class_attr]][guess] += 1\n",
" if row[class_attr] == guess: correct += 1 \n",
" \n",
" for (actual,guess) in confusion.items():\n",
" print(guess)\n",
"\n",
" print()\n",
" print(f'Accuracy: {round((correct/len(testing))*100, 3)}%')\n",
" \n",
" # Test with sklearn tree\n",
" dtc = tree.DecisionTreeClassifier()\n",
" x,y = load_iris(return_X_y=True)\n",
" x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=(1-split), random_state=0)\n",
" y_pred = dtc.fit(x_train, y_train).predict(x_test)\n",
" print(f'skLearn accuracy: {sum(y_pred == y_test)*100/len(y_pred)}%')\n",
"\n",
"def recur_tree(data):\n",
" rules = {}\n",
" \n",
" # Find info gain per attrT\n",
" info = calc_info(data)\n",
" if info == 0:\n",
" return data[class_attr].unique()[0]\n",
" \n",
" # gain = {attr:sum([info - calc_info(data[data[attr] == v]) for v in data[attr].unique()]) for attr in data if attr != class_attr}\n",
" gain = {attr:0 for attr in data if attr != class_attr}\n",
" for attr in gain:\n",
" for v in data[attr].unique():\n",
" gain[attr] += info - calc_info(data[data[attr] == v])\n",
" \n",
" # Choose highest info gain\n",
" attr = max(gain, key=gain.get)\n",
" if (gain[attr] == 0): \n",
" return data[class_attr].unique()[0]\n",
" \n",
" # Split data based on values of attr and recur\n",
" rules[attr] = {}\n",
" for v in data[attr].unique():\n",
" rules[attr][v] = recur_tree(data[data[attr] == v])\n",
"\n",
" return rules\n",
" \n",
"def calc_info(data):\n",
" return abs(sum([(count/len(data))*math.log((count/len(data)), 2) for count in data[class_attr].value_counts()]))\n",
" \n",
"def print_tree(rules, indent=0):\n",
" if type(rules) != dict: return rules\n",
" \n",
" for key in rules.keys():\n",
" print('\\n'+' '*3*indent + f'* {key}', end='')\n",
" s = print_tree(rules[key], indent + 1)\n",
" if s: print(f' --> {s}', end='')\n",
" \n",
" return None\n",
"\n",
"def test_tree(row, rules):\n",
" if type(rules) != dict: return rules\n",
" \n",
" attr = list(rules.keys())[0]\n",
" return test_tree(row, rules[attr][row[attr]])\n",
"\n",
"def naive_bayes(data, testing):\n",
" confusion, correct = {v:{c:0 for c in data[class_attr].unique()} for v in data[class_attr].unique()}, 0\n",
" class_freq = {c:(len(data[data[class_attr] == c])) for c in data[class_attr].unique()}\n",
" for i, row in testing.iterrows():\n",
" probs = {c:(len(data[data[class_attr] == c]))/len(data) for c in data[class_attr].unique()}\n",
" \n",
" for attr in data:\n",
" if attr != class_attr:\n",
" same_value = data[data[attr] == row[attr]]\n",
" for c in class_freq.keys():\n",
" probs[c] *= len(same_value[same_value[class_attr] == c])/class_freq[c]\n",
" \n",
" guess = max(probs, key=probs.get)\n",
" confusion[row[class_attr]][guess] += 1\n",
" if row[class_attr] == guess: correct += 1\n",
" \n",
" print(f'Naive Bayes examined {len(data)} samples')\n",
" print('---')\n",
" print(\"Confusion Matrix\")\n",
" for (actual,guess) in confusion.items():\n",
" print(guess)\n",
" print()\n",
" print(f'Accuracy: {round((correct/len(testing))*100, 3)}%')\n",
" \n",
" # Test with sklearn GaussianNaiveBayes\n",
" nb = GaussianNB()\n",
" x,y = load_iris(return_X_y=True)\n",
" x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=(1-split), random_state=0)\n",
" y_pred = nb.fit(x_train, y_train).predict(x_test)\n",
" print(f'skLearn accuracy: {sum(y_pred == y_test)*100/len(y_pred)}%')\n",
" \n",
"if __name__ == '__main__':\n",
" main()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (default)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

192
w5/lab6.py Normal file
View File

@ -0,0 +1,192 @@
import math
import pandas as pd
from sklearn import preprocessing, tree, model_selection
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import load_iris
filename = 'iris.csv'
needs_discretized = True
class_attr = 'class'
split = .67
classifier = 3
def main():
# Read CSV
df = pd.read_csv(filename)
# Randomize Order
df = df.sample(frac=1)
# Discretize
if needs_discretized:
for col in df:
if col != class_attr:
df[col] = pd.qcut(df[col], q=5)
# Split Data
if split != 1:
testing = df.head(-math.floor(len(df)*split))
data = df.head(math.floor(len(df)*split))
else:
testing = data = df
# Choose Classifier
if classifier == 1:
r1(data, testing)
elif classifier == 2:
decision_tree(data, testing)
else:
naive_bayes(data, testing)
def r1(data, testing):
# Set up big dictionary
rules = dict()
for attr in data:
if attr != class_attr:
rules[attr] = dict()
# Loop thru data
for attr in data:
if attr != class_attr:
freq = {v:{c:0 for c in data[class_attr].unique()} for v in data[attr].unique()}
for i, sample in data.iterrows():
freq[sample[attr]][sample[class_attr]] += 1
attr_rule = dict()
error = 0
for (k,v) in freq.items():
rule = max(v, key=v.get)
for c in v:
if c != rule:
error += v[c]
attr_rule[k] = rule
error /= len(data)
rules[attr] = (attr_rule, error)
# Select best attr
best_attr = min(rules, key=lambda x: rules[x][1])
rule = rules[best_attr][0]
print(f'R1 chose {best_attr}')
print(print_tree(rule))
print('---')
confusion = {v:{c:0 for c in data[class_attr].unique()} for v in data[class_attr].unique()}
correct = 0
for i, row in testing.iterrows():
confusion[row[class_attr]][rule[row[best_attr]]] += 1
if row[class_attr] == rule[row[best_attr]]: correct += 1
print("Confusion Matrix")
for (actual,guess) in confusion.items():
print(guess)
print()
print(f'Accuracy: {round((correct/len(testing))*100, 3)}%')
def decision_tree(data, testing):
print(f'Decision Tree examined {len(data)} samples and built the following tree:', end='')
rules = recur_tree(data)
print_tree(rules)
print('\n---')
print("Confusion Matrix")
confusion, correct = {v:{c:0 for c in data[class_attr].unique()} for v in data[class_attr].unique()}, 0
for i, row in testing.iterrows():
guess = test_tree(row, rules)
confusion[row[class_attr]][guess] += 1
if row[class_attr] == guess: correct += 1
for (actual,guess) in confusion.items():
print(guess)
print()
print(f'Accuracy: {round((correct/len(testing))*100, 3)}%')
# Test with sklearn tree
dtc = tree.DecisionTreeClassifier()
x,y = load_iris(return_X_y=True)
x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=(1-split), random_state=0)
y_pred = dtc.fit(x_train, y_train).predict(x_test)
print(f'skLearn accuracy: {sum(y_pred == y_test)*100/len(y_pred)}%')
def recur_tree(data):
rules = {}
# Find info gain per attrT
info = calc_info(data)
if info == 0:
return data[class_attr].unique()[0]
# gain = {attr:sum([info - calc_info(data[data[attr] == v]) for v in data[attr].unique()]) for attr in data if attr != class_attr}
gain = {attr:0 for attr in data if attr != class_attr}
for attr in gain:
for v in data[attr].unique():
gain[attr] += info - calc_info(data[data[attr] == v])
# Choose highest info gain
attr = max(gain, key=gain.get)
if (gain[attr] == 0):
return data[class_attr].unique()[0]
# Split data based on values of attr and recur
rules[attr] = {}
for v in data[attr].unique():
rules[attr][v] = recur_tree(data[data[attr] == v])
return rules
def calc_info(data):
return abs(sum([(count/len(data))*math.log((count/len(data)), 2) for count in data[class_attr].value_counts()]))
def print_tree(rules, indent=0):
if type(rules) != dict: return rules
for key in rules.keys():
print('\n'+' '*3*indent + f'* {key}', end='')
s = print_tree(rules[key], indent + 1)
if s: print(f' --> {s}', end='')
return None
def test_tree(row, rules):
if type(rules) != dict: return rules
attr = list(rules.keys())[0]
return test_tree(row, rules[attr][row[attr]])
def naive_bayes(data, testing):
confusion, correct = {v:{c:0 for c in data[class_attr].unique()} for v in data[class_attr].unique()}, 0
class_freq = {c:(len(data[data[class_attr] == c])) for c in data[class_attr].unique()}
for i, row in testing.iterrows():
probs = {c:(len(data[data[class_attr] == c]))/len(data) for c in data[class_attr].unique()}
for attr in data:
if attr != class_attr:
same_value = data[data[attr] == row[attr]]
for c in class_freq.keys():
probs[c] *= len(same_value[same_value[class_attr] == c])/class_freq[c]
guess = max(probs, key=probs.get)
confusion[row[class_attr]][guess] += 1
if row[class_attr] == guess: correct += 1
print(f'Naive Bayes examined {len(data)} samples')
print('---')
print("Confusion Matrix")
for (actual,guess) in confusion.items():
print(guess)
print()
print(f'Accuracy: {round((correct/len(testing))*100, 3)}%')
# Test with sklearn GaussianNaiveBayes
nb = GaussianNB()
x,y = load_iris(return_X_y=True)
x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=(1-split), random_state=0)
y_pred = nb.fit(x_train, y_train).predict(x_test)
print(f'skLearn accuracy: {sum(y_pred == y_test)*100/len(y_pred)}%')
if __name__ == '__main__':
main()

8417
w5/mushroom.csv Normal file

File diff suppressed because it is too large Load Diff

15
w5/play_tennis.csv Normal file
View File

@ -0,0 +1,15 @@
Outlook,Temp,Humidity,Wind,class
Sunny,Hot,High,Weak,No
Sunny,Hot,High,Strong,No
Overcast,Hot,High,Weak,Yes
Rain,Mild,High,Weak,Yes
Rain,Cool,Normal,Weak,Yes
Rain,Cool,Normal,Strong,No
Overcast,Cool,Normal,Strong,Yes
Sunny,Mild,High,Weak,No
Sunny,Cool,Normal,Weak,Yes
Rain,Mild,Normal,Weak,Yes
Sunny,Mild,Normal,Strong,Yes
Overcast,Mild,High,Strong,Yes
Overcast,Hot,Normal,Weak,Yes
Rain,Mild,High,Strong,No
1 Outlook Temp Humidity Wind class
2 Sunny Hot High Weak No
3 Sunny Hot High Strong No
4 Overcast Hot High Weak Yes
5 Rain Mild High Weak Yes
6 Rain Cool Normal Weak Yes
7 Rain Cool Normal Strong No
8 Overcast Cool Normal Strong Yes
9 Sunny Mild High Weak No
10 Sunny Cool Normal Weak Yes
11 Rain Mild Normal Weak Yes
12 Sunny Mild Normal Strong Yes
13 Overcast Mild High Strong Yes
14 Overcast Hot Normal Weak Yes
15 Rain Mild High Strong No

13
w5/willwait.csv Normal file
View File

@ -0,0 +1,13 @@
Alternative Restaurant Nearby,Bar Area To Wait,Friday or Saturday,Hungry,Patrons,Price Range,Raining,Reservation,Type,Estimated Wait,WillWait
Yes,No,No,Yes,Some,$$$,No,Yes,French,0-10,Yes
Yes,No,No,Yes,Full,$,No,No,Thai,30-60,No
No,Yes,No,No,Some,$,No,No,Burger,0-10,Yes
Yes,No,Yes,Yes,Full,$,No,No,Thai,10-30,Yes
Yes,No,Yes,No,Full,$$$,No,Yes,French,>60,No
No,Yes,No,Yes,Some,$$,Yes,Yes,Italian,0-10,Yes
No,Yes,No,No,None,$,Yes,No,Burger,0-10,No
No,No,No,Yes,Some,$$,Yes,Yes,Thai,0-10,Yes
No,Yes,Yes,No,Full,$,Yes,No,Burger,>60,No
Yes,Yes,Yes,Yes,Full,$$$,No,Yes,Italian,10-30,No
No,No,No,No,None,$,No,No,Thai,0-10,No
Yes,Yes,Yes,Yes,Full,$,No,No,Burger,30-60,Yes
1 Alternative Restaurant Nearby Bar Area To Wait Friday or Saturday Hungry Patrons Price Range Raining Reservation Type Estimated Wait WillWait
2 Yes No No Yes Some $$$ No Yes French 0-10 Yes
3 Yes No No Yes Full $ No No Thai 30-60 No
4 No Yes No No Some $ No No Burger 0-10 Yes
5 Yes No Yes Yes Full $ No No Thai 10-30 Yes
6 Yes No Yes No Full $$$ No Yes French >60 No
7 No Yes No Yes Some $$ Yes Yes Italian 0-10 Yes
8 No Yes No No None $ Yes No Burger 0-10 No
9 No No No Yes Some $$ Yes Yes Thai 0-10 Yes
10 No Yes Yes No Full $ Yes No Burger >60 No
11 Yes Yes Yes Yes Full $$$ No Yes Italian 10-30 No
12 No No No No None $ No No Thai 0-10 No
13 Yes Yes Yes Yes Full $ No No Burger 30-60 Yes