【DL学习笔记01】深度学习入门——基于Python的理论与实现(ch01&02:Python入门&perceptron)

本文介绍了Python科学计算库NumPy的基础用法,包括数组操作、矩阵运算和广播机制;接着展示了数据可视化库Matplotlib的图形绘制功能,包括简单图形、多条曲线图和图像显示;最后讲解了感知机的概念,通过实例演示了如何实现简单的逻辑运算。
摘要由CSDN通过智能技术生成

目录

NumPy

Matplotlib

感知机

NumPy

# 导入NumPy
import numpy as np

# np.array()接受Python列表作为参数,生成NumPy数组(numpy.ndarray)
x = np.array([1.0, 2.0, 3.0])
print(x)
print(type(x))

# 算法运算
# 注:元素个数相同时,可以对各个元素进行算数运算。如果元素个数不同,程序就会报错
x = np.array([1.0, 2.0, 3.0])
y = np.array([2.0, 4.0, 6.0])
print(x + y)
print(type(x + y))
print(x - y)
print(type(x - y))
print(x * y)
print(type(x * y))
print(x / y)
print(type(x / y))
# 广播
print(x / 2.0)
print(type(x / 2.0))

# 生成N维数组——矩阵
A = np.array([[3, 0], [0, 6]])
print(A)
print(A.shape)  # 查看矩阵A的形状
print(A.dtype)  # 查看矩阵元素的数据类型

# 矩阵的运算
B = np.array([[3, 0], [0, 6]])
print(A + B)
print(A * B)
print(A * 10)

# 广播
# NumPy中,形状不同的数组之间也可以进行运算
A = np.array([[1, 2], [3, 4]])
B = np.array([10, 20])
print(A * B)

# 访问元素
# 注:元素的索引从0开始
X = np.array([[51, 55], [14, 19], [0, 4]])
print(X)
print(X[0])  # 第0行
print(X[0][1])  # (0,1)的元素
# for语句访问各个元素。
for row in X:
    print(row)
# NumPy还可以使用数组访问各个元素
X = X.flatten()  # 将X转换为一维数组
print(X)
print(X[np.array([0, 2, 4])])  # 获取索引为0、 2、 4的元素
print(X > 15)  # 从X中抽出大于15的元素,得到一个布尔型的数组
print(X[X > 15])  # 从X中抽出大于15的元素,取出True对应的元素

Matplotlib

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import imread

print("#####绘制简单图形#####")
# 生成数据
x = np.arange(0, 6, 0.1)
y = np.sin(x)
# 绘制图形
plt.plot(x, y)
plt.show()
print("#####pyplot的功能#####")
# 生成数据
y1 = np.sin(x)
y2 = np.cos(x)
# 绘制图形
plt.plot(x, y1, label="sin")
plt.plot(x, y2, linestyle="--", label="cos")  # 用虚线绘制
plt.xlabel("x")  # x轴标签
plt.ylabel("y")  # y轴标签
plt.title('sin&cos')  # 标题
plt.legend()
plt.show()
print("#####显示图像#####")
img = imread('2670536155_c170f49cd0.jpg')
plt.imshow(img)
plt.show()

感知机

# 感知机接收多个输入信号,输出一个信号
# 感知机的信号只有0/1两种取值
import numpy as np

print("###simple realize###")

# 与门
def AND1(x1, x2):
    w1, w2, theta = 0.5, 0.5, 0.7
    tmp = x1 * w1 + x2 * w2
    if tmp <= theta:
        return 0
    elif tmp > theta:
        return 1

for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
    y = AND1(xs[0], xs[1])
    print(str(xs) + " -> " + str(y))

# 使用权重和偏置实现与门
def AND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.7
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
    y = AND(xs[0], xs[1])
    print(str(xs) + " -> " + str(y))

def OR(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.2
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

def NAND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([-0.5, -0.5])
    b = 0.7
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1

print("###多层感知机###")

def XOR(x1, x2):
    s1 = NAND(x1, x2)
    s2 = OR(x1, x2)
    y = AND(s1, s2)
    return y

for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
    y = XOR(xs[0], xs[1])
    print(str(xs) + " -> " + str(y))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值