랜덤 토플 리츠 행렬을 만들어 역전 가능성을 추정합니다. 현재 코드는랜덤 행렬 계산 속도 향상
import random
from scipy.linalg import toeplitz
import numpy as np
for n in xrange(1,25):
rankzero = 0
for repeats in xrange(50000):
column = [random.choice([0,1]) for x in xrange(n)]
row = [column[0]]+[random.choice([0,1]) for x in xrange(n-1)]
matrix = toeplitz(column, row)
if (np.linalg.matrix_rank(matrix) < n):
rankzero += 1
print n, (rankzero*1.0)/50000
이 가능합니까?
정확도를 높이기 위해 50000 값을 늘리고 싶지만 현재 너무 느립니다. 사용하여 프로파일
은 for n in xrange(10,14)
400000 9.482 0.000 9.482 0.000 {numpy.linalg.lapack_lite.dgesdd}
4400000 7.591 0.000 11.089 0.000 random.py:272(choice)
200000 6.836 0.000 10.903 0.000 index_tricks.py:144(__getitem__)
1 5.473 5.473 62.668 62.668 toeplitz.py:3(<module>)
800065 4.333 0.000 4.333 0.000 {numpy.core.multiarray.array}
200000 3.513 0.000 19.949 0.000 special_matrices.py:128(toeplitz)
200000 3.484 0.000 20.250 0.000 linalg.py:1194(svd)
6401273/64.421 0.000 2.421 0.000 {len}
200000 2.252 0.000 26.047 0.000 linalg.py:1417(matrix_rank)
4400000 1.863 0.000 1.863 0.000 {method 'random' of '_random.Random' objects}
2201015 1.240 0.000 1.240 0.000 {isinstance}
[...]
대단히 감사합니다. 혹시 랭킹이 det보다 빠르면 어떤 생각이 드나요? 아주 작은 것, 5000은 하단의 50000과 일치해야합니다. – marshall
det() vs rank() - CPU에 따라 다를 수 있습니다. 작은 테스트를하는 것이 좋습니다. timeit det (np.random.randint (0,2, size = (25,25)) 대 % timeit matrix_rank (np.random.randint (0,2, size = (25,25)) 5000과 50000에 대해서는 의도적으로 더 쉽게 테스트 할 수 있도록 작게 만들었습니다. –
det (np.random.randint (0,2, size = (25,25)))는 약 42 us이고 matrix_rank (np .random.randint (0,2, size = (25,25)))는 약 190 우리가 꽤 명확하다. – marshall