1、数组转置和轴对称
import numpy as np
import numpy.random as np_random
print '转置矩阵'
arr = np.arange(15).reshape((3, 5))
print arr
print arr.T
print '转置矩阵做点积'
print np.dot(arr.T, arr)
print
print '高维矩阵转换'
# arr为三维的数组
arr = np.arange(16).reshape((2, 2, 4))
print arr
'''
详细解释:
arr数组的内容为
- a[0][0] = [0, 1, 2, 3]
- a[0][1] = [4, 5, 6, 7]
- a[1][0] = [8, 9, 10, 11]
- a[1][1] = [12, 13, 14, 15]
transpose的参数为坐标,正常顺序为(0, 1, 2, ... , n - 1),
现在传入的为(1, 0, 2)代表a[x][y][z] = a[y][x][z],第0个和第1个坐标互换。
- a'[0][0] = a[0][0] = [0, 1, 2, 3]
- a'[0][1] = a[1][0] = [8, 9, 10, 11]
- a'[1][0] = a[0][1] = [4, 5, 6, 7]
- a'[1][1] = a[1][1] = [12, 13, 14, 15]
'''
print arr.transpose((1, 0, 2))
print arr.swapaxes(1, 2) # 直接交换第1和第2个坐标
注:在使用transpose对高维数组进行转置的时候,传递一个元组(由轴序号组成)。
例如:三维数组的轴序号就是(0,1,2),n维度数组的轴序号就是(0,1,…,n-1)
上面例子中传入(1,0,2)表示交换轴为0和1的数据
2、numpy快速的元素级数组函数
import numpy as np
import numpy.random as np_random
print('求平方根')
arr = np.arange(10)
print(np.sqrt(arr))
print
print('数组比较')
x = np_random.randn(8)
y = np_random.randn(8)
print(x)
print(y)
print(np.maximum(x, y))
print('使用modf函数把浮点数分解成整数和小数部分')
arr = np_random.randn(7) * 5 # 统一乘5
print(np.modf(arr))
# 结果
求平方根
[ 0. 1. 1.41421356 1.73205081 2. 2.23606798
2.44948974 2.64575131 2.82842712 3. ]
数组比较
[-0.06004316 0.00880681 -1.21653344 0.22366926 -0.61250815 1.3927452
0.17385704 -1.34299418]
[-0.12082753 1.12156785 0.69139167 1.24387192 -2.38294919 -1.20677894
1.46519425 -0.40380433]
[-0.06004316 1.12156785 0.69139167 1.24387192 -0.61250815 1.3927452
1.46519425 -0.40380433]
使用modf函数把浮点数分解成整数和小数部分
(array([-0.62859936, 0.69964832, 0.77634507, 0.10423475, 0.6272251 ,
0.88250647, 0.09868083]), array([-1., 6., 0., 2., 4., 7., 3.]))
3、numpy中meshgrid函数
import matplotlib.pyplot as plt
import numpy as np
import pylab
points = np.arange(-5, 5, 0.01) # 生成100个点
print(points)
xs, ys = np.meshgrid(points, points) # xs, ys为100行100列 互为转置矩阵
print(xs)
print(ys)
z = np.sqrt(xs ** 2 + ys ** 2)
print(z)
plt.imshow(z, cmap=plt.cm.gray)
plt.colorbar()
plt.title("")
pylab.show()
4、numpy中where函数
import numpy as np
import numpy.random as np_random
'''
关于zip函数的一点解释,zip可以接受任意多参数,然后重新组合成1个tuple列表。
zip([1, 2, 3], [4, 5, 6], [7, 8, 9])
返回结果:[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
'''
print('通过真值表选择元素')
x_arr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
y_arr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])
cond = np.array([True, False, True, True, False])
result = [(x if c else y) for x, y, c in zip(x_arr, y_arr, cond)] # 通过列表推到实现
print(result)
# 如果cond为true,取x_arr,否则去y_arr
print(np.where(cond, x_arr, y_arr)) # 使用NumPy的where函数
print('更多where的例子')
arr = np_random.randn(4, 4)
print(arr)
print(np.where(arr > 0, 2, -2))
print(np.where(arr > 0, 2, arr))
print
print('where嵌套')
cond_1 = np.array([True, False, True, True, False])
cond_2 = np.array([False, True, False, True, False])
# 传统代码如下
result = []
for i in range(len(cond)):
if cond_1[i] and cond_2[i]:
result.append(0)
elif cond_1[i]:
result.append(1)
elif cond_2[i]:
result.append(2)
else:
result.append(3)
print(result)
# np版本代码
result = np.where(cond_1 & cond_2, 0, \
np.where(cond_1, 1, np.where(cond_2, 2, 3)))
print(result)