深度学习基础

本文介绍了Python中NumPy库的基本使用,包括创建数组、矩阵操作和元素访问。同时,详细展示了如何利用Matplotlib进行数据可视化,绘制简单图形以及添加轴标签、图例等元素。此外,还演示了读取图片并显示的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第1章 Python入门

1.1 NumPy

np.array() 接收Python列表作为参数,生成NumPy数组。

>>> import numpy as np
>>> x = np.array([1.0, 2.0, 3.0])
>>> x
array([1., 2., 3.])
>>> type(x)
<class 'numpy.ndarray'>

1.1.1 广播

>>> x = np.array([1.0, 2.0 ,3.0])
>>> x / 2.0
array([0.5, 1. , 1.5])

1.1.2 矩阵

>>> x = np.array([[1, 2], [3, 4]])
>>> x.shape
(2, 2)
>>> x.dtype
dtype('int64')

shape 显示矩阵的形状

dtype 显示矩阵的元素类型

>>> A = np.array([[1, 2], [3, 4]])
>>> B = np.array([10, 20])
>>> A * B
array([[10, 40],
[30, 80]])

B是一维数组,扩展成了二维数组,和A的形状一致。

1.1.3 访问元素

下标访问

>>> X = np.array([[51, 55], [14, 19], [0, 4]])
>>> X
array([[51, 55],
[14, 19],
[ 0, 4]])
>>> X[0]
array([51, 55])
>>> X[0][1]
55

for方式访问

>>> for row in X:
... print(row)
...
[51 55]
[14 19]
[0 4]

1.1.4 拍扁成一维数组

>>> X = X.flatten()
>>> X
array([51, 55, 14, 19, 0, 4])

1.1.5 数组访问元素

>>> X[np.array([0, 2, 4])]
array([51, 14, 0])

抽出大于15的元素

>>> X > 15
array([ True, True, False, True, False, False])
>>> X[X > 15]
array([51, 55, 19])

1.2 Matplotlib

使用matplotlib.pyplot

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.arange(0, 6, 0.1) # 以0.1为单位,生成0到6的数据
>>> y = np.sin(x)
>>> plt.plot(x, y)
[<matplotlib.lines.Line2D object at 0x11be611f0>]
>>> plt.show()

pyplot的丰富功能

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.arange(0, 6, 0.1)
>>> y1 = np.sin(x)
>>> y2 = np.cos(x)
>>> plt.plot(x, y1, label = "sin")
[<matplotlib.lines.Line2D object at 0x11c498790>]
>>> plt.plot(x, y2, linestyle = "--", label = "cos")
[<matplotlib.lines.Line2D object at 0x11c498d90>]
>>> plt.xlabel("x")
Text(0.5, 0, 'x')
>>> plt.ylabel("y"
Text(0, 0.5, 'y')
>>> plt.title('sin & cos')
Text(0.5, 1.0, 'sin & cos')
>>> plt.legend()
<matplotlib.legend.Legend object at 0x11c4853d0>
>>> plt.show()

使用matplotlib.image的imread

>>> import matplotlib.pyplot as plt
>>> from matplotlib.image import imread
>>> img = imread('./Desktop/auto_man.jpeg')
>>> plt.imshow(img)
<matplotlib.image.AxesImage object at 0x11b35dee0>
>>> plt.show()

第2章 感知机

2.1 感知机是什么

感知机接收多个输入信号,输出一个信号。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值