各位朋友,我已开通微信公共号:
小程在线
我会把文章及时的更新到公共号上,欢迎大家的关注。
#####数据的布尔值判断
import numpy as np
x=np.array([1,2,3,4,5])
x
Out[1]: array([1, 2, 3, 4, 5])
#判断X是否小于2
x<2
Out[2]: array([ True, False, False, False, False])
#判断X小于2或者大于4
(x<2) | (x>4)
Out[4]: array([ True, False, False, False, True])
mask=x<3
mask
Out[5]: array([ True, True, False, False, False])
x[mask]
Out[6]: array([1, 2])
np.sum(x<3)
Out[7]: 2
y=np.array([[1,2,3,4,5],[6,7,8,9,10]])
y
Out[8]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
x == y[0,]
Out[9]: array([ True, True, True, True, True])
#两个数组比较
a1 = np.arange(9).reshape(3, 3)
a2 = np.arange(9, 0 , -1).reshape(3, 3)
a1 < a2
Out[10]:
array([[ True, True, True],
[ True, True, False],
[False, False, False]])
#####数据的切片选择(开始位,结束位,步长)
#位置值从0开始的
x=np.arange(0,10)
x[4:6]
Out[11]: array([4, 5])
#从0开始,步长为2的数据
x[::2]
Out[12]: array([0, 2, 4, 6, 8])
#倒序
x[::-1]
Out[13]: array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
#取前6个数据
x[:6]
Out[14]: array([0, 1, 2, 3, 4, 5])
#从第2的位置开始选
x[2:]
Out[15]: array([2, 3, 4, 5, 6, 7, 8, 9])
#二维数据的切片处理
y=np.arange(0,16).reshape(4,4)
y
Out[16]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
y[:,1:3]
Out[17]:
array([[ 1, 2],
[ 5, 6],
[ 9, 10],
[13, 14]])
y[1:3,2:4]
Out[18]:
array([[ 6, 7],
[10, 11]])
y[[1,3],:]
Out[19]:
array([[ 4, 5, 6, 7],
[12, 13, 14, 15]])
#######数据的维度变化########
x=np.arange(0,9)
y=x.reshape(3,3)
y
Out[20]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
x=np.arange(0,9)
y=x.reshape(3,3)
y
Out[21]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
#返回一个新的数组,不是修改原来的数组
y.reshape(9)
Out[22]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])
y
Out[23]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
reshaped = y.reshape(np.size(y))
raveled = y.ravel() #将数据展开成一维的
reshaped[2] = 1000
raveled[5] = 2000
y
Out[24]:
array([[ 0, 1, 1000],
[ 3, 4, 2000],
[ 6, 7, 8]])
y = np.arange(0, 9).reshape(3,3)
flattened = y.flatten() #将数据展开成一维的
flattened[0] = 1000
flattened
Out[25]: array([1000, 1, 2, 3, 4, 5, 6, 7, 8])
flattened.shape = (3, 3)
flattened
Out[26]:
array([[1000, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]])
#数据的转置
flattened.T
Out[27]:
array([[1000, 3, 6],
[ 1, 4, 7],
[ 2, 5, 8]])
####数据的合并处理
a = np.arange(9).reshape(3, 3)
b = (a + 1) * 10
a
Out[28]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
b
Out[29]:
array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
#水平方向
np.hstack((a, b))
Out[30]:
array([[ 0, 1, 2, 10, 20, 30],
[ 3, 4, 5, 40, 50, 60],
[ 6, 7, 8, 70, 80, 90]])
#垂直方向
np.vstack((a, b))
Out[31]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
#axis = 1横轴方向,axis = 0 竖轴方向
np.concatenate((a, b), axis = 1)
Out[32]:
array([[ 0, 1, 2, 10, 20, 30],
[ 3, 4, 5, 40, 50, 60],
[ 6, 7, 8, 70, 80, 90]])
#每列相互拼接
np.dstack((a, b))
Out[33]:
array([[[ 0, 10],
[ 1, 20],
[ 2, 30]],
[[ 3, 40],
[ 4, 50],
[ 5, 60]],
[[ 6, 70],
[ 7, 80],
[ 8, 90]]])
#两列拼接
one_d_a = np.arange(5)
one_d_b = (one_d_a + 1) * 10
np.column_stack((one_d_a, one_d_b))
Out[36]:
array([[ 0, 10],
[ 1, 20],
[ 2, 30],
[ 3, 40],
[ 4, 50]])
##行叠加
np.row_stack((one_d_a, one_d_b))
Out[37]:
array([[ 0, 1, 2, 3, 4],
[10, 20, 30, 40, 50]])
####通用函数
m = np.arange(10, 19).reshape(3, 3)
print (m)
print ("{0} min of the entire matrix".format(m.min()))
print ("{0} max of entire matrix".format(m.max()))
##最小值、最大值的位置
print ("{0} position of the min value".format(m.argmin()))
print ("{0} position of the max value".format(m.argmax()))
#每列、每行的最小值
print ("{0} mins down each column".format(m.min(axis = 0)))
print ("{0} mins across each row".format(m.min(axis = 1)))
#每列、每行的最大值
print ("{0} maxs down each column".format(m.max(axis = 0)))
print ("{0} maxs across each row".format(m.max(axis = 1)))
[[10 11 12]
[13 14 15]
[16 17 18]]
10 min of the entire matrix
18 max of entire matrix
0 position of the min value
8 position of the max value
[10 11 12] mins down each column
[10 13 16] mins across each row
[16 17 18] maxs down each column
[12 15 18] maxs across each row
#平均值,标准差,方差
a
Out[41]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
a = np.arange(1,10)
a.mean(), a.std(), a.var()
Out[39]: (5.0, 2.581988897471611, 6.666666666666667)
#求和,乘积
a.sum(), a.prod()
Out[40]: (45, 362880)
#累加和,累加乘
a.cumsum(), a.cumprod()
Out[42]:
(array([ 1, 3, 6, 10, 15, 21, 28, 36, 45], dtype=int32),
array([ 1, 2, 6, 24, 120, 720, 5040, 40320,
362880], dtype=int32))