numpy隨機索引(不重復)和打亂元素


def getRandomIndex(n, x):
	# 索引范圍為[0, n),隨機選x個不重復,注意replace=False才是不重復,replace=True則有可能重復
    index = np.random.choice(np.arange(n), size=x, replace=False)
    return index

getRandomIndex(12, 12)
Out[13]: array([11, 6, 10, 8, 0, 2, 9, 3, 4, 7, 1, 5])

shuffle_indexes=np.random.permutation(len(X))
test_ratio=0.2
test_size=int(len(X)*test_ratio)     #規定測試集個數

test_indexes=shuffle_indexes[:test_size]  #測試集索引
train_indexes=shuffle_indexes[test_size:] #訓練集索引

讓列表元素隨機

np.random.shuffle(examples)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM