import numpy as np
te = [i for i in range(1, 10, 1)]
a = np.array(te)
"""
[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[ 2 4 6 8 10 12 14 16 18]
[0 0 0 0]
[ 0 222 0 0]
[ 1 0 222 0 0 6 7 8 9]
[0 0 0 0 0 0 0 0 0]
[[0. 0.]
[0. 0.]
[0. 0.]]
"""
print(a)
b = np.arange(1, 10, 1)
print(b)
print(a + b)
b[1:5] = 0
c = b[1:5]
d = b[1:5].copy()
c[1] = 222
print(d)
print(c)
print(b)
n = np.zeros_like(b)
print(n)
m = np.zeros((3,2))
print(m)
import numpy as np
li = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
"""
[1 2 3]
[1 4 7]
[[4 5]
[7 8]]
"""
te = np.array(li)
print(te[0])
print(te[:, 0])
print(te[1:, :2])
import numpy as np
"""
布尔索引=================================================================================================================
"""
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
data = np.random.randn(7, 4)
"""
[[ 0.37098025 1.33009106 1.09237444 0.33994922]
[-2.33787083 1.79194859 1.2029973 -1.39176508]
[ 0.37955073 0.25785069 -0.23929716 0.60049713]
[ 0.02142607 0.78854935 -0.09470551 -0.40453658]
[ 0.43533971 -0.15318796 1.47682134 1.15099976]
[-0.23613028 0.88793186 -0.10776622 -0.94578356]
[ 0.00372199 -1.69371697 1.50986182 1.07881397]]
"""
print(data)
"""
[ True False False True False False False]
"""
print(names == 'Bob')
"""
[[0.37098025 1.33009106]
[0.02142607 0.78854935]]
"""
print(data[names == 'Bob', :2])
"""
[[-2.33787083 1.79194859 1.2029973 -1.39176508]
[ 0.37955073 0.25785069 -0.23929716 0.60049713]
[ 0.43533971 -0.15318796 1.47682134 1.15099976]
[-0.23613028 0.88793186 -0.10776622 -0.94578356]
[ 0.00372199 -1.69371697 1.50986182 1.07881397]]
"""
print(data[~(names == 'Bob')])
"""
[[ 0.37098025 1.33009106 1.09237444 0.33994922]
[ 0.37955073 0.25785069 -0.23929716 0.60049713]
[ 0.02142607 0.78854935 -0.09470551 -0.40453658]
[ 0.43533971 -0.15318796 1.47682134 1.15099976]]
"""
print(data[(names == 'Bob') | (names == 'Will')])
"""
[[ 0.37098025 1.33009106 1.09237444 0.33994922]
[-2.33787083 1.79194859 1.2029973 -1.39176508]
[ 0.37955073 0.25785069 -0.23929716 0.60049713]
[ 0.02142607 0.78854935 -0.09470551 -0.40453658]
[ 0.43533971 -0.15318796 1.47682134 1.15099976]
[-0.23613028 0.88793186 -0.10776622 -0.94578356]
[ 0.00372199 -1.69371697 1.50986182 1.07881397]]
"""
data[data < 0] = 0
print(data)
"""
神奇索引=================================================================================================================
"""
arr = np.empty((8, 4))
for i in range(8):
arr[i] = i
"""
[[0. 0. 0. 0.]
[1. 1. 1. 1.]
[2. 2. 2. 2.]
[3. 3. 3. 3.]
[4. 4. 4. 4.]
[5. 5. 5. 5.]
[6. 6. 6. 6.]
[7. 7. 7. 7.]]
"""
print(arr)
"""
[[1. 1. 1. 1.]
[2. 2. 2. 2.]
[4. 4. 4. 4.]
[7. 7. 7. 7.]]
"""
print(arr[[1, 2, 4, -1]])
arr_1 = np.arange(32).reshape((8, 4))
"""
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]
[24 25 26 27]
[28 29 30 31]]
"""
print(arr_1)
"""
[ 4 23 29 10]
"""
print(arr_1[[1, 5, 7, 2], [0, 3, 1, 2]])
"""
[[ 4 7 5 6]
[20 23 21 22]
[28 31 29 30]
[ 8 11 9 10]]
"""
print(arr_1[[1, 5, 7, 2]][:, [0, 3, 1, 2]])
import numpy as np
arr = np.arange(15).reshape((3, 5))
"""
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
"""
print(arr)
"""
[[ 0 5 10]
[ 1 6 11]
[ 2 7 12]
[ 3 8 13]
[ 4 9 14]]
"""
print(arr.T)
"""
[[ 30 80 130]
[ 80 255 430]
[130 430 730]]
"""
print(np.dot(arr, arr.T))
arr_t = np.arange(12).reshape((2, 3, 2))
"""
[[[ 0 1]
[ 2 3]
[ 4 5]]
[[ 6 7]
[ 8 9]
[10 11]]]
"""
print(arr_t)
"""
原数组
x[0][0][0] = 0 x[1][0][0] = 6
x[0][0][1] = 1 x[1][0][1] = 7
x[0][1][0] = 2 x[1][1][0] = 8
x[0][1][1] = 3 x[1][1][1] = 9
x[0][2][0] = 4 x[1][2][0] = 10
x[0][2][1] = 5 x[1][2][1] = 11
换轴后
x[0][0][0] = 0 x[0][0][1] = 6
x[0][1][0] = 1 x[0][1][1] = 7
x[1][0][0] = 2 x[1][0][1] = 8
x[1][1][0] = 3 x[1][1][1] = 9
x[2][0][0] = 4 x[2][0][1] = 10
x[2][1][0] = 5 x[2][1][1] = 11
"""
arr_n = arr_t.transpose(1, 0, 2)
"""
[[[ 0 1]
[ 6 7]]
[[ 2 3]
[ 8 9]]
[[ 4 5]
[10 11]]]
"""
print(arr_n)
"""
[[[ 0 1]
[ 2 3]
[ 4 5]]
[[ 6 7]
[ 8 9]
[10 11]]]
"""
print(np.swapaxes(arr_n, 0, 1))
import numpy as np
cond = [True, False, True, True, False]
arr_1 = np.arange(1.1, 1.6, 0.1)
arr_2 = np.arange(2.1, 2.6, 0.1)
"""
[1.1 2.2 1.3 1.4 2.5]
"""
print(np.where(cond, arr_1, arr_2))
arr = np.random.randn(4, 4)
"""
[[-5.00864517e-01 -2.51396323e-01 5.25682477e-01 2.39411071e+00]
[-8.20321636e-01 1.81823919e-01 -9.62702168e-01 -2.34431255e-01]
[-9.69475044e-02 -7.04054854e-01 -8.84066260e-01 -2.16674083e-03]
[ 5.21392340e-01 4.18711970e-01 -6.11410844e-01 -9.69429615e-01]]
"""
print(arr)
"""
[[-2 -2 2 2]
[-2 2 -2 -2]
[-2 -2 -2 -2]
[ 2 2 -2 -2]]
"""
print(np.where(arr > 0, 2, -2))