python中有自己的数据类型.
numpy中的数据类型相对于python来说,更加的细致
In [2]:
#anaconda中自带numpy#如果要自己安装的话,解决依赖关系import numpy as npimport matplotlib.pyplot as pltIn [3]:
lj = plt.imread('onr.jpg')#在jupyter中如果不使用输出函数,那么最后一个变量会默认调用display()plt.imshow(lj)Out[3]:
<matplotlib.image.AxesImage at 0x279508f14e0>
数据库有瓶颈 MySQL IO操作的时候带来的
ndarray在速度上是一个整体存储的空间
In [30]:
#将其它的序列类型转变为ndarray类型np.array('abc')Out[30]:
array('abc', dtype='<U3')In [25]:
np.array([1,2,3,4,5])[0]Out[25]:
1In [26]:
np.array((1,2,3,4,5))[0]Out[26]:
1In [28]:
#set类型是序列?不是#无序的类型可以强制转换,但是不能使用np.array({1,2,3,4,5})Out[28]:
array({1, 2, 3, 4, 5}, dtype=object)In [33]:
np.array({'a':1,'b':2})Out[33]:
array({'a': 1, 'b': 2}, dtype=object)In [34]:
#对于可迭代类型np.array(range(10))Out[34]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])但是引用传地址在ndarray中不能正常的使用
In [40]:
li_ = [1,2,3,4,5]In [41]:
nd1 = np.array(li_)In [42]:
display(li_,nd1)[1, 2, 3, 4, 5]array([1, 2, 3, 4, 5])In [43]:
li_[0] = 0In [44]:
display(li_,nd1)[0, 2, 3, 4, 5]array([1, 2, 3, 4, 5])In [45]:
#拷贝传值a=1b=aa=2display(a,b)21In [46]:
#引用传地址l1=[1,2,3]l2=l1l1[0] = 10display(l1,l2)[10, 2, 3][10, 2, 3]In [51]:
# name sex age idCard #当前的数据是 4x4的矩阵 = 方阵sign = np.array(['name','sex','age','idCard'])arr = np.array([ ['tom','male',18,123456], ['jack','male',18,123456], ['liye','male',18,123456], ['lijing','male',18,123456],])In [52]:
signOut[52]:
array(['name', 'sex', 'age', 'idCard'], dtype='<U6')In [48]:
arrOut[48]:
array([['tom', 'male', '18', '123456'],
['jack', 'male', '18', '123456'],
['liye', 'male', '18', '123456'],
['lijing', 'male', '18', '123456']], dtype='<U6')low 范围的最小值high 范围的最大值size 数组的形状dtype 数据类型In [85]:
#2行3列 tinyint uint8 0-255# np.random.randint(-1000,1000,size=(2,3),dtype='uint8').itemsizenp.random.randint(-1000,1000,size=(2,3),dtype=int).itemsizeOut[85]:
8#随机小数#size 形状In [90]:
np.random.random((5,5))Out[90]:
array([[0.9710657 , 0.59959458, 0.17277926, 0.7564499 , 0.8199467 ],
[0.05728386, 0.43645809, 0.55643072, 0.24820215, 0.64320721],
[0.83246593, 0.46085084, 0.59217745, 0.64244586, 0.46066542],
[0.73132308, 0.17251823, 0.92862237, 0.13484279, 0.94449531],
[0.38324227, 0.21124364, 0.10590877, 0.85026129, 0.63822026]])#randn 是生成一个 标准的正态分布的数组 , 中间值是0 均值是0 标准差值为1#什么是正态分布?高斯分布d0, d1, ..., dn 代表的是多少维度In [95]:
np.random.randn(5,3)Out[95]:
array([[-0.62357575, 1.15439985, -1.65257655],
[ 1.97774917, -0.05774362, -0.77469661],
[-1.65905117, -1.24069957, 0.87832254],
[ 0.07512725, 0.00605548, 0.02697345],
[ 0.22584547, 1.48109243, -1.10517901]])#rand 只是用来生成一个随机的浮点数的In [100]:
np.random.rand(5,3)Out[100]:
array([[0.32596556, 0.16245572, 0.39403561],
[0.11270325, 0.83104786, 0.77568375],
[0.58643552, 0.26710588, 0.4707389 ],
[0.43680775, 0.37349157, 0.41696032],
[0.39967651, 0.69804483, 0.82111621]])#normal 生成一个自定义正太分布的数组loc=0.0 location 定位 中值scale=1.0 波动 size=None 数组的形状超过波动范围的值是异常值In [101]:
np.random.normal(100,10,10)Out[101]:
array([ 97.1131475 , 107.82816673, 99.37605262, 104.48124786,
110.60289338, 95.01571761, 100.66840617, 103.5317543 ,
85.9189614 , 92.54307587])In [102]:
A = np.array([[1,2,3],[7,8,9]])AOut[102]:
array([[1, 2, 3],
[7, 8, 9]])In [103]:
A.TOut[103]:
array([[1, 7],
[2, 8],
[3, 9]])In [107]:
np.dot(A.T,A)Out[107]:
array([[50, 58, 66],
[58, 68, 78],
[66, 78, 90]])# real imagIn [110]:
res = np.full(shape=(2,3),fill_value=0.123+0.345j)In [114]:
res.realOut[114]:
array([[0.123, 0.123, 0.123],
[0.123, 0.123, 0.123]])In [116]:
res.imagOut[116]:
array([[0.345, 0.345, 0.345],
[0.345, 0.345, 0.345]])#nbytesIn [126]:
o = np.ones(shape=(3,5),dtype=np.int8)In [127]:
oOut[127]:
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]], dtype=int8)In [128]:
o.shapeOut[128]:
(3, 5)In [129]:
o.nbytesOut[129]:
15In [130]:
o.dtypeOut[130]:
dtype('int8')In [133]:
#对原数据不产生影响o = o.astype(np.float)#stridesIn [139]:
z = np.zeros(shape=(4,3))zOut[139]:
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])In [140]:
z.itemsizeOut[140]:
8In [141]:
z.nbytesOut[141]:
96In [142]:
#8指的是每一个元素所占用的字节数#32 指的是行向量所占用的字节数z.stridesOut[142]:
(24, 8)In [161]:
#N 是 阶#k = 2 代表对角线向上偏移两格位置#k = -1 代表对角线向下偏移一格位置I = np.eye(3)IOut[161]:
array([[0., 0., 0.],
[0., 0., 0.],
[1., 0., 0.]])In [146]:
A = np.random.randint(1,10,(3,3))AOut[146]:
array([[4, 9, 7],
[4, 6, 5],
[9, 6, 5]])In [148]:
np.dot(I,A)Out[148]:
array([[4., 9., 7.],
[4., 6., 5.],
[9., 6., 5.]])In [156]:
#v 代表对角线上的值,这个值必须是一个向量np.diag([1,0.5,0.3])Out[156]:
array([[1. , 0. , 0. ],
[0. , 0.5, 0. ],
[0. , 0. , 0.3]])三角矩阵 上三角 下三角In [168]:
a = np.eye(3,k=-2)aOut[168]:
array([[0., 0., 0.],
[0., 0., 0.],
[1., 0., 0.]])In [167]:
b = np.eye(3,k=-1)bOut[167]:
array([[0., 0., 0.],
[1., 0., 0.],
[0., 1., 0.]])In [166]:
c = np.eye(3)cOut[166]:
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])In [169]:
a+b+cOut[169]:
array([[1., 0., 0.],
[1., 1., 0.],
[1., 1., 1.]])稀疏矩阵 希松 松散大部分的值是0,小部分的位置有其它值的文本处理In [171]:
np.array([[0,0,0,1],[0,0,0,0],[0,2,0,0],[0,0,0,0]])Out[171]:
array([[0, 0, 0, 1],
[0, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 0]])axis参数 轴axis = 0 行合并 yaxis = 1 列合并 xIn [177]:
np.ones(shape=(4,5)).sum(axis=1)Out[177]:
array([5., 5., 5., 5.])In [182]:
A = np.random.randint(1,10,(4,5))AOut[182]:
array([[6, 4, 4, 8, 3],
[9, 1, 7, 6, 8],
[8, 6, 9, 9, 2],
[4, 2, 3, 2, 6]])In [185]:
np.mean(A,axis=-1) Out[185]:
array([5. , 6.2, 6.8, 3.4])In [196]:
#3 rgblj.shapeOut[196]:
(483, 483, 3)In [4]:
#黑白图片 还是二维的 透光率 jpg 0-255 png 0-1#需要降低维度#np.sum() ndarray.sum()#color map#bug jpg 0-255 [243,200,220]plt.imshow(lj.sum(axis=2),cmap='gray')Out[4]:
<matplotlib.image.AxesImage at 0x27950987588>
In [5]:
plt.imshow(lj.max(axis=-1),cmap='gray')Out[5]:
<matplotlib.image.AxesImage at 0x279509e4400>
In [6]:
plt.imshow(lj.min(axis=-1),cmap='gray')Out[6]:
<matplotlib.image.AxesImage at 0x27950a403c8>
In [7]:
plt.imshow(lj.mean(axis=-1),cmap='gray')Out[7]:
<matplotlib.image.AxesImage at 0x27950bcef60>
In [8]:
plt.imshow(lj)Out[8]:
<matplotlib.image.AxesImage at 0x27950aa7940>
In [12]:
plt.imshow(lj[0:300,60:280])Out[12]:
<matplotlib.image.AxesImage at 0x27951d5ab70>
In [ ]: