Files
task-3-2-1-Text-Processing-…/hh.py
2026-04-21 11:23:58 +08:00

65 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 文本在计算机中的存储方式
text = "Hello"
# 如果我们看它的"数字形式"
print([ord(c) for c in text])
# 输出: [72, 101, 108, 108, 111]
# 72='H', 101='e', 108='l', 111='o'
# 中文例子
text_cn = "你好"
print([ord(c) for c in text_cn])
# 输出: [20320, 22909]
# 20320='你', 22909='好'
import numpy as np
# 一维向量
v1 = np.array([3]) # 只有1个数字
print(f"v1 = {v1}")
# 二维向量
v2 = np.array([2, 3]) # 2个数字表示平面上的一个点
print(f"v2 = {v2}")
# 三维向量
v3 = np.array([1, 2, 3]) # 3个数字表示立体空间的一个点
print(f"v3 = {v3}")
# 更多维向量(机器学习中常用)
v100 = np.array([0.1, 0.5, -0.3, 0.8, ...]) # 100维
print(f"v100有 {len(v100)} 个元素")
import numpy as np
# 向量加法:对应位置相加
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b # [1+4, 2+5, 3+6] = [5, 7, 9]
print(f"a + b = {c}") # [5, 7, 9]
# 直观理解:
# a = [1, 2, 3] 从原点出发走1步、再走2步、再走3步
# b = [4, 5, 6] 从原点出发走4步、再走5步、再走6步
# a + b = 从原点走完a再走b = [5, 7, 9]
# 向量乘以一个数字(标量)
v = np.array([1, 2, 3])
result = v * 2 # [1*2, 2*2, 3*2] = [2, 4, 6]
print(f"v * 2 = {result}") # [2, 4, 6]
# 直观理解:
# v = [1, 2, 3] 表示"方向"
# v * 2 = [2, 4, 6] 方向不变长度变成2倍
# 两个向量对应位置相乘,然后加起来
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# 点积计算过程:
# 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
dot = np.dot(a, b)
print(f"点积 = {dot}") # 32
# 或者用 @ 运算符
print(f"a @ b = {a @ b}") # 32