# Numpy篇

## Numpy 创建array

``````import numpy as np

# a = np.array([1,2,3], dtype =np.int )
# a = np.array([[1,2,3],[3,4,5]] , dtype =float )  #设置精度中 设置64只能用np.float64
# a = np.zeros((3,5),dtype = np.int64)
# a = np.ones((3,4), dtype = np.int64)
# a = np.empty((3,4), dtype = np.int16)
# a = np.empty((3,4))
# a = np.full((3,4),2) #指定矩阵的全部值
# a = np.arange(10,20)
# a = np.arange(20)
# a = np.arange(20).reshape((5,4))

a = np.linspace(1, 5, 20).reshape((5, 4))  # 线段， 从1到10分段

print(a)
print(a.dtype)
a.fill(2)  # 也可以直接用numpy 中的fill填充2
print(a)
# print(help(np.empty))

``````

## Numpy属性

``````import numpy as np

a = np.array([[1, 2, 3], [5, 9, 8]])

print(a)
print("number of dim:", a.ndim)  # 维度
print("shape:", a.shape)
print("size:", a.size)

``````

## Num基本运算1

``````import numpy as np

#
# #a = np.array([1,11,1])
# a = np.array([[1,11,1],[2,3,4]])
#
#
# b = np.arange(6).reshape(3,2)
#
#
# print(a<5) #判断逻辑符直接输出bool型
# print(a ==5)
#
# print(a)
# print(b)
#
# #c = a*b //数和数相乘 前提：同型矩阵
# #c = b**2
# #c =a - b
# #c = a -b
# #c = np.dot(a,b) //矩阵相乘 或者
# #c = a.dot(b)
# #c = np.cos(a) * 100
#
#
# print(c)

a = np.random.random((2, 4))

# print(np.max(a))
# print(np.sum(a))
# print(np.min(a))
print(np.max(a, axis=0))  # axis轴 为1 为行 0为列
print(np.sum(a, axis=1))
print(np.min(a, axis=1))

print(a)

``````

## Numpy基本运算2

``````import numpy as np

a = np.arange(15, 3, -1).reshape((3, 4))
print(np.argmax(a))  # 最大值索引 argument of a function
print(np.argmin(a))  # 最小值索引
# 平均值(也可以设置按行列求）
print(np.mean(a))
print(a.mean())

# 老版本
print(np.average(a))
# 不支持
# print(a.average)
# 中位数
print(np.median(a))

# 累加前面的值
print(np.cumsum(a))  # Cumulative sum 积累 和

# 输出非0的行和列
print(np.nonzero(a))

# 两数之差
print(np.diff(a))

# 每一行(列）排序
print(np.sort(a, axis=0))

# 转置
print(np.transpose(a))
print(a.T.dot(a))

# 只留下范围内的值
print(np.clip(a, 5, 9))

print(a)

``````

THE END

https://github.com/tesseract-ocr/tessdata

)">