ナイーブベイズ(Naive Bayse)

v3.0.0

In [42]:
# -*- coding: utf-8 -*-
import cv2
import numpy as np

#Ipythonで表示用の設定
import matplotlib.pyplot as plt
%matplotlib inline

TrainData = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]).astype(np.float32)
response  = np.array([1, 1, 1, 2, 2, 2]).astype(np.int)

#学習データの表示
print "Training Data\nlabel: feature"
for i in range(6):
   print response[i],":",TrainData[i]

#学習器を生成
NBC = cv2.ml.NormalBayesClassifier_create()

#学習フェーズ
NBC.train(TrainData, cv2.ml.ROW_SAMPLE, response)

#推定対象生成
test = np.array([[-1.5, -0.1]]).astype(np.float32)

#推定フェーズ
(ret, res) = NBC.predict(test) 

print "\nEstimate Result"
print test,"->",res
Training Data
label: feature
1 : [-1. -1.]
1 : [-2. -1.]
1 : [-3. -2.]
2 : [ 1.  1.]
2 : [ 2.  1.]
2 : [ 3.  2.]

Estimate Result
[[-1.5 -0.1]] -> [[1]]

inserted by FC2 system