亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python中np.random.permutation函數(shù)實(shí)例詳解

 更新時(shí)間:2023年04月01日 10:58:24   作者:<阿睿>  
np.random.permutation是numpy中的一個(gè)函數(shù),它可以將一個(gè)數(shù)組中的元素隨機(jī)打亂,返回一個(gè)打亂后的新數(shù)組,下面這篇文章主要給大家介紹了關(guān)于python中np.random.permutation函數(shù)的相關(guān)資料,需要的朋友可以參考下

一:函數(shù)介紹

np.random.permutation() 總體來(lái)說(shuō)他是一個(gè)隨機(jī)排列函數(shù),就是將輸入的數(shù)據(jù)進(jìn)行隨機(jī)排列,官方文檔指出,此函數(shù)只能針對(duì)一維數(shù)據(jù)隨機(jī)排列,對(duì)于多維數(shù)據(jù)只能對(duì)第一維度的數(shù)據(jù)進(jìn)行隨機(jī)排列。

簡(jiǎn)而言之:np.random.permutation函數(shù)的作用就是按照給定列表生成一個(gè)打亂后的隨機(jī)列表

在處理數(shù)據(jù)集時(shí),通??梢允褂迷摵瘮?shù)進(jìn)行打亂數(shù)據(jù)集內(nèi)部順序,并按照同樣的順序進(jìn)行標(biāo)簽序列的打亂。

二:實(shí)例

2.1 直接處理數(shù)組或列表數(shù)

import numpy as np

data = np.array([1,2,3,4,5,6,7])
a = np.random.permutation(data)
b = np.random.permutation([5,0,9,0,1,1,1])
print(a)
print( "data:", data )
print(b)

2.2 間接處理:不改變?cè)瓟?shù)據(jù)(對(duì)數(shù)組下標(biāo)的處理)

label = np.array([1,2,3,4,5,6,7])
a = np.random.permutation(np.arange(len(label)))
print("Label[a] :" ,label[a] )

補(bǔ):一般只能用于N維數(shù)組 只能將整數(shù)標(biāo)量數(shù)組轉(zhuǎn)換為標(biāo)量索引

why?label1[a1]  label1是列表,a1是列表下標(biāo)的隨機(jī)排列 但是! 列表結(jié)構(gòu)沒(méi)有標(biāo)量索引 label1[a1]報(bào)錯(cuò)

label1=[1,2,3,4,5,6,7]
print(len(label1))

a1 = np.random.permutation(np.arange(len(label1)))#有結(jié)果

print(a1)

print("Label1[a1] :" ,label1[a1] )#這列表結(jié)構(gòu)沒(méi)有標(biāo)量索引 所以會(huì)報(bào)錯(cuò)

2.3 實(shí)例:鳶尾花數(shù)據(jù)中對(duì)鳶尾花的隨機(jī)打亂(可以直接用)

from sklearn import svm
from sklearn import datasets #sklearn 的數(shù)據(jù)集
iris = datasets.load_iris()
iris_x = iris.data
iris_y = iris.target
indices = np.random.permutation(len(iris_x))

#此時(shí) 打亂的是數(shù)組的下標(biāo)的排序
print(indices)
print(indices[:-10])#到倒數(shù)第10個(gè)為止
print(indices[-10:])#最后10個(gè)

# print(type(iris_x))?? <class 'numpy.ndarray'>

#9:1分類
#iris_x_train = iris_x[indices[:-10]]#使用的數(shù)組打亂后的下標(biāo)
#iris_y_train = iris_y[indices[:-10]]
#iris_x_test= iris_x[indices[-10:]]
#iris_y_test= iris_y[indices[-10:]]????

數(shù)組下標(biāo) 即標(biāo)量索引的重新分布情況: 下標(biāo)是0開始

總結(jié)

到此這篇關(guān)于python中np.random.permutation函數(shù)的文章就介紹到這了,更多相關(guān)python np.random.permutation函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論