상세 컨텐츠

본문 제목

[ Numpy 100제 ] 41 ~ 50 번

Python/Numpy

by teang1995 2022. 1. 30. 16:25

본문

반응형

41. How to sum a small array faster than np.sum? (★★☆)

Z = np.arange(10)
np.add.reduce(Z)
# test for np.add.reduce
import time
def check_time(func):
    def wrapper(*args, **kwargs):
        start = time.time()
        func(*args, **kwargs)
        print("used time:", time.time() - start)

    return wrapper

@check_time
def normal_sum(len_array):
    a = np.arange(len_array)
    np.sum(a)

@check_time
def reduced_sum(len_array):
    a = np.arange(len_array)
    np.add.reduce(a)

len_arr = 100
normal_sum(len_arr)
reduced_sum(len_arr)
'''
used time: 4.601478576660156e-05
used time: 0.00480198860168457
'''

42. Consider two random array A and B, check if they are equal (★★☆)

A = np.random.normal(0, 1, 10)
B = np.random.normal(0, 1, 10)


# Assuming identical shape of the arrays and a tolerance for the comparison of values
equal = np.allclose(A,B)
print(equal)

# Checking both the shape and the element values, no tolerance (values have to be exactly equal)
equal = np.array_equal(A,B)
print(equal)
'''
False
False
'''
# explore np.allclose
# numpy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)

A = np.random.normal(0, 1, 10)
B = np.random.normal(0, 1, 10)

print(np.allclose(A, B, atol=1000))
print(np.allclose(A, B, atol=0.1))

'''
True
False
'''

# explore np.array_equal
# numpy.array_equal(a1, a2, equal_nan=False) 
# ***Important*** : no tolerance
A = np.random.normal(0, 1, 10)
B = np.random.normal(0, 1, 10)

print(np.array_equal(A, B))
# False

43. Make an array immutable (read-only) (★★☆)

Z = np.zeros(10)
Z.flags.writeable = False
Z[0] = 1

44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)

Z = np.random.random((10,2))
X,Y = Z[:,0], Z[:,1]
R = np.sqrt(X**2+Y**2)
T = np.arctan2(Y,X)
print(R)
print(T)
'''
[0.45912929 0.92255682 0.82994457 1.15727038 0.72725793 1.03169075
 1.16589678 0.68023568 0.47080096 0.33646058]
[1.33231746 0.44323987 0.35037791 0.91645145 0.22079637 0.95985385
 0.66637595 1.02757962 1.2022807  0.03471622]
'''

45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)

Z = np.random.normal(0, 10, 10)
print(Z)
Z[np.argmax(Z)] = 0 # or Z[Z.argmax()] = 0
print(Z)
'''
[  5.86756166 -10.72293865   3.18633952  -5.98078506  -9.2948068
 -14.92946556   3.52660497  -8.75820848 -17.94239223  -7.78732343]
[  0.         -10.72293865   3.18633952  -5.98078506  -9.2948068
 -14.92946556   3.52660497  -8.75820848 -17.94239223  -7.78732343]
'''

46. Create a structured array with x and y coordinates covering the [0,1]x[0,1] area (★★☆)

Z = np.zeros((5,5), [('x',float),('y',float)])
Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,5),
                             np.linspace(0,1,5))
print(Z)
'''
[[(0.  , 0.  ) (0.25, 0.  ) (0.5 , 0.  ) (0.75, 0.  ) (1.  , 0.  )]
 [(0.  , 0.25) (0.25, 0.25) (0.5 , 0.25) (0.75, 0.25) (1.  , 0.25)]
 [(0.  , 0.5 ) (0.25, 0.5 ) (0.5 , 0.5 ) (0.75, 0.5 ) (1.  , 0.5 )]
 [(0.  , 0.75) (0.25, 0.75) (0.5 , 0.75) (0.75, 0.75) (1.  , 0.75)]
 [(0.  , 1.  ) (0.25, 1.  ) (0.5 , 1.  ) (0.75, 1.  ) (1.  , 1.  )]]
'''

# explore np -> coordinates?
Z = np.zeros((5,5), [('x',float),('y',float)])

print(Z['x'])
print(Z['y'])
print(Z)
'''
[[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. 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.) (0., 0.) (0., 0.) (0., 0.)]
 [(0., 0.) (0., 0.) (0., 0.) (0., 0.) (0., 0.)]]
'''

47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)

# My Soultion
X = np.random.uniform(0, 1, 5)
Y = np.random.uniform(0, 1, 5)

print(X)
print(Y)
C = 1/(X - Y)
print(C)
'''
[0.28061467 0.08726279 0.46761322 0.56887701 0.93262726]
[0.75486276 0.07972277 0.41892783 0.77214179 0.17509809]
[ -2.10860099 132.62563333  20.54004592  -4.91969157   1.32008117]
'''

# Answer
X = np.arange(8)
print(X)
Y = X + 0.5
print(Y)
print(np.subtract.outer(X, Y))
C = 1.0 / np.subtract.outer(X, Y)
print(C)
print(np.linalg.det(C)) # Why?
'''
[0 1 2 3 4 5 6 7]
[0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5]
[[-0.5 -1.5 -2.5 -3.5 -4.5 -5.5 -6.5 -7.5]
 [ 0.5 -0.5 -1.5 -2.5 -3.5 -4.5 -5.5 -6.5]
 [ 1.5  0.5 -0.5 -1.5 -2.5 -3.5 -4.5 -5.5]
 [ 2.5  1.5  0.5 -0.5 -1.5 -2.5 -3.5 -4.5]
 [ 3.5  2.5  1.5  0.5 -0.5 -1.5 -2.5 -3.5]
 [ 4.5  3.5  2.5  1.5  0.5 -0.5 -1.5 -2.5]
 [ 5.5  4.5  3.5  2.5  1.5  0.5 -0.5 -1.5]
 [ 6.5  5.5  4.5  3.5  2.5  1.5  0.5 -0.5]]
[[-2.         -0.66666667 -0.4        -0.28571429 -0.22222222 -0.18181818
  -0.15384615 -0.13333333]
 [ 2.         -2.         -0.66666667 -0.4        -0.28571429 -0.22222222
  -0.18181818 -0.15384615]
 [ 0.66666667  2.         -2.         -0.66666667 -0.4        -0.28571429
  -0.22222222 -0.18181818]
 [ 0.4         0.66666667  2.         -2.         -0.66666667 -0.4
  -0.28571429 -0.22222222]
 [ 0.28571429  0.4         0.66666667  2.         -2.         -0.66666667
  -0.4        -0.28571429]
 [ 0.22222222  0.28571429  0.4         0.66666667  2.         -2.
  -0.66666667 -0.4       ]
 [ 0.18181818  0.22222222  0.28571429  0.4         0.66666667  2.
  -2.         -0.66666667]
 [ 0.15384615  0.18181818  0.22222222  0.28571429  0.4         0.66666667
   2.         -2.        ]]
3638.1636371179666
'''

48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)

for dtype in [np.int8, np.int32, np.int64]:
   print(np.iinfo(dtype).min)
   print(np.iinfo(dtype).max)
for dtype in [np.float32, np.float64]:
   print(np.finfo(dtype).min)
   print(np.finfo(dtype).max)
   print(np.finfo(dtype).eps)

'''
-128
127
-2147483648
2147483647
-9223372036854775808
9223372036854775807
-3.4028235e+38
3.4028235e+38
1.1920929e-07
-1.7976931348623157e+308
1.7976931348623157e+308
2.220446049250313e-16
'''

49. How to print all the values of an array? (★★☆)

np.set_printoptions(threshold=float("inf"))
Z = np.zeros((40,40))
print(Z)

50. How to find the closest value (to a given scalar) in a vector? (★★☆)

# My Soultion
Z = np.random.normal(0, 10, 10)
scalar = 5
print(Z)

print(Z[np.argmin(np.abs(Z - scalar))])

'''
[-11.33735027   0.53781634   8.49856071  -9.32584894   0.05189519
   6.06827857  -9.75448748  -7.10097725  24.39793682   8.47501747]
6.0682785713322875
'''

'Python > Numpy' 카테고리의 다른 글

[ Numpy 100제 ] 31~40 번  (0) 2022.01.30
[ Numpy 100제 ] 21 ~ 30 번  (0) 2021.10.06
[ Numpy 100제 ] 11 ~ 20 번  (0) 2021.10.02
[ Numpy 100제 ] 01 ~ 10 번  (0) 2021.10.01

관련글 더보기

댓글 영역