-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathknn.py~
41 lines (35 loc) · 1.2 KB
/
knn.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
import os
from PIL import Image
import sys
from optparse import OptionParser
import numpy as np
from numpy import *
from mvpa.clfs.knn import kNN
from mvpa.datasets import Dataset
#pymvpa stuff
f_handle = open("classdatafile.txt", 'r')
f_handle2 = open("classidfile.txt", 'r')
f_handle3 = open("predictdata.txt", 'r')
features = genfromtxt(f_handle, dtype = float)
classes = genfromtxt(f_handle2, dtype = int)
predictdata = genfromtxt(f_handle3, dtype = float)
predictdata = np.expand_dims(predictdata, axis=0)
print predictdata
print np.shape(features), features.ndim, features.dtype
print np.shape(classes), classes.ndim, classes.dtype
print np.shape(predictdata), predictdata.ndim, predictdata.dtype
f_handle.close()
f_handle2.close()
f_handle3.close()
training = Dataset(samples=features,labels=classes)
clf = kNN(k=2)
print "clf = ", clf
clf.train(training)
#print np.mean(clf.predict(training.samples) == training.labels)
classID = clf.predict(predictdata)
print "classID = ", classID
#print clf.trained_labels
if classID[0] == 1: print "Image is of class: GRASS"
if classID[0] == 2: print "Image is of class: DIRT/GRAVEL"
if classID[0] == 3: print "Image is of class: CEMENT/ASPHALT"