python_funtion

python 반복문

명징직조지훈 2022. 11. 3. 15:25

행렬 생성과 이어서

2022.11.03 - [python_funtion] - numpy random 행렬 생성과 모양 변경

 

numpy random 행렬 생성과 모양 변경

numpy.random.random() : 0, 1 사이의 난수 생성 reshape 를 통한 원하는 크기의 행렬로의 변환 import numpy as np a = np.random.random(16) a >>> array([0.78823164, 0.40877064, 0.49785185, 0.64090304, 0.88461623, 0.09129619, 0.92878012, 0.4

teach-meaning.tistory.com

in 을 통해 리스트의 요소들을 방문할 수 있다.

input = np.random.random(16).reshape(4,4)
input
>>>
array([[0.19540161, 0.24351475, 0.46340701, 0.5057199 ],
       [0.74607146, 0.82589344, 0.96936138, 0.16499607],
       [0.23055915, 0.14391571, 0.597096  , 0.31677631],
       [0.59643783, 0.93654069, 0.16791685, 0.07300657]])
       
for i in(input):
  print(i)
>>>
[0.19540161 0.24351475 0.46340701 0.5057199 ]
[0.74607146 0.82589344 0.96936138 0.16499607]
[0.23055915 0.14391571 0.597096   0.31677631]
[0.59643783 0.93654069 0.16791685 0.07300657]

또는 range 함수를 통한 반복 횟수의 지정 가능

for i in range(input.shape[0]):
  print(i, input[i])
>>>
0 [0.19540161 0.24351475 0.46340701 0.5057199 ]
1 [0.74607146 0.82589344 0.96936138 0.16499607]
2 [0.23055915 0.14391571 0.597096   0.31677631]
3 [0.59643783 0.93654069 0.16791685 0.07300657]