NumPy 기초 과제


개요

NumPy 기초 과제
import numpy as np

np.random.seed(1)
A = np.random.random((10,15))
B = np.random.random((10,15))

문제

  1. What is the value of the second row and third column of A?
  2. What is the average value of the fifth row of A?
  3. What is the sum of all elements of A-B?
  4. What is the size(not shape) of AB'?
  5. What is the sum of the second column of BA'?
  6. What is the sum of diagonals of BA'?

풀이

import numpy as np

np.random.seed(1)
A = np.random.random((10,15))
B = np.random.random((10,15))

#### 1. What is the value of the second row and third column of A?
print( A[1,2] )
# 0.558689828446

#### 2. What is the average value of the fifth row of A?
print( A[4].mean() )
print( np.average(A[4]) )
print( A.mean(axis=1)[4] )
# 0.487058290328

#### 3. What is the sum of all elements of A-B?
print( (A-B).sum() )
print( A.sum() - B.sum() )
# -8.23869499888

#### 4. What is the size(not shape) of AB'?
print( A.dot(B.T).size )
print( np.dot(A, B.T).size )
# 100

#### 5. What is the sum of the second column of BA'?
print( np.dot(B, A.T).sum(axis=0)[1] )
# 40.964734835

#### 6. What is the sum of diagonals of BA'?
print( np.dot(B, A.T).diagonal().sum() )
# 39.7123707064

같이 보기