Exemplo n.º 1
0
import pickle
import bayes
import imtools

# Pickleを使って2Dの点を読み込む
with open('points_normal.pkl', 'r') as f:
  class_1 = pickle.load(f)
  class_2 = pickle.load(f)
  labels = pickle.load(f)

# ベイズ分類器を学習させる
bc = bayes.BayesClassifier()
bc.train([class_1,class_2],[1,-1])

# Pickleを使ってテストデータを読み込む
with open('points_normal_test.pkl', 'r') as f:
  class_1 = pickle.load(f)
  class_2 = pickle.load(f)
  labels = pickle.load(f)

# いくつかの点についてテストする
print bc.classify(class_1[:10])[0]

# 点と判別境界を表示する
def classify0(x,y,bc=bc):
  points = vstack((x,y))
  return bc.classify(points.T)[0]

imtools.plot_2D_boundary([-6,6,-6,6],[class_1,class_2],classify0,[1,-1])
show()
from numpy import *
import pickle
import sys
sys.path.append('../ch1/')
import imtools
import bayes

# load 2D example points using Pickle
with open('points_normal.pkl', 'r') as f:
  class_1 = pickle.load(f)
  class_2 = pickle.load(f)
  labels = pickle.load(f)

# train Bayes classifier
bc = bayes.BayesClassifier()
bc.train([class_1,class_2],[1,-1])

# load test data using Pickle
with open('points_normal_test.pkl', 'r') as f:
  class_1 = pickle.load(f)
  class_2 = pickle.load(f)
  labels = pickle.load(f)
# test on some points
print bc.classify(class_1[:10])[0]
# plot points and decision boundary

def classify(x,y,bc=bc):
  points = vstack((x,y))
  return bc.classify(points.T)[0]
imtools.plot_2D_boundary([-6,6,-6,6],[class_1,class_2],classify,[1,-1])
show()
Exemplo n.º 3
0
    labels = pickle.load(f)
    
#convert to lists for libsvm

class_1 = map(list,class_1)
class_2 = map(list,class_2)
labels = list(labels)
samples = class_1+class_2

#create SVM
prob = svm_problem(labels,samples)
param = svm_parameter('-t 2')
# train SVM on data
m = svm_train(prob,param)

res = svm_predict(labels,samples,m)

with open('points_normal_test.pkl','r') as f:
    class_1 = pickle.load(f)
    class_2 = pickle.load(f)
    labels = pickle.load(f)
    
#convert to lists for libsvm
class_1 = map(list,class_1)
class_2 = map(list,class_2)
#definefunctionforplotting
def predict(x,y,model=m):
    return np.array(svm_predict([0]*len(x),zip(x,y),model)[0])
#plottheclassificationboundary
imtools.plot_2D_boundary([-6,6,-6,6],[np.array(class_1),np.array(class_2)],predict,[-1,1])
show()
Exemplo n.º 4
0
Arquivo: 0619.py Projeto: ta-oyama/PCV
# 以下で、もう一方のデータ(テスト用)を読み込んで分類器をテストする
# pickleを使ってテストデータを読み込む
with open('points_normal_test.pkl','r') as f:
    class_1 = pickle.load(f)
    class_2 = pickle.load(f)
    labels = pickle.load(f)

# いくつかの点についてテストする
print bc.classify(class_1[:10])[0]

# 点と判別境界を表示する
def classify(x,y,bc=bc):
    points = np.vstack((x,y))
    return bc.classify(points.T)[0]

imtools.plot_2D_boundary([-6,6,-6,6],[class_1,class_2],classify,[1,-1])

reload(pca)
#8.2.1 PCAで次元削減する
#まず前回使ったfeatures,test_featuresを読み込む
def read_gesture_features_labels(path):
    # .dsiftで終わるすべてのファイル名のリストを作る
    featlist = [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.dsift')]
    
    #特徴量を読み込む
    features = []
    for featfile in featlist:
        l,d = sift.read_features_from_file(featfile)
        features.append(d.flatten())
    features = np.array(features)
    
Exemplo n.º 5
0
samples = class_1+class_2 # 2つのリストを連結する

# SVMを生成する
prob = svm_problem(labels,samples)
param = svm_parameter('-t 2')

# データを使ってSVMを学習させる
m = svm_train(prob,param)

# 学習はうまくいったかな?
res = svm_predict(labels,samples,m)

# Pickleを用いてテストデータを読み込む
with open('points_ring_test.pkl', 'r') as f:
  class_1 = pickle.load(f)
  class_2 = pickle.load(f)
  labels = pickle.load(f)

# libsvm用にリストに変換する
class_1 = map(list,class_1)
class_2 = map(list,class_2)

# 描画用の関数を定義する
def predict(x,y,model=m):
  return array(svm_predict([0]*len(x),zip(x,y),model)[0])

# 分類境界を描画する
imtools.plot_2D_boundary([-6,6,-6,6],[array(class_1),array(class_2)],
                         predict,[1,-1])
show()
Exemplo n.º 6
0
labels = list(labels)
samples = class_1 + class_2  # concatenate the two lists
# create SVM

prob = svm_problem(labels, samples)
param = svm_parameter('-t 2')
# train SVM on data
m = svm_train(prob, param)
# how did the training do?
res = svm_predict(labels, samples, m)
# load test data using Pickle

with open('points_normal_test.pkl', 'r') as f:
    class_1 = pickle.load(f)
    class_2 = pickle.load(f)
    labels = pickle.load(f)

    # convert to lists for libsvm
    class_1 = map(list, class_1)
    class_2 = map(list, class_2)
# define function for plotting


def predict(x, y, model=m):
    return array(svm_predict([0] * len(x), map(list, zip(x, y)), model)[0])


# plot the classification boundary
imtools.plot_2D_boundary([-6, 6, -6, 6],
                         [array(class_1), array(class_2)], predict, [-1, 1])
show()