100 numpy exercises(51 – 60)

numpyの理解を深めるために100問エクササイズに挑戦してみました。

今回は51問目から60問目までです。

51. Create a structured array representing a position (x,y) and a color (r,g,b).

Z = np.zeros(10, [ ('position', [ ('x', float, 1),
                                  ('y', float, 1)]),
                   ('color',    [ ('r', float, 1),
                                  ('g', float, 1),
                                  ('b', float, 1)])])
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.))]
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:5: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  """

52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances.

Z = np.random.random((10,2))
X,Y = np.atleast_2d(Z[:,0], Z[:,1])
D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)
print(D)

# Much faster with scipy
import scipy
# Thanks Gavin Heverly-Coulson (#issue 1)
import scipy.spatial

Z = np.random.random((10,2))
D = scipy.spatial.distance.cdist(Z,Z)
print(D)
[[0.         0.2809089  0.30252854 0.88683932 0.88265986 0.28441381
  0.51807014 0.35491962 0.44658136 0.06641354]
 [0.2809089  0.         0.42235897 1.09596698 1.10172339 0.50413538
  0.56901235 0.58507899 0.605861   0.2672009 ]
 [0.30252854 0.42235897 0.         0.68195446 0.69359625 0.17008359
  0.2213877  0.23483918 0.18443208 0.24186822]
 [0.88683932 1.09596698 0.68195446 0.         0.05390363 0.60485031
  0.65440183 0.53205068 0.49974274 0.85661714]
 [0.88265986 1.10172339 0.69359625 0.05390363 0.         0.60416229
  0.68232101 0.52820798 0.51461927 0.8561526 ]
 [0.28441381 0.50413538 0.17008359 0.60485031 0.60416229 0.
  0.35538907 0.08151098 0.18845592 0.25207311]
 [0.51807014 0.56901235 0.2213877  0.65440183 0.68232101 0.35538907
  0.         0.38638574 0.22283063 0.45391389]
 [0.35491962 0.58507899 0.23483918 0.53205068 0.52820798 0.08151098
  0.38638574 0.         0.18250087 0.3292397 ]
 [0.44658136 0.605861   0.18443208 0.49974274 0.51461927 0.18845592
  0.22283063 0.18250087 0.         0.39745875]
 [0.06641354 0.2672009  0.24186822 0.85661714 0.8561526  0.25207311
  0.45391389 0.3292397  0.39745875 0.        ]]
[[0.         0.30606031 0.23907925 0.3307453  0.47625149 0.43332986
  0.52627726 0.68407716 0.79826859 0.26952745]
 [0.30606031 0.         0.07319769 0.41407134 0.45281992 0.41699951
  0.65172914 0.6729099  0.58978636 0.14170884]
 [0.23907925 0.07319769 0.         0.35353114 0.46596058 0.42667623
  0.63496029 0.68922764 0.65020792 0.1508412 ]
 [0.3307453  0.41407134 0.35353114 0.         0.77618559 0.7333656
  0.85700795 0.99528604 1.00290031 0.48876855]
 [0.47625149 0.45281992 0.46596058 0.77618559 0.         0.04317951
  0.28872818 0.22351339 0.44106151 0.31655745]
 [0.43332986 0.41699951 0.42667623 0.7333656  0.04317951 0.
  0.29122175 0.26447417 0.46093937 0.27858071]
 [0.52627726 0.65172914 0.63496029 0.85700795 0.28872818 0.29122175
  0.         0.30158413 0.7145625  0.51222406]
 [0.68407716 0.6729099  0.68922764 0.99528604 0.22351339 0.26447417
  0.30158413 0.         0.47740938 0.53926074]
 [0.79826859 0.58978636 0.65020792 1.00290031 0.44106151 0.46093937
  0.7145625  0.47740938 0.         0.53589883]
 [0.26952745 0.14170884 0.1508412  0.48876855 0.31655745 0.27858071
  0.51222406 0.53926074 0.53589883 0.        ]]

53. How to convert a float (32 bits) array into an integer (32 bits) in place?

Z = (np.random.rand(10)*100).astype(np.float32)
Y = Z.view(np.int32)
Y[:] = Z
print(Y)
[98 93 59 86 72 22 53 18 63 63]

54. How to read the following file?

1, 2, 3, 4, 5
6,  ,  , 7, 8
 ,  , 9,10,11
from io import StringIO

# Fake file
s = StringIO('''1, 2, 3, 4, 5

                6,  ,  , 7, 8

                 ,  , 9,10,11
''')
Z = np.genfromtxt(s, delimiter=",", dtype=np.int)
print(Z)
[[ 1  2  3  4  5]
 [ 6 -1 -1  7  8]
 [-1 -1  9 10 11]]

55. What is the equivalent of enumerate for numpy arrays?

Z = np.arange(9).reshape(3,3)
for index, value in np.ndenumerate(Z):
    print(index, value)
for index in np.ndindex(Z.shape):
    print(index, Z[index])
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8

56. Generate a generic 2D Gaussian-like array.

X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))
D = np.sqrt(X*X+Y*Y)
sigma, mu = 1.0, 0.0
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )
print(G)
[[0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
  0.57375342 0.51979489 0.44822088 0.36787944]
 [0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367
  0.69905581 0.63331324 0.54610814 0.44822088]
 [0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308
  0.81068432 0.73444367 0.63331324 0.51979489]
 [0.57375342 0.69905581 0.81068432 0.89483932 0.9401382  0.9401382
  0.89483932 0.81068432 0.69905581 0.57375342]
 [0.60279818 0.73444367 0.85172308 0.9401382  0.98773022 0.98773022
  0.9401382  0.85172308 0.73444367 0.60279818]
 [0.60279818 0.73444367 0.85172308 0.9401382  0.98773022 0.98773022
  0.9401382  0.85172308 0.73444367 0.60279818]
 [0.57375342 0.69905581 0.81068432 0.89483932 0.9401382  0.9401382
  0.89483932 0.81068432 0.69905581 0.57375342]
 [0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308
  0.81068432 0.73444367 0.63331324 0.51979489]
 [0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367
  0.69905581 0.63331324 0.54610814 0.44822088]
 [0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
  0.57375342 0.51979489 0.44822088 0.36787944]]

57. How to randomly place p elements in a 2D array?

n = 10
p = 3
Z = np.zeros((n,n))
np.put(Z, np.random.choice(range(n*n), p, replace=False),1)
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. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

58. Subtract the mean of each row of a matrix.

X = np.random.rand(5, 10)

# Recent versions of numpy
Y = X - X.mean(axis=1, keepdims=True)

# Older versions of numpy
Y = X - X.mean(axis=1).reshape(-1, 1)

print(Y)
[[-0.21708193  0.16476002  0.10010128 -0.1552951   0.0498982  -0.23382438
  -0.21986362  0.36478577  0.23473997 -0.08822021]
 [ 0.44659211 -0.05101604 -0.34631266 -0.43750324  0.4095865   0.40601684
  -0.29478801 -0.37089177 -0.22845377  0.46677003]
 [ 0.10643261 -0.4525966  -0.1377457   0.41566824 -0.15420906  0.47798846
  -0.3470325   0.1825592   0.17238796 -0.26345262]
 [-0.18868209  0.66934892 -0.16878288 -0.05320691  0.12403004 -0.29388894
  -0.31056285 -0.30595092  0.31572224  0.21197338]
 [ 0.03099117 -0.2123511   0.21616598 -0.39693961 -0.18490105  0.53406112
  -0.31208333 -0.40526997  0.39268159  0.33764519]]

59. How to sort an array by the nth column?

Z = np.random.randint(0,10,(3,3))
print(Z)
print(Z[Z[:,1].argsort()])
[[0 8 7]
 [0 6 6]
 [0 7 6]]
[[0 6 6]
 [0 7 6]
 [0 8 7]]

60. How to tell if a given 2D array has null columns?

Z = np.random.randint(0,3,(3,10))
print((~Z.any(axis=0)).any())
False

次回は61〜70問目に挑戦してみます。