Ipython

Ipython:一个性能强大的python终端

特点:一行一行执行,调试速度相对pycharm比较快

ipython(jupyter) notebook:集文本、代码、图像、公式的展现与一体的超级python web界面

ipython sell:功能强大的交互式shell

一、Numpy

注意:numpy默认nadarray的所有类型是相同的,如果传来的列表中包含不同的类型,则为同一类型,优先级 str>float>int

(numeric python)Numpy系统是python的一种开源的数值计算扩展

特点:一个强大的N维数组对象Array,

成熟的(广播)函数库,

用于整合C/C++和Fortran代码的工具包,

实用的线性代数,

傅里叶变换和随机数生成函数,

numpy和稀疏矩阵运算包scipy配合使用更强大

导入

import numpy as np (一般这样缩写)

查看版本号 np.__version__

#pyplot显示画图,数据分析可视化

import matplotlib.pyplot as plt

plt.imread('cat.png')读取图片

类型是numpy.ndarray

plt.imshow(cat)

plt.show()显示图片

创建ndarray

n1 = np.array([3,1,4,5])一维数组

 

n2 = np.array([[2,3,4,5],[3,4,6,7],[67,3,4,5]])二维数组

array([[ 2, 3, 4, 5], [ 3, 4, 6, 7], [67, 3, 4, 5]])

n2.shape 查看数组形状返回的是一个元组

结果(3,4)三行四列

n1.shape 结果(4,)因为只有一个4

cat = plt.imread('cat.png')

cat.shape (257, 417, 3) 高,宽,色彩

小结任何一张2维图片的转化成数据3维数组,长宽最后一维颜色

 

使用np的routines函数创建

包含以下常见创建方法

1)np.ones(shape,dtype=None,oreder='C')

np.ones(shape = (10,8),dtype=int)

结果:array([[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1]])

2)np.zeros(shape,dtype=float,order='C')

np.zeros((4,4),)

array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]])

3)np.full(shape,fill_value,dtype=None,order='C')

np.full((4,4),fill_value=1024)

array([[1024, 1024, 1024, 1024], [1024, 1024, 1024, 1024], [1024, 1024, 1024, 1024], [1024, 1024, 1024, 1024]])

4)np.eye(N,M=None,k=0,dtype=float)【无法再进行运算】(机械学习运算使用)

np.eye(10)【一元十次方程,满秩】

x + y = 10

2x+ 2y = 20

[[1,1],[2,2]]求不出来 x,y

array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])

5)np.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None)

#lin = linear 线性(分割数据)

np.linspace(0,100,20)

array([ 0. , 5.26315789, 10.52631579, 15.78947368, 21.05263158, 26.31578947, 31.57894737, 36.84210526, 42.10526316, 47.36842105, 52.63157895, 57.89473684, 63.15789474, 68.42105263, 73.68421053, 78.94736842, 84.21052632, 89.47368421, 94.73684211, 100. ])

6)np.arange([start,]stop,[step],dtype=None)

np.arange(0,100,3)[左闭右开]

array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99])

7)np.random.randint(low,high=None,size=None,dtype='l')

np.random.randint(0,150,size=5)#随机生成5个数范围0-150

8)np.random.randn(d0,d1,....,dn) 标准正太分布

array([0.5947149 , 0.3294103 , 0.57991633, 0.8852336 , 0.96530837, 0.71374175, 0.80512688, 0.53780773, 0.86183162, 0.94997268, 0.78236602, 0.75138055, 0.77436397, 0.62240548, 0.02360329, 0.8477814 , 0.38567406, 0.32622077, 0.04750335, 0.9434 , 0.53819817, 0.96430135, 0.83119207, 0.1613626 , 0.37307527, 0.43531505, 0.85773095, 0.68163937, 0.66828996, 0.14085551, 0.28603707, 0.17289119, 0.5412281 , 0.27044416, 0.94081946, 0.67541809, 0.83741093, 0.53472578, 0.11894584, 0.59381507, 0.8056224 , 0.68696387, 0.83690063, 0.0031504 , 0.12927811, 0.64202531, 0.67560029, 0.04389345, 0.32110887, 0.27624352, 0.67046739, 0.64403352, 0.74352604, 0.38510764, 0.81030634, 0.95848995, 0.88255054, 0.35610651, 0.25733846, 0.69120222, 0.49321505, 0.41149438, 0.21667289, 0.80595463, 0.67136948, 0.8275332 , 0.06461031, 0.04138162, 0.86898063, 0.59987602, 0.23816082, 0.45428312, 0.85555206, 0.55748032, 0.31206032, 0.53067284, 0.08617814, 0.17278818, 0.21213118, 0.48582689, 0.31414976, 0.94771537, 0.9727316 , 0.62461635, 0.37598786, 0.97531608, 0.79619861, 0.12745736, 0.16190435, 0.57421559, 0.04728805, 0.50383927, 0.98204842, 0.81231584, 0.45980568, 0.15299056, 0.99199238, 0.56887663, 0.44387714, 0.5033106 ])

 

9)​np.random.normal(loc=0.0,scale=1.0,size=None),正太锚点,波动系数,多大的值,值越大波动越剧烈 自定义标准正太

np.random.normal(loc = 175,scale = 1 ,size=100)

array([175.70304801, 174.0952638 , 176.35773036, 174.16579134, 174.7882515 , 175.6941854 , 176.00451738, 175.37028059, 174.52104817, 174.4779992 , 175.90006855, 174.30331654, 174.96182668, 173.52789394, 173.63881372, 175.63038361, 174.52243895, 175.88953986, 176.64942193, 176.689514 , 175.61452231, 174.46451231, 176.16968133, 175.31125365, 175.37372832, 174.83784144, 175.15812039, 176.2454188 , 174.66243601, 175.9809932 , 176.03013973, 175.61958932, 175.37934332, 176.47934739, 175.80884858, 172.8709663 , 175.5539523 , 173.78167371, 174.22452019, 175.23495255, 173.40320417, 175.79393948, 175.18603217, 176.00988476, 174.66148631, 175.53115698, 175.65352327, 175.23028617, 174.37738341, 175.06200274, 176.4390313 , 175.64823555, 173.13875546, 175.39262455, 174.91916835, 174.33968048, 176.19694789, 174.73114257, 175.74899067, 175.0487877 , 174.2616631 , 173.68820956, 174.79090568, 173.72070336, 175.48312489, 176.06018294, 175.67115914, 175.27238076, 173.60130376, 174.50145284, 175.38811631, 176.02534308, 176.01164145, 172.4058429 , 174.73228822, 175.25767038, 174.83671249, 173.3740476 , 175.74229654, 173.72357977, 173.54617159, 173.77015156, 175.67409929, 175.10254291, 175.53322904, 174.50538129, 174.75740618, 174.51032369, 173.85193552, 173.93766299, 175.93936517, 176.00246401, 173.90321825, 175.01166172, 174.35101296, 175.75242552, 175.81279617, 175.47608029, 175.37530525, 176.64182925])

 

10)np.random.random(size=(200,300,3))#可以随机生成一张图片

array([[[0.67253249, 0.82130528, 0.04209736], [0.49207207, 0.81722267, 0.6611615 ], [0.07413395, 0.60654644, 0.08149565], ..., [0.00436333, 0.34643531, 0.54882373], [0.77527579, 0.85391413, 0.19263193], [0.50493632, 0.89299445, 0.54739697]]])#中间部分省略了

ndim求数组的维度

1.索引

一维与列表完全一致 多维同理

n1 = np.random.randint(0,100,(3,4,5))

array([[[54, 97, 77, 25, 78], [16, 36, 36, 70, 99], [75, 42, 53, 56, 11], [56, 64, 65, 37, 72]], [[71, 2, 66, 83, 30], [21, 12, 41, 21, 35], [ 1, 35, 96, 93, 11], [45, 93, 29, 60, 12]], [[11, 7, 42, 7, 9], [13, 18, 47, 54, 56], [74, 61, 81, 72, 41], [35, 36, 30, 33, 23]]])

n1[0,3,1]

拿到64

2.切片

根据索引修改数据

一维与列表完全一致,多维同理(左闭右开)

array([[[13, 23, 98, 33, 41], [66, 55, 8, 32, 10], [64, 30, 68, 17, 37], [ 1, 70, 11, 71, 17]], [[56, 47, 37, 97, 29], [88, 7, 49, 23, 59], [68, 19, 12, 3, 98], [65, 9, 16, 55, 61]], [[65, 46, 73, 22, 12], [81, 44, 17, 60, 12], [83, 16, 25, 10, 86], [84, 56, 96, 20, 33]]])

n1[0:2,1:3,-2:-1]

array([[[32], [17]], [[23], [ 3]]])

 

逗号分开维度, 可以进行数组翻转

3.变形

使用reshape函数,注意参数是tuple!

n3 = np.arange(0,10,1)

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

n3.shape

n3.reshape(5,2)

array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])

cat

cat.shape

(257, 417, 3)

cat.reshape(257*417*3)

array([0.91764706, 0.7411765 , 0.5254902 , ..., 0.5019608 , 0.30588236, 0.22745098], dtype=float32)

 

如果reshape是负数直接转换为一个一维的数组 ndarray

4.级联

1.np.concatenate()【将两个数组连到一块】

2.级联的参数是列表:一定要加中括号或小括号

3.维度必须相同

4.形状相符

5.【重点】级联的方向默认是shape这个tuple的第一个值所代表的维度方向

6.可通过axis参数改变级联的方向

cat = plt.imread('cat.png')

plt.imshow(cat)

plt.show()

cats = np.concatenate((cat,cat))

plt.imshow(cats)

plt.show()

cats = np.concatenate((cat,cat))

plt.imshow(cats)

plt.show()

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐