import numpy
get_num = numpy.genfromtxt(‘../xls/text_for_numpy.txt‘, dtype=‘str‘, delimiter=‘,‘)
print(get_num)
‘‘‘
[[‘001‘ ‘厄运莎拉‘ ‘2001A‘]
[‘002‘ ‘XML‘ ‘2002A‘]
[‘003‘ ‘wei‘ ‘2003A‘]
[‘004‘ ‘123‘ ‘2004A‘]
[‘005‘ ‘kkk‘ ‘2005A‘]]
‘‘‘
print(type(get_num))
# <class ‘numpy.ndarray‘>
# 所有的numpy都是 ndarry 的格式
print(numpy.array([get_num]))
num = [[1, 2, 3], [2, 3, 4]]
a_num = numpy.array(num)
# numpy.array中 数据结构必须一定
print(a_num) # 转成array类型
# [[1 2 3]
# [2 3 4]]
print(a_num.shape) # 查看结构
# (2, 3)
print(a_num[0, 1]) # 2-------------> 0 代表第一个样本,1 代表第二个指标
print(a_num[0, :]) # 第一行
print(a_num[:, 0]) # 第一列
print(a_num == 5)
‘‘‘
bool 类型
[[False False False]
[False False False]]
‘‘‘
import numpy
num_new = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 22, 11, 12]])
index = (num_new == 22)
print(index)
‘‘‘
[[False False False False]
[False False False False]
[False True False False]]
‘‘‘
print(num_new[index])
‘‘‘
[22] # 存在即返回,不存在返回[]
‘‘‘
import numpy
num_new = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 22, 11, 12]])
index_new = (num_new[:, 1] == 22)
print(num_new[index_new, :]) # [[ 9 22 11 12]]
import numpy
num_new = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 22, 11, 12]])
# 与
print(‘一个数等于5或者11‘, (num_new == 5) & (num_new == 11))
‘‘‘
一个数等于5或者11
[[False False False False]
[False False False False]
[False False False False]]
‘‘‘
import numpy
num_new = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 22, 11, 12]])
# 或
print(‘一个数可能等于5,也可能等于11‘, (num_new == 5) | (num_new == 11))
‘‘‘
一个数可能等于5,也可能等于11
[[False False False False]
[ True False False False]
[False False True False]]
‘‘‘
import numpy
num_new = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 22, 11, 12]])
print(num_new.dtype)
# int32
change_type = num_new.astype(float)
print(change_type)
‘‘‘
[[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 9. 22. 11. 12.]]
‘‘‘
print(change_type.dtype)
# float64
import numpy
num_new = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 22, 11, 12]])
print(num_new.min()) # 1
print(num_new.max()) # 22
print(num_new[1, :].max()) # 8(第二行最大值)
import numpy
num_new = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 22, 11, 12]])
print(sum(num_new[1, :])) # 26(第二行数据之和)
print(sum(num_new[:, 0])) # 15(第一列数据之和)
import numpy
num_new = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 22, 11, 12]])
print(num_new.sum(axis=1)) # 行求和 [10 26 54]
print(num_new.sum(axis=0)) # 列求和 [15 30 21 24]
print(numpy.arange(18))
# [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
a = numpy.arange(18).reshape(3, 6)
print(a)
‘‘‘
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]]
‘‘‘
print(a.shape) # 查看行和列分别是多少 (3, 6)
print(a.ndim) # 查看维度 2
print(a.dtype) # 查看数据类型 int32
print(a.size) # 查看多少个数据 18
print(numpy.zeros((2, 3)))
# (2, 3)元组形式,不然报错
‘‘‘
[[0. 0. 0.]
[0. 0. 0.]]
‘‘‘
print(numpy.ones((2, 3, 2))) # 2行3列的二维矩阵
‘‘‘
[[[1. 1.]
[1. 1.]
[1. 1.]]
[[1. 1.]
[1. 1.]
[1. 1.]]]
‘‘‘
print(numpy.arange(1, 30, 2))
# [ 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29]
print(numpy.random.random((2, 3)))
‘‘‘
[[0.69185679 0.67417911 0.10834083]
[0.2324281 0.77956343 0.57141579]]
‘‘‘
print(numpy.linspace(0, 2*numpy.pi, 100))
# pi是Π,起始值是0,终点是 2Π,造出100个值
‘‘‘
[0. 0.06346652 0.12693304 0.19039955 0.25386607 0.31733259
0.38079911 0.44426563 0.50773215 0.57119866 0.63466518 0.6981317
0.76159822 0.82506474 0.88853126 0.95199777 1.01546429 1.07893081
1.14239733 1.20586385 1.26933037 1.33279688 1.3962634 1.45972992
1.52319644 1.58666296 1.65012947 1.71359599 1.77706251 1.84052903
1.90399555 1.96746207 2.03092858 2.0943951 2.15786162 2.22132814
2.28479466 2.34826118 2.41172769 2.47519421 2.53866073 2.60212725
2.66559377 2.72906028 2.7925268 2.85599332 2.91945984 2.98292636
3.04639288 3.10985939 3.17332591 3.23679243 3.30025895 3.36372547
3.42719199 3.4906585 3.55412502 3.61759154 3.68105806 3.74452458
3.8079911 3.87145761 3.93492413 3.99839065 4.06185717 4.12532369
4.1887902 4.25225672 4.31572324 4.37918976 4.44265628 4.5061228
4.56958931 4.63305583 4.69652235 4.75998887 4.82345539 4.88692191
4.95038842 5.01385494 5.07732146 5.14078798 5.2042545 5.26772102
5.33118753 5.39465405 5.45812057 5.52158709 5.58505361 5.64852012
5.71198664 5.77545316 5.83891968 5.9023862 5.96585272 6.02931923
6.09278575 6.15625227 6.21971879 6.28318531]
‘‘‘
v = numpy.array([[1, 2, 3, 5], [7, 8, 9, 10]])
n = numpy.arange(0, 8, 1).reshape(2, 4)
print(‘v‘, v)
‘‘‘
v
[[ 1 2 3 5]
[ 7 8 9 10]]
‘‘‘
print(‘n‘, n)
‘‘‘
n
[[0 1 2 3]
[4 5 6 7]]
‘‘‘
print(‘v+n‘, v+n)
‘‘‘
v+n
[[ 1 3 5 8]
[11 13 15 17]]
‘‘‘
print(‘v-n‘, v-n)
‘‘‘
v-n
[[1 1 1 2]
[3 3 3 3]]
‘‘‘
print(‘v*n‘, v*n)
‘‘‘
v*n
[[ 0 2 6 15]
[28 40 54 70]]
‘‘‘
print(‘v*2‘, v*2)
‘‘‘
v*2
[[ 2 4 6 10]
[14 16 18 20]]
‘‘‘
print(v < 35)
‘‘‘
[[ True True True True]
[ True True True True]]
‘‘‘
k = numpy.array([[1, 2], [3, 4]])
p = numpy.array([[6, 7], [9, 10]])
print(k)
print(‘------------‘)
print(p)
print(‘------------‘)
print(k*p)
print(‘------------‘)
print(k.dot(p))
print(‘------------‘)
print(numpy.dot(k, p))
‘‘‘
k
[[1 2]
[3 4]]
------------
p
[[ 6 7]
[ 9 10]]
------------
k*p
[[ 6 14]
[27 40]]
------------
k.dot(p)
[[24 27] # 矩阵乘法 [1*6+2*9,1*7+2*10
[54 61]] 3*6+4*9, 3*7+4*10]
------------
numpy.dot(k, p)
[[24 27]
[54 61]]
‘‘‘
import numpy as np
B = np.arange(3)
print(B)
# [0 1 2]
print(np.exp(B)) # e的几次幂
# [1. 2.71828183 7.3890561 ]
import numpy as np
B = np.arange(3)
print(B)
# [0 1 2]
print(np.sqrt(B)) # 数组中二次根号
# [0. 1. 1.41421356]
import numpy as np
ap = np.floor(10*np.random.random((3, 4)))
print(ap)
‘‘‘
[[0. 1. 9. 4.]
[0. 1. 9. 3.]
[9. 1. 8. 8.]]
‘‘‘
import numpy as np
ap = np.floor(10*np.random.random((3, 4)))
print(ap.ravel())
# [0. 1. 9. 4. 0. 1. 9. 3. 9. 1. 8. 8.]
# 把矩阵变成向量
ap.shape = (6, 2)
# ap.shape = (6,-1) 相同结果,因为二维中确定行,列数是自动计算的,写一个默认值 -1,只是让numpy自己计算
print(ap)
# [[0. 1.]
# [9. 4.]
# [0. 1.]
# [9. 3.]
# [9. 1.]
# [8. 8.]]
print(a.T)
# 转至,行和列变换
‘‘‘
[[ 0 6 12]
[ 1 7 13]
[ 2 8 14]
[ 3 9 15]
[ 4 10 16]
[ 5 11 17]]
‘‘‘
import numpy as np
h1 = np.floor(10*np.random.random((2, 2)))
h2 = np.floor(10*np.random.random((2, 2)))
print(h1, h2, np.hstack((h1, h2))) # 注意元祖格式
‘‘‘
[[5. 4.] [[1. 9.] [[5. 4. 1. 9.]
[4. 9.]] [6. 9.]] [4. 9. 6. 9.]]
‘‘‘
import numpy as np
h1 = np.floor(10*np.random.random((2, 2)))
h2 = np.floor(10*np.random.random((2, 2)))
print(h1, h2, np.vstack((h1, h2)))
‘‘‘
[[8. 6.] [[7. 1.] [[8. 6.]
[6. 2.]] [3. 9.]] [6. 2.]
[7. 1.]
[3. 9.]]
‘‘‘
3.3.1 横向切割
import numpy as np
j = np.floor(10*np.random.random((2, 12)))
# 平均切割
print(j, np.hsplit(j, 3))
‘‘‘
[[7. 6. 3. 7. 1. 9. 1. 7. 6. 9. 0. 4.]
[2. 3. 3. 5. 7. 0. 6. 8. 4. 6. 7. 0.]]
----------------------------------------
[array([[7., 6., 3., 7.],[2., 3., 3., 5.]]),
array([[1., 9., 1., 7.],[7., 0., 6., 8.]]),
array([[6., 9., 0., 4.],[4., 6., 7., 0.]])]
‘‘‘
import numpy as np
j = np.floor(10*np.random.random((2, 12)))
print(j, np.hsplit(j, (3, 4)))
‘‘‘
[[7. 6. 3. 7. 1. 9. 1. 7. 6. 9. 0. 4.]
[2. 3. 3. 5. 7. 0. 6. 8. 4. 6. 7. 0.]]
----------------------------------------
[array([[7., 6., 3.],[2., 3., 3.]]),
array([[7.],[5.]]),
array([[1., 9., 1., 7., 6., 9., 0., 4.],[7., 0., 6., 8., 4., 6., 7., 0.]])]
‘‘‘
3.3.2 纵向切割
import numpy as np
j = np.floor(10*np.random.random((2, 12)))
print(j.T, np.vsplit(j.T, 3))
‘‘‘
[[6. 3.]
[9. 0.]
[5. 2.]
[4. 8.]
[8. 2.]
[1. 9.]
[4. 0.]
[6. 2.]
[8. 3.]
[5. 7.]
[0. 7.]
[2. 1.]]
----------------------------
[array([[6., 3.],
[9., 0.],
[5., 2.],
[4., 8.]]),
array([[8., 2.],
[1., 9.],
[4., 0.],
[6., 2.]]),
array([[8., 3.],
[5., 7.],
[0., 7.],
[2., 1.]])]
‘‘‘
import numpy as np
j = np.floor(10*np.random.random((2, 12)))
print(j.T, np.vsplit(j.T, (3, 4)))
‘‘‘
[[6. 3.]
[9. 0.]
[5. 2.]
[4. 8.]
[8. 2.]
[1. 9.]
[4. 0.]
[6. 2.]
[8. 3.]
[5. 7.]
[0. 7.]
[2. 1.]]
----------------------------
[array([[6., 3.],
[9., 0.],
[5., 2.]]),
array([[4., 8.]]),
array([[8., 2.],
[1., 9.],
[4., 0.],
[6., 2.],
[8., 3.],
[5., 7.],
[0., 7.],
[2., 1.]])]
‘‘‘
na = np.arange(12)
nb = na
print(nb is na) # True
print(na.shape) # (12,)
print(id(na)) # 1946844535904
print(id(nb)) # 1946844535904
nc = na.view()
print(nc is na) # False
nc.shape = 2, 6
print(na.shape) # (12,)
nc[0, 4] = 1234
print(na) # [ 0 1 2 3 1234 5 6 7 8 9 10 11]
print(nc) # [[ 0 1 2 3 1234 5]
# [ 6 7 8 9 10 11]]
print(id(na)) # 1217619840656
print(id(nc)) # 1217619839776
nna = np.arange(12).reshape((2, -1))
nd = nna.copy() # False
print(nd is nna)
nd[0, 0] = 9999
print(nna)
‘‘‘
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
‘‘‘
print(nd)
‘‘‘
[[9999 1 2 3 4 5]
[ 6 7 8 9 10 11]]
‘‘‘
sre = np.sin(np.arange((20)).reshape(5, 4))
print(sre)
‘‘‘
[[ 0. 0.84147098 0.90929743 0.14112001]
[-0.7568025 -0.95892427 -0.2794155 0.6569866 ]
[ 0.98935825 0.41211849 -0.54402111 -0.99999021]
[-0.53657292 0.42016704 0.99060736 0.65028784]
[-0.28790332 -0.96139749 -0.75098725 0.14987721]]
‘‘‘
ind = sre.argmax(axis=0) # 列查找
print(ind)
‘‘‘
[2 0 3 1]
‘‘‘
sre_max = sre[ind, range(sre.shape[1])]
print(sre_max)
‘‘‘
[0.98935825 0.84147098 0.99060736 0.6569866 ]
ss = np.arange(0, 40, 10)
print(ss)
# [ 0 10 20 30]
pp = np.tile(ss, (3, 5))
print(pp)
‘‘‘
[[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]]
‘‘‘
e = np.array([[4, 3, 5], [6, 2, 1]])
print(e)
‘‘‘
[[4 3 5]
[6 2 1]]
‘‘‘
k = np.sort(e, axis=1)
print(k)
‘‘‘
[[3 4 5]
[1 2 6]]
‘‘‘
po = np.array([4, 3, 1, 2])
ol = np.argsort(po)
print(ol)
# 从小到大进行排列的索引值为 [2 3 1 0]
print(po[ol])
# 排序完成的数组为 [1 2 3 4]
原文:https://www.cnblogs.com/mapel1594184/p/14243509.html