# 转换数据类型

numpy的array函数生成的数据默认是int型，可以使用astype对数据类型进行转换，代码如下所示：

``````import numpy as np

a = np.array([2,3,4])
print(a,a.dtype)
print('------')
b = a.astype(np.complex)#转换数据类型为complex
print(b.dtype,a.dtype)
``````

# 改变矩阵形状

``````d = np.random.random((2,3,5))
print(d,d.shape)
d.shape = (3,2,5)
print(d,d.shape)
print('------')
e = d.reshape(3,10)#当知道矩阵形状的行或者列时，另一个参数可以用-1代替，就是让程序自己计算
#e = d.reshape(3,-1)
print(e,e.shape,d.shape)
``````

# 数组的运算

## 数组之间的运算

``````a1 = [2,3,5]
b1 = ['222','dfy',88]
print(a1+b1)

a = np.array([2,3,5])
b = np.array([4,5,2])
print(a+b)
print(a**2)

c = np.array([[1,2,3],[4,5,6]])
d = np.array([[2,3,4],[7,8,9]])
print(c+d)
``````

`````` = np.array([[1,2,3],[4,5,6]])
d = np.array([[2,3,4],[7,8,9]])
print(c+d)
d.shape = (3,2)
print(c)
print(d)
print('------')
print(np.dot(c,d))
print('``````')
print(c.dot(d))#计算结果与np.dot(c,d)相同
``````

# 转置

``````import numpy as np

#转置
a = np.arange(24).reshape((2,3,4))
print(a)
b = a.T
c = np.transpose(a)#np.transpose跟a.T得到的结果是相同的

print(b,b.shape)
print(c,c.shape)
``````