使用numpy.eye創(chuàng)建one-hot編碼的實(shí)現(xiàn)
一、np.eye說明
np.eye 是 NumPy 中的一個(gè)函數(shù),用于創(chuàng)建一個(gè)二維數(shù)組,其中對(duì)角線上為1,其余元素為0。它通常用于生成單位矩陣或?qū)蔷仃?。其基本用法如下?/p>
matrix = np.eye(3)
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
np.eye 函數(shù)的主要參數(shù)有:
N:生成的矩陣的行數(shù)。
M:生成的矩陣的列數(shù)(可選,默認(rèn)為 N)。
k:對(duì)角線的索引(可選,默認(rèn)為0,即主對(duì)角線,k>0 為上對(duì)角線,k<0 為下對(duì)角線)。
dtype:數(shù)組的數(shù)據(jù)類型(可選,默認(rèn)為 float)。
例如,創(chuàng)建一個(gè) 3x3 的單位矩陣并將對(duì)角線向上移動(dòng)一行:
matrix = np.eye(3, k=1)
[[0. 1. 0.] [0. 0. 1.] [0. 0. 0.]]
二、獨(dú)熱編碼
假設(shè)Y_test_orig為一個(gè)一行多列的二維矩陣,例如:[ [0 0 0 5 1 0 3 1 5 1 5 1 ] ]
np.eye(6)[Y_test_orig.reshape(-1)]
創(chuàng)建和使用 one-hot 編碼。分解一下這個(gè)表達(dá)式:
1、np.eye(6) 創(chuàng)建了一個(gè) 6x6 的單位矩陣(對(duì)角矩陣),其中對(duì)角線上的元素為 1,其余元素為 0。
例如:
import numpy as np identity_matrix = np.eye(6) print(identity_matrix)
[[1. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0.] [0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 1. 0.] [0. 0. 0. 0. 0. 1.]]
Y_test_orig.reshape(-1)
將 Y_test_orig 重新調(diào)整為一維數(shù)組。
Y_test_orig = np.array([[0], [1], [2], [3], [4], [5]]) reshaped = Y_test_orig.reshape(-1) print(reshaped)
[0 1 2 3 4 5]
3、np.eye(6)[Y_test_orig.reshape(-1)]
使用重新調(diào)整后的 Y_test_orig 數(shù)組作為索引,來選擇單位矩陣中的相應(yīng)行
。這將生成一個(gè) one-hot 編碼矩陣,其中每個(gè)類標(biāo)簽都被編碼為一個(gè) one-hot 向量。
Y_test_orig = np.array([[0], [1], [2], [3], [4], [5]]) one_hot_encoded = np.eye(6)[Y_test_orig.reshape(-1)] print(one_hot_encoded)
[[1. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0.] [0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 1. 0.] [0. 0. 0. 0. 0. 1.]]
如果Y_test_orig_test為[ [0 0 0 5 1 0 3 1 5 1 5 1 ] ],那么結(jié)果將會(huì)是:
[[1. 0. 0. 0. 0. 0.] [1. 0. 0. 0. 0. 0.] [1. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 1.] [0. 1. 0. 0. 0. 0.] [1. 0. 0. 0. 0. 0.] [0. 0. 0. 1. 0. 0.] [0. 1. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 1.] [0. 1. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 1.] [0. 1. 0. 0. 0. 0.]]
再轉(zhuǎn)置一下,np.eye(6)[Y_test_orig.reshape(-1)].T
就可以為機(jī)器學(xué)習(xí)所用了:
[[1. 1. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 1. 0. 0. 1. 0. 1. 0. 1.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 1. 0.]]
故獨(dú)熱編碼函數(shù):C為類別數(shù),Y為原始二維矩陣如:[ [0 0 0 5 1 0 3 1 5 1 5 1 ] ]
def convert_to_one_hot(Y, C): Y = np.eye(C)[Y.reshape(-1)].T return Y
到此這篇關(guān)于使用numpy.eye創(chuàng)建one-hot編碼的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)numpy.eye創(chuàng)建one-hot編碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用pptx實(shí)現(xiàn)復(fù)制頁面到其他PPT中
這篇文章主要為大家詳細(xì)介紹了python如何使用pptx庫實(shí)現(xiàn)從一個(gè)ppt復(fù)制頁面到另一個(gè)ppt里面,文中的示例代碼講解詳細(xì),感興趣的可以嘗試一下2023-02-02Python利用matplotlib生成圖片背景及圖例透明的效果
這篇文章主要給大家介紹了Python利用matplotlib生成圖片背景及圖例透明效果的相關(guān)資料,文中給出了詳細(xì)的示例代碼,相信對(duì)大家具有一定的參考家價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-04-04Python對(duì)ElasticSearch獲取數(shù)據(jù)及操作
這篇文章主要為大家詳細(xì)介紹了Python對(duì)ElasticSearch獲取數(shù)據(jù)及操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04解決jupyter notebook圖片顯示模糊和保存清晰圖片的操作
這篇文章主要介紹了解決jupyter notebook圖片顯示模糊和保存清晰圖片的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04python環(huán)境下OPenCV處理視頻流局部區(qū)域像素值
這篇文章主要為大家介紹了python環(huán)境下OPenCV處理視頻流局部區(qū)域像素值的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11