import numpy
n, m = map(int, input().split())
array = numpy.array([list(map(int, input().split())) for _ in range(n)])
print(numpy.transpose(array))
print(array.flatten())
Concatenate
Two or more arrays can be concatenated together using the concatenate function with a tuple of the arrays to be joined:
If an array has more than one dimension, it is possible to specify the axis along which multiple arrays are concatenated. By default, it is along the first dimension.
import numpy
n, m, p = map(int, input().split())
arr_n = numpy.array([list(map(int, input().split())) for _ in range(n)])
arr_m = numpy.array([list(map(int, input().split())) for _ in range(m)])
print(numpy.concatenate((arr_n, arr_m)))
Zeroes and Ones
The zeros tool returns a new array with a given shape and type filled with 's.
import numpy
print numpy.zeros((1,2)) #Default type is float
#Output : [[ 0. 0.]]
print numpy.zeros((1,2), dtype = numpy.int) #Type changes to int
#Output : [[0 0]]
The ones tool returns a new array with a given shape and type filled with 's.
import numpy
print numpy.ones((1,2)) #Default type is float
#Output : [[ 1. 1.]]
print numpy.ones((1,2), dtype = numpy.int) #Type changes to int
#Output : [[1 1]]
The identity tool returns an identity array. An identity array is a square matrix with all the main diagonal elements as and the rest as . The default type of elements is float.
import numpy
print numpy.identity(3) #3 is for dimension 3 X 3
#Output
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
The eye tool returns a 2-D array with 's as the diagonal and 's elsewhere. The diagonal can be main, upper or lower depending on the optional parameter . A positive is for the upper diagonal, a negative is for the lower, and a (default) is for the main diagonal.
import numpy
numpy.set_printoptions(legacy='1.13')
n, m = map(int, input().split())
print(numpy.eye(n, m, k=0))
Array Mathematics
Basic mathematical functions operate element-wise on arrays. They are available both as operator overloads and as functions in the NumPy module.
import numpy
a = numpy.array([1,2,3,4], float)
b = numpy.array([5,6,7,8], float)
print a + b #[ 6. 8. 10. 12.]
print numpy.add(a, b) #[ 6. 8. 10. 12.]
print a - b #[-4. -4. -4. -4.]
print numpy.subtract(a, b) #[-4. -4. -4. -4.]
print a * b #[ 5. 12. 21. 32.]
print numpy.multiply(a, b) #[ 5. 12. 21. 32.]
print a / b #[ 0.2 0.33333333 0.42857143 0.5 ]
print numpy.divide(a, b) #[ 0.2 0.33333333 0.42857143 0.5 ]
print a % b #[ 1. 2. 3. 4.]
print numpy.mod(a, b) #[ 1. 2. 3. 4.]
print a**b #[ 1.00000000e+00 6.40000000e+01 2.18700000e+03 6.55360000e+04]
print numpy.power(a, b) #[ 1.00000000e+00 6.40000000e+01 2.18700000e+03 6.55360000e+04]
import numpy
n, m = map(int, input().split())
A = numpy.array([list(map(int, input().split())) for _ in range(n)])
B = numpy.array([list(map(int, input().split())) for _ in range(n)])
print(numpy.add(A, B))
print(numpy.subtract(A, B))
print(numpy.multiply(A, B))
print(numpy.floor_divide(A, B))
print(numpy.mod(A, B))
print(numpy.power(A, B))
Floor, Ceil and Rint
The tool floor returns the floor of the input element-wise.
The floor of is the largest integer where .
By default, the axis value is None. Therefore, it performs the product over all the dimensions of the input array.
Task
import numpy
n, m = map(int, input().split())
arr = numpy.array([list(map(int, input().split())) for _ in range(n)])
print(numpy.product(numpy.sum(arr, axis=0), axis=0))
Min and Max
The tool min returns the minimum value along a given axis.
By default, the axis value is None. Therefore, it finds the maximum over all the dimensions of the input array.
Task
import numpy
n, m = map(int, input().split())
arr = numpy.array([list(map(int, input().split())) for _ in range(n)])
print(numpy.max(numpy.min(arr, axis=1), axis=0))
Mean, Var, and Std
The mean tool computes the arithmetic mean along the specified axis.
By default, the axis is None. Therefore, it computes the standard deviation of the flattened array.
Task
import numpy
n, m = map(int, input().split())
arr = numpy.array([list(map(int, input().split())) for _ in range(n)])
print(numpy.mean(arr, axis=1))
print(numpy.var(arr, axis=0))
print(round(numpy.std(arr), 11))
Dot and Cross
The dot tool returns the dot product of two arrays.
import numpy
A = numpy.array([ 1, 2 ])
B = numpy.array([ 3, 4 ])
print numpy.dot(A, B) #Output : 11
The cross tool returns the cross product of two arrays.
import numpy
A = numpy.array([ 1, 2 ])
B = numpy.array([ 3, 4 ])
print numpy.cross(A, B) #Output : -2
Task
import numpy
n = int(input())
A = numpy.array([list(map(int, input().split())) for _ in range(n)])
B = numpy.array([list(map(int, input().split())) for _ in range(n)])
print(numpy.dot(A, B))
Inner and Outer
import numpy
A = numpy.array([0, 1])
B = numpy.array([3, 4])
print numpy.inner(A, B) #Output : 4
import numpy
A = numpy.array([0, 1])
B = numpy.array([3, 4])
print numpy.outer(A, B) #Output : [[0 0]
# [3 4]]
Task
import numpy
A = numpy.array(list(map(int, input().split())))
B = numpy.array(list(map(int, input().split())))
print(numpy.inner(A, B))
print(numpy.outer(A, B))
Polynomials
The poly tool returns the coefficients of a polynomial with the given sequence of roots.