난수 행렬을 만드는 간단한 방법
난수 행렬을 만들려고하는데 내 솔루션이 너무 길고보기 흉해 보입니다.
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)]
매우 읽기 어렵고 한 줄에 맞지 않습니다.
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 ]])
다음을 삭제할 수 있습니다 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]])
사용 np.random.randint () numpy.random.random_integers로는 ()는 지원되지 않습니다
random_matrix = numpy.random.randint(min_val,max_val,(<num_rows>,<num_cols>))
Coursera Machine Learning Neural Network 연습의 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)
임의의 정수 배열을 만드는 간단한 방법은 다음과 같습니다.
matrix = np.random.randint(maxVal, size=(rows, columns))
다음은 0에서 10까지의 임의 정수로 구성된 2 x 3 행렬을 출력합니다.
a = np.random.randint(10, size=(2,3))
random_matrix = [[random.random for j in range(collumns)] for i in range(rows)
for i in range(rows):
print random_matrix[i]
x = np.int_(np.random.rand(10) * 10)
10 개 중 난수를 위해. 20 개 중 20 개를 곱해야합니다.
"난수 행렬"이라고 말하면 위에서 언급 한 Pavel https://stackoverflow.com/a/15451997/6169225 로 numpy를 사용할 수 있습니다 .이 경우이 분포 (의사)와 관련이 없다고 가정합니다. ) 난수를 준수합니다.
However, if you require a particular distribution (I imagine you are interested in the uniform distribution), numpy.random has very useful methods for you. For example, let's say you want a 3x2 matrix with a pseudo random uniform distribution bounded by [low,high]. You can do this like so:
numpy.random.uniform(low,high,(3,2))
Note, you can replace uniform by any number of distributions supported by this library.
Further reading: https://docs.scipy.org/doc/numpy/reference/routines.random.html
An answer using map-reduce:-
map(lambda x: map(lambda y: ran(),range(len(inputs[0]))),range(hiden_neurons))
#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
참고URL : https://stackoverflow.com/questions/15451958/simple-way-to-create-matrix-of-random-numbers
'IT TIP' 카테고리의 다른 글
| 클래스의 공용 메서드에 대해 "사용되지 않음"경고 비활성화 (0) | 2020.10.31 |
|---|---|
| 입력이 기본 64가 아닌 문자를 포함하므로 유효한 Base-64 문자열이 아닙니다. (0) | 2020.10.31 |
| DbSet없는 원시 SQL 쿼리-Entity Framework Core (0) | 2020.10.31 |
| 파이썬에서 sendmail을 통해 메일 보내기 (0) | 2020.10.31 |
| 동적 변수 개수가있는 공식 (0) | 2020.10.31 |