簡體   English   中英

創建隨機數矩陣的簡單方法

[英]Simple way to create matrix of random numbers

我正在嘗試創建一個隨機數矩陣,但我的解決方案太長而且看起來很丑

random_matrix = [[random.random() for e in range(2)] for e in range(3)]

這看起來不錯,但在我的實現中它是

weights_h = [[random.random() for e in range(len(inputs[0]))] for e in range(hiden_neurons)]

這是非常不可讀的,不適合一行。

您可以刪除range(len())

weights_h = [[random.random() for e in inputs[0]] for e in range(hiden_neurons)]

但實際上,您可能應該使用 numpy。

In [9]: numpy.random.random((3, 3))
Out[9]:
array([[ 0.37052381,  0.03463207,  0.10669077],
       [ 0.05862909,  0.8515325 ,  0.79809676],
       [ 0.43203632,  0.54633635,  0.09076408]])

看看numpy.random.rand

文檔字符串:rand(d0, d1, ..., dn)

給定形狀的隨機值。

創建一個給定形狀的數組,並使用來自[0, 1)均勻分布的隨機樣本進行傳播。


>>> import numpy as np
>>> np.random.rand(2,3)
array([[ 0.22568268,  0.0053246 ,  0.41282024],
       [ 0.68824936,  0.68086462,  0.6854153 ]])

使用np.random.randint()作為np.random.random_integers()已棄用

random_matrix = np.random.randint(min_val,max_val,(<num_rows>,<num_cols>))

看起來您正在執行 Coursera 機器學習神經網絡練習的 Python 實現。 這是我為 randInitializeWeights(L_in, L_out) 所做的

#get a random array of floats between 0 and 1 as Pavel mentioned 
W = numpy.random.random((L_out, L_in +1))

#normalize so that it spans a range of twice epsilon
W = W * 2 * epsilon

#shift so that mean is at zero
W = W - epsilon

首先,創建numpy數組,然后將其轉換為matrix 請參閱下面的代碼:

import numpy

B = numpy.random.random((3, 4)) #its ndArray
C = numpy.matrix(B)# it is matrix
print(type(B))
print(type(C)) 
print(C)

為了創建隨機數數組,NumPy 提供了使用以下方法創建數組:

  1. 實數

  2. 整數

使用隨機實數創建數組有 2 個選項

  1. random.rand(用於生成隨機數的均勻分布)
  2. random.randn(用於生成隨機數的正態分布)

隨機數

import numpy as np 
arr = np.random.rand(row_size, column_size) 

隨機數

import numpy as np 
arr = np.random.randn(row_size, column_size) 

使用隨機整數創建數組

import numpy as np
numpy.random.randint(low, high=None, size=None, dtype='l')

在哪里

  • 低 = 要從分布中提取的最低(有符號)整數
  • 高(可選)= 如果提供,則高於要從分布中提取的最大(有符號)整數
  • size(optional) = 輸出形狀,即如果給定的形狀是,例如 (m, n, k),則繪制 m * n * k 個樣本
  • dtype(optional) = 結果的所需 dtype。

例如:

給定的示例將生成一個 0 到 4 之間的隨機整數數組,其大小為 5*5 並且有 25 個整數

arr2 = np.random.randint(0,5,size = (5,5))

為了創建 5 x 5 矩陣,應將其修改為

arr2 = np.random.randint(0,5,size = (5,5)),將乘號*改為逗號,#

[[2 1 1 0 1][3 2 1 4 3][2 3 0 3 3][1 3 1 0 0][4 1 2 0 1]]

例2:

給定的示例將生成一個介於 0 和 1 之間的隨機整數數組,其大小為 1*10 並且有 10 個整數

arr3= np.random.randint(2, size = 10)

[0 0 0 0 1 1 0 0 1 1]

x = np.int_(np.random.rand(10) * 10)

對於 10 中的隨機數。 對於 20 中的隨機數,我們必須乘以 20。

當您說“隨機數矩陣”時,您可以將 numpy 用作上面提到的 Pavel https://stackoverflow.com/a/15451997/6169225 ,在這種情況下,我假設對您而言這些分布無關緊要(偽) 隨機數堅持。

但是,如果您需要特定的分布(我想您對均勻分布感興趣), numpy.random為您提供了非常有用的方法。 例如,假設您想要一個具有以 [low,high] 為界的偽隨機均勻分布的 3x2 矩陣。 你可以這樣做:

numpy.random.uniform(low,high,(3,2))

請注意,您可以使用此庫支持的任意數量的分布替換uniform

進一步閱讀:https ://docs.scipy.org/doc/numpy/reference/routines.random.html

創建隨機整數數組的一種簡單方法是:

matrix = np.random.randint(maxVal, size=(rows, columns))

下面輸出一個由 0 到 10 的隨機整數組成的 2 x 3 矩陣:

a = np.random.randint(10, size=(2,3))

使用 map-reduce 的答案:-

map(lambda x: map(lambda y: ran(),range(len(inputs[0]))),range(hiden_neurons))
random_matrix = [[random.random for j in range(collumns)] for i in range(rows)
for i in range(rows):
    print random_matrix[i]
#this is a function for a square matrix so on the while loop rows does not have to be less than cols.
#you can make your own condition. But if you want your a square matrix, use this code.

import random

import numpy as np

def random_matrix(R, cols):

        matrix = []

        rows =  0

        while  rows < cols:

            N = random.sample(R, cols)

            matrix.append(N)

            rows = rows + 1

    return np.array(matrix)

print(random_matrix(range(10), 5))
#make sure you understand the function random.sample

numpy.random.rand(row, column) 根據給定的指定 (m,n) 參數生成 0 到 1 之間的隨機數。 因此,使用它來創建一個 (m,n) 矩陣並乘以范圍限制的矩陣並將其與上限相加。

分析:如果生成零,則僅保持下限,但如果生成一,則僅保持上限。 換句話說,使用 rand numpy 生成限制可以生成極端所需的數字。

import numpy as np

high = 10
low = 5
m,n = 2,2

a = (high - low)*np.random.rand(m,n) + low

輸出:

a = array([[5.91580065, 8.1117106 ],
          [6.30986984, 5.720437  ]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM