鱼书学习--python入门
深度学习入门基于Python的理论与实现pdf链接深度学习入门基于Python的理论与实现.pdf · xiaomingbreaklimit/电子书收藏 - Gitee.com安装3.x的python版本需要两个外部库NumPy库科学计算的基础库它为Python提供了高性能的多维数组对象以及处理这些数组的工具Matplotlib库数据可视化库创建静态、动画和交互式图表的Python 2D绘图库python解释器基础运算和类型PS C:\Users\w30070889\Desktop python Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32 Type help, copyright, credits or license for more information. 5/2 2.5 5%2 1 5//2 2 2*10 20 2**10 1024 type(hi) Traceback (most recent call last): File stdin, line 1, in module NameError: name hi is not defined type(hi) class str type(hi) class str type(10) class int type(3.14) class float x 1.11111111111111111111111111111111111111111111111111 print(x) 1.1111111111111112 # float是8字节存储c的double类型会精度丢失 type(x) class float x100 type(x) class int y3.14 type(y) class float zx*y print(z) 314.0 type(z) class float import os os.system(cls) exit() PS C:\Users\w30070889\DesktopPython是属于“动态类型语言”的编程语言是指变量的类型是根据情况自动决定的。整数和小数相乘的结果是小数数据类型的自动转换。“#”是注释的意思它后面的文字会被Python忽略。列表和切片 vector[1,4,3,3,2,2,3] type(vector) class list print(vector) [1, 4, 3, 3, 2, 2, 3] len(vector) 7 vector[0]666 # 索引从0开始 print(vector) [666, 4, 3, 3, 2, 2, 3] vector[1:6] # 列表的切片用于显示部分列表1和6是索引取出索引1到索引6的子列表[1,6) [4, 3, 3, 2, 2] vector[0:6] [666, 4, 3, 3, 2, 2] vector[:6] # 前面索引为空表示[0,6) [666, 4, 3, 3, 2, 2] vector[1:7] [4, 3, 3, 2, 2, 3] vector[1:] # 后面索引为空表示[1,len(vector))最后一个元素也要带上 [4, 3, 3, 2, 2, 3] vector[:-1] # -1表示最后一个索引 [666, 4, 3, 3, 2, 2] vector[:-2] # -2表示倒数第二个索引 [666, 4, 3, 3, 2]字典 map{apple:1,banana:2} type(map) class dict map[apple] 1 map[apple]5 print(map) {apple: 5, banana: 2} map[orange]3 print(map) {apple: 5, banana: 2, orange: 3}布尔及运算 youJumpTrue type(youJump) class bool iJumpFalse youJump True not youJump False youJump and iJump False youJump or iJump Trueif、for语句函数 for i in [1,2,3]: ... print(i) ... 1 2 3 youjumpTrue if youJump: ... print(I\ll jump) ... else: ... print(If you dont jump, I wont jump either) ... Ill jump def myPrint(object): ... print(use myPrint:object.) ... myPrint(hello world) use myPrint:hello world. myPrint(youJump) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 2, in myPrint TypeError: can only concatenate str (not bool) to strpython的缩进是有实际的语法意义的相当于C中的{}表示代码块的作用是被重载的但是两边的类型必须一致Windows的情况下输入Ctrl-Z然后按Enter键可以退出python解释器接下来使用python脚本文件介绍类class Man: def __init__(self, name, age): self.name name self.age age print(Initialized!) def hello(self): print(Hello self.name , age str(self.age) !) def goodbye(self): print(Good-bye self.name !) m Man(David, 15) m.hello() m.goodbye()~\Desktop\python学习\demo python test.py Initialized! Hello David, age 15! Good-bye David!内置类型可由type()查看的类型不用import可直接用的类型其他类型标准库类型三方库类型自定义class类型numpy库 import numpy as np xnp.array([1.0,2.0,3.0]) x array([1., 2., 3.]) print(x) [1. 2. 3.] type(x) class numpy.ndarray ynp.array([10.0,20.0,30.0]) xy array([11., 22., 33.]) x-y array([ -9., -18., -27.]) x*y array([10., 40., 90.]) x/y array([0.1, 0.1, 0.1]) x*2 array([2., 4., 6.]) znp.array([1.0]) xz array([2., 3., 4.]) znp.array([1.0,2.0]) xz Traceback (most recent call last): File stdin, line 1, in module ValueError: operands could not be broadcast together with shapes (3,) (2,) x.shape (3,) Anp.array([[1,2],[3,4]]) A array([[1, 2], [3, 4]]) print(A) [[1 2] [3 4]] A.shape (2, 2) Bnp.array([[10,20],[30,40]]) AB array([[11, 22], [33, 44]]) A*B array([[ 10, 40], [ 90, 160]]) A*2 array([[2, 4], [6, 8]]) Cnp.array([[1,2,3],[4,5,6]]) C.shape (2, 3) Cnp.array([[[1,2,3],[4,5,6]]]) C.shape (1, 2, 3)张量就是“多维数组”的通用叫法。把张量理解为数据容器它的“维度”决定了数据的复杂程度0维张量标量一个单独的数字。例如np.array(3.14)形状是()。1维张量向量一列数字。例如你刚才用的np.array([1.0, 2.0, 3.0])形状是(3,)。2维张量矩阵一个表格。例如你试图创建的np.array([[1,2],[3,4]])形状是(2, 2)。3维张量及以上多个表格堆叠。例如一张彩色图片高、宽、RGB通道形状是(H, W, 3)或者连续的多张图片形状是(N, H, W, 3)。NumPy的广播功能使得符合某些规则的不同形状数组之间也可以顺利进行运算当两个数组进行运算时NumPy 会从尾部最右侧维度开始向前逐一比较它们的形状shape当维度不够时左边自动补1两个维度必须满足以下其中一条才能进行广播相等其中一个是 1缺失的维度视为 1如果比较到某一维既不相等也没有一个是 1则报错ValueError。 Cnp.array([[[1,2,3],[4,5,6]]]) C.shape (1, 2, 3) Dnp.array([1,2,3]) C*D array([[[ 1, 4, 9], [ 4, 10, 18]]]) D.shape (3,) Dnp.array([[1],[2],[3]]) D.shape (3, 1) C*D Traceback (most recent call last): File stdin, line 1, in module ValueError: operands could not be broadcast together with shapes (1,2,3) (3,1)元素访问 import numpy as np Xnp.array([[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[13,14,15,16],[17,18,19,20],[21,22,23,24]]]) print(X) [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] [[13 14 15 16] [17 18 19 20] [21 22 23 24]]] X.shape (2, 3, 4) type(X) class numpy.ndarray X[0] array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) X[0][1] array([5, 6, 7, 8]) X[0][1][2] np.int64(7) for block in X: ... for row in block: ... print(row) ... [1 2 3 4] [5 6 7 8] [ 9 10 11 12] [13 14 15 16] [17 18 19 20] [21 22 23 24] X[[1]] array([[[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]]) X[[1]].shape (1, 3, 4) X.shape (2, 3, 4) YX.flatten() print(Y) [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24] print(np.array([0,2,4])) [0 2 4] Y[np.array([0,2,4])] array([1, 3, 5]) print(Y%20) [False True False True False True False True False True False True False True False True False True False True False True False True] Y[Y%20] array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]) (Y%20).dtype dtype(bool) Y.dtype dtype(int64) X.dtype dtype(int64)张量可以通过传入int类型向量获取对应索引的元素子张量张量通过比较运算可以获得一个布尔数组可以通过传入布尔向量取出对应位置的元素布尔数组的元素个数应该等于shape元组最左边维度的大小.dtype获取张量中元素的类型list和dict用不了dtype甚至二者中的元素类型都可以不一样 vector[1,2,3] type(vector) class list vector.dtype Traceback (most recent call last): File stdin, line 1, in module AttributeError: list object has no attribute dtype vector[1,1.0,1] vector[2] 1 map{hello:world,1:2} map[hello] world map.dtype Traceback (most recent call last): File stdin, line 1, in module AttributeError: dict object has no attribute dtypematplotlib库 import numpy as np import matplotlib.pyplot as plt xnp.arange(-3,3,0.1) ynp.sin(x) x array([-3.00000000e00, -2.90000000e00, -2.80000000e00, -2.70000000e00, -2.60000000e00, -2.50000000e00, -2.40000000e00, -2.30000000e00, -2.20000000e00, -2.10000000e00, -2.00000000e00, -1.90000000e00, -1.80000000e00, -1.70000000e00, -1.60000000e00, -1.50000000e00, -1.40000000e00, -1.30000000e00, -1.20000000e00, -1.10000000e00, -1.00000000e00, -9.00000000e-01, -8.00000000e-01, -7.00000000e-01, -6.00000000e-01, -5.00000000e-01, -4.00000000e-01, -3.00000000e-01, -2.00000000e-01, -1.00000000e-01, 2.66453526e-15, 1.00000000e-01, 2.00000000e-01, 3.00000000e-01, 4.00000000e-01, 5.00000000e-01, 6.00000000e-01, 7.00000000e-01, 8.00000000e-01, 9.00000000e-01, 1.00000000e00, 1.10000000e00, 1.20000000e00, 1.30000000e00, 1.40000000e00, 1.50000000e00, 1.60000000e00, 1.70000000e00, 1.80000000e00, 1.90000000e00, 2.00000000e00, 2.10000000e00, 2.20000000e00, 2.30000000e00, 2.40000000e00, 2.50000000e00, 2.60000000e00, 2.70000000e00, 2.80000000e00, 2.90000000e00]) y array([-1.41120008e-01, -2.39249329e-01, -3.34988150e-01, -4.27379880e-01, -5.15501372e-01, -5.98472144e-01, -6.75463181e-01, -7.45705212e-01, -8.08496404e-01, -8.63209367e-01, -9.09297427e-01, -9.46300088e-01, -9.73847631e-01, -9.91664810e-01, -9.99573603e-01, -9.97494987e-01, -9.85449730e-01, -9.63558185e-01, -9.32039086e-01, -8.91207360e-01, -8.41470985e-01, -7.83326910e-01, -7.17356091e-01, -6.44217687e-01, -5.64642473e-01, -4.79425539e-01, -3.89418342e-01, -2.95520207e-01, -1.98669331e-01, -9.98334166e-02, 2.66453526e-15, 9.98334166e-02, 1.98669331e-01, 2.95520207e-01, 3.89418342e-01, 4.79425539e-01, 5.64642473e-01, 6.44217687e-01, 7.17356091e-01, 7.83326910e-01, 8.41470985e-01, 8.91207360e-01, 9.32039086e-01, 9.63558185e-01, 9.85449730e-01, 9.97494987e-01, 9.99573603e-01, 9.91664810e-01, 9.73847631e-01, 9.46300088e-01, 9.09297427e-01, 8.63209367e-01, 8.08496404e-01, 7.45705212e-01, 6.75463181e-01, 5.98472144e-01, 5.15501372e-01, 4.27379880e-01, 3.34988150e-01, 2.39249329e-01]) type(x) class numpy.ndarray type(y) class numpy.ndarray x.dtype dtype(float64) y.dtype dtype(float64) plt.plot(x,y) [matplotlib.lines.Line2D object at 0x000002003BADCF50] plt.show()三角函数import numpy as np import matplotlib.pyplot as plt plt.rcParams[font.sans-serif] [SimHei] # 指定默认字体为黑体[reference:5][reference:6] plt.rcParams[axes.unicode_minus] False # 解决负号 - 显示为方块的问题[reference:7][reference:8] # 使用 arange注意终点要略大于 pi因为 arange 不包含终点 x np.arange(-np.pi / 2, np.pi / 2 0.001, 0.001) print(x) y_sin np.sin(x) y_cos np.cos(x) y_tan_limit 2 # 限制y轴的范围否则图形会很难看 x_tan x[(np.tan(x) y_tan_limit) (np.tan(x) -y_tan_limit)] # 这里不能用and要用and的话表示两个向量进行运算向量长度还不一定一样 y_tan np.tan(x_tan) plt.plot(x, y_sin, labelsin) plt.plot(x, y_cos, labelcos, linestyle--) plt.plot(x_tan, y_tan, labeltan, linestyle:) plt.xlabel(x轴) plt.ylabel(y轴) plt.title(三角函数) plt.legend() plt.show()显示图像import numpy as np import matplotlib.pyplot as plt from matplotlib.image import imread imgimread(仪玄.png) print(img.shape) print(img) plt.imshow(img) plt.show()本章所学的内容• Python是一种简单易记的编程语言。• Python是开源的可以自由使用。• 本书中将使用Python 3.x实现深度学习。• 本书中将使用NumPy和Matplotlib这两种外部库。• Python有“解释器”和“脚本文件”两种运行模式。• Python能够将一系列处理集成为函数或类等模块。• NumPy中有很多用于操作多维数组的便捷方法。