上传文件至 /

This commit is contained in:
2026-04-23 16:02:54 +08:00
parent c39c151794
commit f6fc4b5fc6

158
hh.py
View File

@@ -1,64 +1,128 @@
# 文本在计算机中的存储方式 # 实际演示:查看字符的编码值
text = "Hello"
# 如果我们看它的"数字形式" # 英文例子
print([ord(c) for c in text]) text_en = "Hello"
# 输出: [72, 101, 108, 108, 111] print("=" * 50)
# 72='H', 101='e', 108='l', 111='o' print("英文文本的字符编码")
print("=" * 50)
print(f"文本: {text_en}")
print(f"每个字符的ASCII码: {[ord(c) for c in text_en]}")
print()
# 逐个显示
for c in text_en:
print(f" '{c}' -> {ord(c)}")
print()
print("=" * 50)
print("中文文本的字符编码")
print("=" * 50)
# 中文例子 # 中文例子
text_cn = "你好" text_cn = "你好"
print([ord(c) for c in text_cn]) print(f"文本: {text_cn}")
# 输出: [20320, 22909] print(f"每个字符的UTF-8编码值: {[ord(c) for c in text_cn]}")
# 20320='你', 22909='好' print()
# 逐个显示
for c in text_cn:
print(f" '{c}' -> {ord(c)}")
# 用chr()函数反向验证:数字编码转字符
print("验证:数字编码转字符")
print()
# 65是大写字母A
print(f"chr(65) = '{chr(65)}' # 应该是大写字母 A")
# 97是小写字母a
print(f"chr(97) = '{chr(97)}' # 应该是小写字母 a")
# 20013是中文"中"
print(f"chr(20013) = '{chr(20013)}' # 应该是中文''")
# 25991是中文"文"
print(f"chr(25991) = '{chr(25991)}' # 应该是中文''")
print("=" * 50)
print("练习题1答案")
print("=" * 50)
# 1. 用 ord() 函数打印 "Hello" 每个字符的ASCII码
print("1. 'Hello' 的ASCII码:")
print([ord(c) for c in "Hello"])
# 2. 验证字符65对应大写字母A
print()
print("2. 验证 chr(65):")
print(f"chr(65) = '{chr(65)}'")
# 验证范围
print()
print("验证 A-Z 的ASCII码范围 (65-90):")
print([chr(i) for i in range(65, 91)])
import numpy as np import numpy as np
# 一维向量 print("=" * 50)
v1 = np.array([3]) # 只有1个数字 print("NumPy向量创建演示")
print(f"v1 = {v1}") print("=" * 50)
# 维向量 # 维向量只有1个数字
v2 = np.array([2, 3]) # 2个数字表示平面上的一个点 v1 = np.array([3])
print(f"v2 = {v2}") print(f"一维向量 v1 = {v1}")
print(f"v1 有 {len(v1)} 个元素")
# 维向量 # 维向量2个数字表示平面上的一个点
v3 = np.array([1, 2, 3]) # 3个数字表示立体空间的一个点 v2 = np.array([2, 3])
print(f"v3 = {v3}") print(f"\n二维向量 v2 = {v2}")
print(f"v2 有 {len(v2)} 个元素")
# 更多维向量(机器学习中常用 # 维向量(3个数字表示立体空间的一个点
v100 = np.array([0.1, 0.5, -0.3, 0.8, ...]) # 100维 v3 = np.array([1, 2, 3])
print(f"v100有 {len(v100)} 个元素") print(f"\n三维向量 v3 = {v3}")
print(f"v3 有 {len(v3)} 个元素")
# 高维向量(机器学习中常用,几十维到几千维)
v10 = np.array([0.1, 0.5, -0.3, 0.8, 0.2, -0.1, 0.7, 0.3, -0.2, 0.6])
print(f"\n10维向量 v10 = {v10}")
print(f"v10 有 {len(v10)} 个元素")
import numpy as np import numpy as np
import matplotlib.pyplot as plt
# 向量加法:对应位置相加 # 设置中文字体(如果系统有的话)
a = np.array([1, 2, 3]) try:
b = np.array([4, 5, 6]) plt.rcParams['font.sans-serif'] = ['SimHei', 'Noto Sans CJK SC', 'WenQuanYi Micro Hei']
c = a + b # [1+4, 2+5, 3+6] = [5, 7, 9] plt.rcParams['axes.unicode_minus'] = False
except:
pass # 如果没有中文字体就用默认
print(f"a + b = {c}") # [5, 7, 9] # 创建画布
fig, ax = plt.subplots(figsize=(8, 8))
# 直观理解: # 定义向量
# a = [1, 2, 3] 从原点出发走1步、再走2步、再走3步 vectors = {
# b = [4, 5, 6] 从原点出发走4步、再走5步、再走6步 'A = [2, 3]': np.array([2, 3]),
# a + b = 从原点走完a再走b = [5, 7, 9] 'B = [4, 1]': np.array([4, 1]),
# 向量乘以一个数字(标量) 'C = [1, 1]': np.array([1, 1]),
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] # 画每个向量
colors = ['red', 'blue', 'green']
for (name, vec), color in zip(vectors.items(), colors):
ax.annotate('', xy=vec, xytext=(0, 0),
arrowprops=dict(arrowstyle='->', color=color, lw=2))
ax.text(vec[0]+0.1, vec[1]+0.1, name, fontsize=12, color=color)
# 直观理解: # 画坐标系
# v = [1, 2, 3] 表示"方向" ax.axhline(y=0, color='black', linewidth=0.5)
# v * 2 = [2, 4, 6] 方向不变长度变成2倍 ax.axvline(x=0, color='black', linewidth=0.5)
# 两个向量对应位置相乘,然后加起来
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# 点积计算过程: # 设置范围
# 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32 ax.set_xlim(-0.5, 5.5)
ax.set_ylim(-0.5, 4)
ax.set_xlabel('x (abscissa)', fontsize=12)
ax.set_ylabel('y (ordinate)', fontsize=12)
ax.set_title('2D Vector Visualization', fontsize=14)
ax.grid(True, alpha=0.3)
ax.set_aspect('equal')
dot = np.dot(a, b) plt.show()
print(f"点积 = {dot}") # 32 print("Note: Arrows represent vectors. Endpoint of arrow = vector endpoint")
# 或者用 @ 运算符
print(f"a @ b = {a @ b}") # 32