Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
2f7c2732
Commit
2f7c2732
authored
May 27, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
StatRed ass3: Changed file structures.
parent
13a9a5bb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
138 additions
and
0 deletions
+138
-0
statred/ass3/Makefile
statred/ass3/Makefile
+12
-0
statred/ass3/classifiers.py
statred/ass3/classifiers.py
+43
-0
statred/ass3/classify.py
statred/ass3/classify.py
+83
-0
No files found.
statred/ass3/Makefile
0 → 100644
View file @
2f7c2732
SEED
=
100
knnb
:
echo
'nnb:'
;
\
python classify.py nnb
${SEED}
;
\
for
i
in
1 3 5 7 9
;
do
\
echo
'knnb '
$$
i
':'
;
\
python classify.py knnb
$$
i
${SEED}
;
\
done
clean
:
rm
knnb-
*
.pdf
statred/ass3/classifiers.py
0 → 100644
View file @
2f7c2732
from
pylab
import
argmin
,
tile
class
NNb
:
def
__init__
(
self
,
X
,
c
):
self
.
n
,
self
.
N
=
X
.
shape
self
.
X
,
self
.
c
=
X
,
c
def
classify
(
self
,
x
):
d
=
self
.
X
-
tile
(
x
.
reshape
(
self
.
n
,
1
),
self
.
N
);
dsq
=
sum
(
d
*
d
,
0
)
return
self
.
c
[
argmin
(
dsq
)]
class
kNNb
:
def
__init__
(
self
,
X
,
c
,
k
):
self
.
n
,
self
.
N
=
X
.
shape
self
.
X
,
self
.
c
,
self
.
k
=
X
,
c
,
k
def
classify
(
self
,
x
):
d
=
self
.
X
-
tile
(
x
.
reshape
(
self
.
n
,
1
),
self
.
N
);
dsq
=
sum
(
d
*
d
,
0
)
minindices
=
dsq
.
argsort
()
# Count class occurrences in k nearest neighbours
hist
=
{}
for
c
in
self
.
c
[
minindices
[:
self
.
k
]]:
try
:
hist
[
c
]
+=
1
except
KeyError
:
hist
[
c
]
=
1
# Return the majority class
max_nbb
=
(
0
,
None
)
for
c
,
count
in
hist
.
iteritems
():
if
count
>
max_nbb
[
0
]:
max_nnb
=
(
count
,
c
)
return
max_nnb
[
1
]
class
MEC
:
def
__init__
(
self
,
X
,
c
):
self
.
n
,
self
.
N
=
X
.
shape
self
.
X
,
self
.
c
=
X
,
c
def
classify
(
self
,
x
):
# TODO: Implement MEC classifier
pass
statred/ass3/
1-knnb
.py
→
statred/ass3/
classify
.py
View file @
2f7c2732
from
pylab
import
loadtxt
,
arange
,
loadtxt
,
permutation
,
transpose
,
\
from
pylab
import
loadtxt
,
arange
,
loadtxt
,
permutation
,
transpose
,
\
zeros
,
sum
,
plot
,
subplot
,
array
,
scatter
,
logical_and
,
figure
,
\
zeros
,
sum
,
plot
,
subplot
,
array
,
scatter
,
logical_and
,
figure
,
\
s
how
,
savefig
,
tile
,
argmin
,
seed
s
avefig
,
seed
from
sys
import
argv
,
exit
from
sys
import
argv
,
exit
import
classifiers
try
:
# Parse input
k
=
int
(
argv
[
1
]
)
argc
=
len
(
argv
)
except
IndexError
:
if
argc
<
2
:
print
'Usage: python %s
K [ SEED ] (use K = -1 for the regular NNb method)
'
%
argv
[
0
]
print
'Usage: python %s
METHOD [ K ] [ SEED ]
'
%
argv
[
0
]
exit
()
exit
()
method
=
argv
[
1
]
try
:
if
method
==
'knnb'
:
if
argc
<
3
:
print
'Usage: python %s %s K [ SEED ]'
%
tuple
(
argv
[:
2
])
exit
()
k
=
int
(
argv
[
2
])
if
argc
==
4
:
seed
(
int
(
argv
[
3
]))
elif
method
not
in
[
'nnb'
,
'mec'
]:
print
'Unknown classification method "%s"'
%
argv
[
1
]
exit
()
elif
argc
==
3
:
seed
(
int
(
argv
[
2
]))
seed
(
int
(
argv
[
2
]))
except
IndexError
:
pass
class
NNb
:
def
__init__
(
self
,
X
,
c
,
k
):
self
.
n
,
self
.
N
=
X
.
shape
self
.
X
,
self
.
c
=
X
,
c
def
classify
(
self
,
x
):
d
=
self
.
X
-
tile
(
x
.
reshape
(
self
.
n
,
1
),
self
.
N
);
dsq
=
sum
(
d
*
d
,
0
)
return
self
.
c
[
argmin
(
dsq
)]
class
kNNb
:
def
__init__
(
self
,
X
,
c
,
k
):
self
.
n
,
self
.
N
=
X
.
shape
self
.
X
,
self
.
c
,
self
.
k
=
X
,
c
,
k
def
classify
(
self
,
x
):
d
=
self
.
X
-
tile
(
x
.
reshape
(
self
.
n
,
1
),
self
.
N
);
dsq
=
sum
(
d
*
d
,
0
)
minindices
=
dsq
.
argsort
()
# Count class occurrences in k nearest neighbours
hist
=
{}
for
c
in
self
.
c
[
minindices
[:
self
.
k
]]:
try
:
hist
[
c
]
+=
1
except
KeyError
:
hist
[
c
]
=
1
# Return the majority class
max_nbb
=
(
0
,
None
)
for
c
,
count
in
hist
.
iteritems
():
if
count
>
max_nbb
[
0
]:
max_nnb
=
(
count
,
c
)
return
max_nnb
[
1
]
# Load dataset
def
cnvt
(
s
):
def
cnvt
(
s
):
tab
=
{
'Iris-setosa'
:
1.0
,
'Iris-versicolor'
:
2.0
,
'Iris-virginica'
:
3.0
}
tab
=
{
'Iris-setosa'
:
1.0
,
'Iris-versicolor'
:
2.0
,
'Iris-virginica'
:
3.0
}
if
tab
.
has_key
(
s
):
if
tab
.
has_key
(
s
):
...
@@ -60,26 +37,31 @@ ind = permutation(ind) # random permutation
...
@@ -60,26 +37,31 @@ ind = permutation(ind) # random permutation
L
=
ind
[
0
:
90
]
# learning set indices
L
=
ind
[
0
:
90
]
# learning set indices
T
=
ind
[
90
:]
# test set indices
T
=
ind
[
90
:]
# test set indices
# Learning
S
et
# Learning
s
et
X
=
transpose
(
XC
[
L
,
0
:
4
])
X
=
transpose
(
XC
[
L
,
0
:
4
])
if
k
==
-
1
:
args
=
[
X
,
XC
[
L
,
-
1
]]
knnc
=
NNb
(
X
,
XC
[
L
,
-
1
],
k
)
if
method
==
'nnb'
:
else
:
method_class
=
classifiers
.
NNb
knnc
=
kNNb
(
X
,
XC
[
L
,
-
1
],
k
)
elif
method
==
'knnb'
:
method_class
=
classifiers
.
kNNb
args
.
append
(
k
)
elif
method
==
'mec'
:
method_class
=
classifiers
.
MEC
classifier
=
method_class
(
*
args
)
# Classification of
Test S
et
# Classification of
test s
et
c
=
zeros
(
len
(
T
))
c
=
zeros
(
len
(
T
))
for
i
in
arange
(
len
(
T
)):
for
i
in
arange
(
len
(
T
)):
c
[
i
]
=
knnc
.
classify
(
XC
[
T
[
i
],
0
:
4
])
c
[
i
]
=
classifier
.
classify
(
XC
[
T
[
i
],
0
:
4
])
# Confusion
M
atrix
# Confusion
m
atrix
CM
=
zeros
((
3
,
3
))
CM
=
zeros
((
3
,
3
))
for
i
in
range
(
3
):
for
i
in
range
(
3
):
for
j
in
range
(
3
):
for
j
in
range
(
3
):
CM
[
i
,
j
]
=
sum
(
logical_and
(
XC
[
T
,
4
]
==
i
+
1
,
c
==
j
+
1
))
CM
[
i
,
j
]
=
sum
(
logical_and
(
XC
[
T
,
4
]
==
i
+
1
,
c
==
j
+
1
))
print
CM
print
CM
# Plot
Test S
et
# Plot
test s
et
figure
(
1
)
figure
(
1
)
color
=
array
([[
1
,
0
,
0
],
[
0
,
1
,
0
],
[
0
,
0
,
1
]])
color
=
array
([[
1
,
0
,
0
],
[
0
,
1
,
0
],
[
0
,
0
,
1
]])
for
i
in
range
(
4
):
for
i
in
range
(
4
):
...
@@ -92,5 +74,10 @@ for i in range(4):
...
@@ -92,5 +74,10 @@ for i in range(4):
facecolor
=
[
1
,
1
,
1
]
*
len
(
T
))
facecolor
=
[
1
,
1
,
1
]
*
len
(
T
))
scatter
(
XC
[
T
,
i
],
XC
[
T
,
j
],
s
=
30
,
marker
=
'+'
,
scatter
(
XC
[
T
,
i
],
XC
[
T
,
j
],
s
=
30
,
marker
=
'+'
,
edgecolor
=
color
[
c
.
astype
(
int
)
-
1
])
edgecolor
=
color
[
c
.
astype
(
int
)
-
1
])
#from pylab import show
#show()
#show()
savefig
(
'knnb-%d.pdf'
%
k
)
if
method
==
'knnb'
:
filename
=
'knnb-%d.pdf'
%
k
else
:
filename
=
'%s.pdf'
%
method
savefig
(
filename
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment