Python basics 8 numpy test

1.First of all

Keep the basics of numpy in test format as a memorandum. It is assumed that ** numpy ** has been imported in advance.

Question_01 Question

Function function that generates B from A(A)Write

A:
array([[5, 3, 4, 3],
       [7, 6, 7, 4],
       [6, 7, 5, 4]])
B:
array([[False, False, False, False],
       [False, False, False, False],
       [False, False, False, False]])

Answer

def function(A):
    return A > 7

Question_02 Question

Function function that generates B from A(A)Write

A:
array([4, 5, 4, 5, 4, 3, 2])

B:
array([2, 3, 4, 4, 4, 5, 5])

Answer

def function(A):
    return np.sort(A)

Question_03 Question

Function function that generates B from A(A)Write

A:
array([[ 1,  1, 19],
       [ 7,  3,  6],
       [16,  1, 11]])
B:
array([ 1,  7, 16,  1,  3,  1, 19,  6, 11])

Answer

def function(A):
    return A.T.flatten()

Question_04 Question

Function function that generates B from A(A)Write

A:
array([1, 5, 4, 2, 8, 2, 9])

B:
array([1, 2, 2, 4, 5, 8, 9])

Answer

def function(A):
    return np.sort(A)

Question_05 Question

A,Function function that generates C from B(A, B)Write

A:
array([[1, 3, 1],
       [3, 3, 2],
       [2, 2, 3]])
B:
array([[2, 3, 1],
       [2, 3, 1],
       [3, 3, 2]])
C:
array([[11, 15,  6],
       [18, 24, 10],
       [17, 21, 10]])

Answer

def function(A, B):
    return np.dot(A, B)

Question_06 Question

A,Function function that generates C from B(A, B)Write

A:
array([8, 0, 3, 0, 3, 0, 0, 5, 6, 8, 5, 8, 6, 3, 3])

B:
array([3, 1, 8, 4, 5, 4, 5, 5, 2, 5, 4, 0, 7, 3, 7])

C:
array([[24,  0, 24,  0, 15],
       [ 0,  0, 25, 12, 40],
       [20,  0, 42,  9, 21]])

Answer

def function(A, B):
    return np.reshape(A*B, (3, 5))

Question_07 Question

Function function that generates B from A(A)Write

A:
array([[2, 1, 2],
       [5, 5, 1],
       [5, 1, 1]])
B:
array([[-1, -2, -1],
       [ 2,  2, -2],
       [ 2, -2, -2]])

Answer

def function(A):
    return A-3

Question_08 Question

A,Function function that generates C from B(A, B)Write

A:
array([[6, 8, 3, 2, 6, 6],
       [3, 2, 2, 7, 1, 0],
       [6, 2, 5, 8, 2, 1],
       [3, 3, 5, 5, 8, 7],
       [3, 6, 6, 8, 4, 6],
       [8, 7, 6, 4, 2, 1]])
B:
array([[2, 6, 2, 6, 0, 0],
       [7, 4, 5, 4, 2, 5],
       [8, 5, 7, 2, 6, 0],
       [6, 4, 3, 1, 3, 2],
       [3, 8, 1, 4, 0, 0],
       [3, 4, 3, 1, 6, 2]])
C:
array([[6, 8, 3, 2, 6, 6, 2, 6, 2, 6, 0, 0],
       [3, 2, 2, 7, 1, 0, 7, 4, 5, 4, 2, 5],
       [6, 2, 5, 8, 2, 1, 8, 5, 7, 2, 6, 0],
       [3, 3, 5, 5, 8, 7, 6, 4, 3, 1, 3, 2],
       [3, 6, 6, 8, 4, 6, 3, 8, 1, 4, 0, 0],
       [8, 7, 6, 4, 2, 1, 3, 4, 3, 1, 6, 2]])

Answer

def function(A, B):
    return np.hstack([A, B])

Question_09 Question

Function function that generates B from A(A)Write

A:
array([[2, 5, 7, 5, 8, 3, 7],
       [8, 3, 0, 0, 7, 5, 0],
       [2, 3, 1, 6, 7, 4, 0],
       [8, 3, 8, 7, 8, 2, 4],
       [3, 7, 5, 1, 6, 5, 2],
       [7, 2, 3, 5, 7, 1, 6],
       [3, 7, 5, 8, 1, 1, 2]])
B:
array([[8],
       [8],
       [7],
       [8],
       [7],
       [7],
       [8]])

Answer

def function(A):
    return np.reshape(np.max(A, axis=1), (7,1))

Question_10 Question

Function function that generates B from A(A)Write

A:
array([8, 4, 7, 8, 2, 8, 6, 2, 1, 3, 5, 7, 6, 0, 3])

B:
array([[64, 16, 49],
       [64,  4, 64],
       [36,  4,  1],
       [ 9, 25, 49],
       [36,  0,  9]])

Answer

def function(A):
    return np.reshape(A**2, (5,3))

Question_11 Question

A,Function function that generates C from B(A, B)Write

A:
array([[5, 5, 4, 5, 6, 4],
       [7, 2, 4, 3, 8, 8],
       [4, 7, 4, 2, 5, 8],
       [7, 1, 6, 5, 5, 3],
       [4, 6, 5, 8, 6, 6],
       [2, 2, 7, 5, 8, 1]])
B:
array([[7, 6, 6, 6, 1, 8],
       [3, 5, 5, 1, 6, 5],
       [7, 4, 8, 4, 8, 3],
       [6, 4, 5, 6, 8, 4],
       [5, 4, 1, 5, 4, 6],
       [6, 2, 5, 3, 1, 3]])
C:
array([[35, 30, 24, 30,  6, 32],
       [21, 10, 20,  3, 48, 40],
       [28, 28, 32,  8, 40, 24],
       [42,  4, 30, 30, 40, 12],
       [20, 24,  5, 40, 24, 36],
       [12,  4, 35, 15,  8,  3]])

Answer

def function(A, B):
    return A*B

Question_12 Question

Function function that generates B from A(A)Write

A:
array([[6, 0, 6, 6, 8, 0, 2],
       [2, 5, 5, 8, 8, 4, 3],
       [5, 1, 0, 4, 0, 4, 6],
       [3, 1, 1, 7, 4, 7, 7],
       [3, 4, 5, 8, 0, 8, 0],
       [2, 5, 6, 8, 4, 6, 0],
       [0, 5, 1, 3, 6, 1, 6]])
B:
array([[21, 21, 24, 44, 30, 30, 24]])

Answer

def function(A):
    return np.sum(A, axis=0)

Question_13 Question

A,Function function that generates C from B(A, B)Write

A:
array([[60, 40, 70, 30, 70, 50, 40],
       [20, 30, 40, 50, 10, 20, 20],
       [80, 20, 40, 10, 80, 50, 60],
       [90, 70, 40, 60, 80, 80, 80],
       [10, 40, 60, 50, 50, 60, 10],
       [60, 20, 40, 40, 10, 90, 90]])
B:
array([[7, 7, 1, 2, 3, 5, 8]])

C:
array([[67, 47, 71, 32, 73, 55, 48],
       [27, 37, 41, 52, 13, 25, 28],
       [87, 27, 41, 12, 83, 55, 68],
       [97, 77, 41, 62, 83, 85, 88],
       [17, 47, 61, 52, 53, 65, 18],
       [67, 27, 41, 42, 13, 95, 98]])

Answer

def function(A, B):
    return A+B

Question_14 Question

Function function that generates B from A(A)Write

A:
array([[2, 3, 3, 8],
       [2, 5, 2, 1],
       [0, 1, 8, 1],
       [3, 7, 0, 1]])
B:
array([[2, 2, 0, 3],
       [3, 5, 1, 7],
       [3, 2, 8, 0],
       [8, 1, 1, 1]])

Answer

def function(A):
    return A.T

Question_15 Question

Function function that generates A()Write

A:
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])

Answer

def function():
    return np.zeros((6, 7))

Question_16 Question

Function function that generates B from A(A)Write
 
A:
array([3, 8, 4, 7, 5])

B:
array([7, 5, 3, 8, 4])

Answer

def function(A):
    return A[[3, 4, 0, 1, 2 ]]

Question_17 Question

Function function that generates B from A(A)Write

A:
array([[5, 7, 4, 6, 3],
       [1, 3, 0, 6, 4],
       [6, 3, 6, 4, 6],
       [2, 6, 3, 5, 0],
       [7, 8, 4, 0, 3],
       [6, 3, 7, 1, 5],
       [2, 7, 1, 4, 7]])
B:
array([[3],
       [0],
       [3],
       [0],
       [0],
       [1],
       [1]])

Answer

def function(A):
    return np.reshape(np.min(A, axis=1), (7,1))

Question_18

A,Function function that generates C from B(A, B)Write

A:
array([[8, 0, 4, 7, 5, 4],
       [8, 7, 0, 2, 8, 7],
       [3, 2, 3, 6, 6, 0],
       [0, 2, 7, 5, 7, 4],
       [5, 7, 5, 5, 6, 5]])
B:
array([[1, 7, 2, 1, 2, 1],
       [2, 6, 6, 1, 5, 4],
       [8, 8, 7, 1, 4, 3],
       [3, 3, 2, 0, 5, 0],
       [6, 1, 6, 0, 8, 4]])
C:
array([[8, 0, 4, 7, 5, 4],
       [8, 7, 0, 2, 8, 7],
       [3, 2, 3, 6, 6, 0],
       [0, 2, 7, 5, 7, 4],
       [5, 7, 5, 5, 6, 5],
       [1, 7, 2, 1, 2, 1],
       [2, 6, 6, 1, 5, 4],
       [8, 8, 7, 1, 4, 3],
       [3, 3, 2, 0, 5, 0],
       [6, 1, 6, 0, 8, 4]])

Answer

def function(A, B):
    return np.vstack([A, B])

Question_19

A,Function function that generates C from B(A, B)Write

A:
array([[70, 50, 60, 20, 30, 70, 20],
       [10, 10, 80, 50, 30, 60, 50],
       [60, 40, 90, 40, 10, 20, 40],
       [40, 80, 90, 20, 50, 20, 80],
       [90, 10, 20, 40, 40, 40, 20]])

B:
array([[1, 2, 2, 2, 3, 8, 1]])

C:
array([[ 70, 100, 120,  40,  90, 560,  20],
       [ 10,  20, 160, 100,  90, 480,  50],
       [ 60,  80, 180,  80,  30, 160,  40],
       [ 40, 160, 180,  40, 150, 160,  80],
       [ 90,  20,  40,  80, 120, 320,  20]])

Answer

def function(A, B):
    return A*B

Question_20

A,Function function that generates C from B(A, B)Write

A:
array([0, 6, 2, 2, 5, 8, 3, 3, 6, 6, 1, 3, 5, 3, 3, 1, 4, 4, 5, 1])

B:
array([3, 5, 4, 8, 6, 4, 7, 2, 3, 5, 6, 7, 4, 6, 8, 8, 6, 4, 2, 8])

C:
array([[-3,  1, -2, -6, -1],
       [ 4, -4,  1,  3,  1],
       [-5, -4,  1, -3, -5],
       [-7, -2,  0,  3, -7]])

Answer

def function(A, B):
    return np.reshape(A-B, (4, 5))

Recommended Posts

Python basics 8 numpy test
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
Python #Numpy basics
Python basics ⑤
Python basics
NumPy basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
My Numpy (Python)
Python basics: list
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
numpy unit test
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
Python Integrity Test
#Python basics (functions)
Python array basics
Python profiling basics
Python basics: functions
#Python basics (class)
Python basics summary
[Python] Numpy memo
Primality test by Python
Primality test with Python
Python and numpy tips
Python basics ② for statement
Python: Unsupervised Learning: Basics
Basics of Python scraping basics
Python test package memo
Errbot: Python chatbot basics
Primality test with python
[Python] Search (NumPy) ABC165C
python numpy array calculation
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
[Python] Sorting Numpy data
python tag integration test
Basics of python: Output
python unit test template
Python Basic --Pandas, Numpy-
Python / Numpy np.newaxis thinking tips
Convert numpy int64 to python int
Algorithm in Python (primality test)
[Python] Calculation method with numpy
Implemented SMO with Python + NumPy
Matrix multiplication in python numpy
python: Basics of using scikit-learn ①
Create a python numpy array