python+opencv+ml《Machine learing for opencv 2017》學習(一):3


3.First Steps in Supervised Learning

在這裏插入圖片描述

3.1評估的指標和函數的分類任務

在這裏插入圖片描述
在這裏插入圖片描述
直接調用sklearn.metrics的模塊函數就行:建議都寫成:(真實值y_true, 預測值y_pred)的順序

"""
分類評價
"""
import numpy as np

from sklearn import metrics


# 實際的標籤
y_true = np.array([0, 1, 0, 0, 0])

# 預測的標籤
y_pred = np.array([1, 1, 1, 1, 1])


# 四類情況:(y_true == 1)轉化爲array([False,  True, False, False, False]),相乘是元素分別相乘,然後sum統計True的個數
# 預測正確,預測爲正(因爲預測正確,自然真實同預測爲正)
true_positive = np.sum((y_pred == 1) * (y_true == 1))
# 1
# 預測正確,預測爲負
true_negative = np.sum((y_pred == 0) * (y_true == 0))
# 0
# 預測錯誤,預測爲正(因爲預測錯誤,自然真實和預測相反爲負)
false_positive = np.sum((y_pred == 1) * (y_true == 0))
# 4
# 預測錯誤,預測爲負
false_negative = np.sum((y_pred == 0) * (y_true == 1))
# 0


# accuracy_score
# 無先後要求
print(metrics.accuracy_score(y_true, y_pred))
# 0.2
"""
實際上就是
np.sum(true_positive + true_negative) / len(y_true)
np.sum(y_pred == y_true) / len(y_true)
"""

# precision_score
# (真實值, 預測值)
print(metrics.precision_score(y_true, y_pred))
# 0.2
"""
np.sum(true_positive) / np.sum(true_positive + false_positive)
"""

# recall_score
# (真實值, 預測值)
print(metrics.recall_score(y_true, y_pred))
# 1.0
"""
true_positive / (true_positive + false_negative)
"""

3.2評估的指標和函數的迴歸任務

在這裏插入圖片描述

"""
迴歸評價
"""

import numpy as np
import matplotlib.pyplot as plt
from sklearn import metrics
%matplotlib inline

# 線性點集:從0到10(包括),共100個點
x = np.linspace(0, 10, 100)

y_pred = np.sin(x)
# 製造噪聲 : np.random.rand()是[0, 1]的均勻分佈,-0.5後就是周圍的分佈[-0.5, 0.5]
y_true = np.sin(x) + np.random.rand(x.size) - 0.5

# 畫布尺寸,英寸
plt.figure(figsize=(10, 6))

# 繪製x和y上的點:linewidth寬度,label角落處的畫線的註解,'d'表示形狀菱形
plt.plot(x, y_pred, linewidth=4, label='model')
plt.plot(x, y_true, 'd', label='data')

# 在繪圖上的x軸,y軸寫註解
plt.xlabel('x')
plt.ylabel('y')

# 線條是什麼的註解寫在左下角
plt.legend(loc='lower left')


# 均方誤差:無先後要求,因爲數學上的平方
mse = metrics.mean_squared_error(y_true, y_pred)
"""
np.mean((y_true - y_pred) ** 2)
"""

# (真實值, 預測值)
metrics.explained_variance_score(y_true, y_pred)
"""
fraction_of_variance_unexplained = np.var(y_true - y_pred) / np.var(y_true)
fraction_of_variance_explained = 1- fraction_of_variance_unexplained
"""

# R方/決定係數:(真實值, 預測值)
metrics.r2_score(y_true, y_pred)
"""
1.0 - np.mean((y_true - y_pred) ** 2) / np.var(y_true)
"""

在這裏插入圖片描述

3.3.k-NN Classifier

在這裏插入圖片描述

3.3.1.實例代碼

import numpy as np
import matplotlib.pyplot as plt
import cv2
%matplotlib inline


# 生成訓練數據:num_samples是樣本的數量,num_features是每個樣本的特徵個數
def generate_data(num_samples, num_features=2):
    # 樣本數據
    data_size = (num_samples, num_features)
    # 生成[0,100)的二維整數數組
    train_data = np.random.randint(0, 100, size=data_size)

    # 樣本的標記
    labels_size = (num_samples, 1)
    # 生成[0,2)即0、1的二維整數列向量
    labels = np.random.randint(0, 2, size=labels_size)

    # 將樣本數據轉爲浮點類型:要求samples.type() == CV_32F || samples.type() == CV_32S
    return train_data.astype(np.float32), labels


# 可視化數據:一類是藍色的(label=0),另一類是紅色的(label=1)
def plot_data(all_blue, all_red):
    # 畫布尺寸,英寸
    plt.figure(figsize=(10, 6))

    # 風格
    plt.style.use('ggplot')

    # 散點:藍色用藍色blue的square方塊,紅色用紅色red的三角^
    plt.scatter(all_blue[:, 0], all_blue[:, 1], c='b', marker='s', s=180)
    plt.scatter(all_red[:, 0], all_red[:, 1], c='r', marker='^', s=180)

    # 在繪圖上的x軸,y軸寫註解
    plt.xlabel('x coordinate (feature 1)')
    plt.ylabel('y coordinate (feature 2)')
    pass


# 生成訓練樣本和標籤
train_data, labels = generate_data(11)
# 訓練樣本中藍色類的(label=0)
blue = train_data[labels.ravel() == 0]
# 訓練樣本中紅色類的(label=1)
red = train_data[labels.ravel() == 1]

# 預測數據集:我們只要生成的數據,就不關心標記了
test, _ = generate_data(2)

# 創建 k-NN classifier
knn = cv2.ml.KNearest_create()

# 訓練:因爲是一行一個數據,所以是ROW_SAMPLE
knn.train(train_data, cv2.ml.ROW_SAMPLE, labels)

# 預測:用最近的k個點,多數投票就是其分類結果
ret, results, neighbor, dist = knn.findNearest(test, 3)
print("Predicted label:\n", results)
print("Neighbor's label:\n", neighbor)
print("Distance to neighbor:\n", dist)

# 可視化結果
# 可視化樣本
plot_data(blue, red)
# 可視化預測的結果:綠色green的圓點o
plt.plot(test[0, 0], test[0, 1], 'go', markersize=14);
# 可視化預測的結果:白色white的圓點o
plt.plot(test[1, 0], test[1, 1], 'wo', markersize=14);

在這裏插入圖片描述

3.3.2.加載datasets的數字數據集

"""
加載datasets的數字數據集
"""
import numpy as np
import cv2
from sklearn import datasets
from sklearn import metrics

# 數據
digits = datasets.load_digits()

# 訓練集:樣本和標籤
# 前1000個數字,將每個圖片的64個像素點認爲是64列特徵,最後再將[0, 255]的像素範圍歸一化[0.0, 1.0]
train_data = digits.images[0:1000, :, :].astype(np.float32).reshape(1000,
                                                                    64) / 255
train_label = digits.target[0:1000].reshape(1000, 1)

# 預測集:預測數據和真實標籤
# 1000-1300的數字
test_data = digits.images[1000:1300].astype(np.float32).reshape(300, 64) / 255
test_label = digits.target[1000:1300].reshape(300, 1)



# 創建 k-NN classifier
knn = cv2.ml.KNearest_create()

# 訓練:因爲是一行一個數據,所以是ROW_SAMPLE
knn.train(train_data, cv2.ml.ROW_SAMPLE, train_label)

# 預測:用最近的50個點,多數投票就是其分類結果
ret, results, neighbor, dist = knn.findNearest(test_data, 50)
# print("Predicted label:\n", results)
# print("Neighbor's label:\n", neighbor)
# print("Distance to neighbor:\n", dist)



# accuracy_score
accuracy_score = metrics.accuracy_score(test_label, results)
print('accuracy_score', accuracy_score)
# accuracy_score 0.9433333333333334

# precision_score
precision_score = metrics.precision_score(test_label, results, average='macro')
print('precision_score', precision_score)
# precision_score 0.9451685677898378

# recall_score
recall_score = metrics.recall_score(test_label, results, average='macro')
print('recall_score', recall_score)
# recall_score 0.9432798347370092
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章