从基础理论到前沿应用,全面掌握人工智能核心技术
理解并处理文本、图像、视频、音频等多种模态,实现真正的全方位理解能力
自主完成复杂任务的智能代理,具备规划、执行、学习和适应能力
确保AI系统安全、可靠、符合人类价值观,建立完善的安全机制
在移动设备、IoT等边缘端高效运行AI模型,实现低延迟、高隐私保护
接近人类水平的通用人工智能,具备跨领域学习和问题解决能力
让AI决策过程透明可理解,建立用户信任和监管合规
机器学习、深度学习、神经网络核心原理
数据处理、科学计算、可视化
线性回归、逻辑回归、决策树
PyTorch、TensorFlow、JAX实战
Transformer架构、预训练、微调
CLIP、Diffusion、多模态对齐
LangChain、Agent框架、工具集成
模型压缩、推理加速、分布式训练
MLOps、模型部署、监控维护
构建具备记忆、工具调用能力的对话AI,集成RAG和知识库
实现文本到图像、视频的生成,支持风格控制和编辑
开发能够自主规划、执行复杂任务的AI代理系统
自动化数据探索、分析和可视化报告生成
AI与物理世界的深度融合,机器人、自动驾驶等领域迎来突破
从AI辅助转向AI原生,软件、硬件全面重新设计
小参数模型性能大幅提升,端侧部署成为主流
全球AI安全框架建立,合规性成为必需
每个用户拥有专属的AI助手,深度理解个人需求
多个AI代理协同工作,形成智能生态系统
从基础到实战,全面掌握Python编程技能
# Python基础语法示例
# 变量定义和数据类型
name = "Python"
age = 25
price = 19.99
is_active = True
data_list = [1, 2, 3, 4, 5]
data_dict = {"name": "Python", "version": "3.9"}
# 条件语句
if age >= 18:
print("成年人")
else:
print("未成年人")
# 循环语句
# for循环
for i in range(5):
print(f"当前数字: {i}")
# while循环
count = 0
while count < 3:
print(f"计数: {count}")
count += 1
# 函数定义
def greet(name, message="Hello"):
"""
问候函数
参数: name - 姓名, message - 问候语
返回: 问候字符串
"""
return f"{message}, {name}!"
# 列表推导式
squares = [x**2 for x in range(10)]
print(f"平方列表: {squares}")
# 字典推导式
square_dict = {x: x**2 for x in range(5)}
print(f"平方字典: {square_dict}")
# 类定义
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"我是{self.name},今年{self.age}岁"
# 异常处理
try:
result = 10 / 0
except ZeroDivisionError:
print("不能除以零")
finally:
print("程序继续执行")
# 文件操作
with open('example.txt', 'w', encoding='utf-8') as f:
f.write("Hello, Python!")
with open('example.txt', 'r', encoding='utf-8') as f:
content = f.read()
print(f"文件内容: {content}")
# 数据处理示例 - 使用Pandas库
import pandas as pd
import numpy as np
# 创建DataFrame
data = {
'姓名': ['张三', '李四', '王五', '赵六'],
'年龄': [25, 30, 35, 28],
'城市': ['北京', '上海', '广州', '深圳'],
'工资': [15000, 20000, 18000, 22000]
}
df = pd.DataFrame(data)
print(f"数据表:\n{df}")
# 数据查看
print(f"\n前3行数据:\n{df.head(3)}")
print(f"\n数据基本信息:\n{df.info()}")
print(f"\n数据统计描述:\n{df.describe()}")
# 数据选择
print(f"\n选择'姓名'列:\n{df['姓名']}")
print(f"\n选择前2行:\n{df.iloc[:2]}")
print(f"\n选择年龄大于28的行:\n{df[df['年龄'] > 28]}")
# 数据筛选和排序
high_salary = df[df['工资'] > 18000]
print(f"\n工资大于18000的员工:\n{high_salary}")
sorted_df = df.sort_values('工资', ascending=False)
print(f"\n按工资降序排列:\n{sorted_df}")
# 数据分组
city_stats = df.groupby('城市').agg({
'工资': ['mean', 'count'],
'年龄': 'mean'
})
print(f"\n按城市分组统计:\n{city_stats}")
# 数据处理
df['奖金'] = df['工资'] * 0.1 # 新增列
df['工资等级'] = pd.cut(df['工资'],
bins=[0, 18000, 20000, float('inf')],
labels=['普通', '良好', '优秀'])
print(f"\n处理后的数据:\n{df}")
# 数据清洗
# 创建含有缺失值的数据
df_missing = pd.DataFrame({
'A': [1, 2, np.nan, 4],
'B': [5, np.nan, np.nan, 8],
'C': ['a', 'b', 'c', 'd']
})
# 处理缺失值
df_filled = df_missing.fillna(0) # 填充为0
df_dropped = df_missing.dropna() # 删除缺失值行
print(f"\n填充缺失值:\n{df_filled}")
print(f"\n删除缺失值:\n{df_dropped}")
# 数据合并
df1 = pd.DataFrame({'key': ['A', 'B', 'C'], 'value1': [1, 2, 3]})
df2 = pd.DataFrame({'key': ['A', 'B', 'D'], 'value2': [4, 5, 6]})
merged_df = pd.merge(df1, df2, on='key', how='outer')
print(f"\n合并后的数据:\n{merged_df}")
# 数据聚合
data = pd.DataFrame({
'部门': ['技术', '技术', '市场', '市场', '财务'],
'员工': ['张三', '李四', '王五', '赵六', '钱七'],
'绩效': [85, 90, 88, 92, 87]
})
dept_stats = data.groupby('部门')['绩效'].agg(['mean', 'max', 'min'])
print(f"\n部门绩效统计:\n{dept_stats}")
# 科学计算示例 - 使用NumPy库
import numpy as np
# 创建数组
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([6, 7, 8, 9, 10])
print(f"数组1: {arr1}")
print(f"数组2: {arr2}")
# 数组运算
print(f"\n加法: {arr1 + arr2}")
print(f"减法: {arr1 - arr2}")
print(f"乘法: {arr1 * arr2}")
print(f"除法: {arr1 / arr2}")
# 数组操作
print(f"\n数组1的平方: {arr1 ** 2}")
print(f"数组1的平方根: {np.sqrt(arr1)}")
print(f"数组1的和: {np.sum(arr1)}")
print(f"数组1的平均值: {np.mean(arr1)}")
print(f"数组1的标准差: {np.std(arr1)}")
# 创建特殊数组
zeros_arr = np.zeros((3, 3)) # 全零数组
ones_arr = np.ones((2, 4)) # 全一数组
random_arr = np.random.random(5) # 随机数组
identity_mat = np.eye(3) # 单位矩阵
print(f"\n全零数组:\n{zeros_arr}")
print(f"全一数组:\n{ones_arr}")
print(f"随机数组: {random_arr}")
print(f"单位矩阵:\n{identity_mat}")
# 数组形状操作
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(f"\n二维数组:\n{arr_2d}")
print(f"数组形状: {arr_2d.shape}")
print(f"数组维度: {arr_2d.ndim}")
print(f"数组大小: {arr_2d.size}")
# 数组索引和切片
print(f"\n第一行: {arr_2d[0]}")
print(f"第一列: {arr_2d[:, 0]}")
print(f"子数组[0:2, 1:3]:\n{arr_2d[0:2, 1:3]}")
# 数组重塑
arr_flat = np.arange(12) # 创建0-11的数组
arr_reshaped = arr_flat.reshape(3, 4) # 重塑为3x4
print(f"\n重塑后的数组:\n{arr_reshaped}")
# 数组拼接
arr_a = np.array([[1, 2], [3, 4]])
arr_b = np.array([[5, 6], [7, 8]])
horizontal = np.hstack((arr_a, arr_b)) # 水平拼接
vertical = np.vstack((arr_a, arr_b)) # 垂直拼接
print(f"\n水平拼接:\n{horizontal}")
print(f"垂直拼接:\n{vertical}")
# 矩阵运算
matrix_a = np.array([[1, 2], [3, 4]])
matrix_b = np.array([[5, 6], [7, 8]])
matrix_mul = np.dot(matrix_a, matrix_b) # 矩阵乘法
print(f"\n矩阵乘法:\n{matrix_mul}")
# 求矩阵的行列式、逆矩阵
det = np.linalg.det(matrix_a)
print(f"行列式: {det}")
if det != 0:
inv_matrix = np.linalg.inv(matrix_a)
print(f"逆矩阵:\n{inv_matrix}")
# 线性代数求解
# 解方程组: 2x + y = 5, x + 3y = 10
A = np.array([[2, 1], [1, 3]])
b = np.array([5, 10])
solution = np.linalg.solve(A, b)
print(f"\n方程组解: x={solution[0]}, y={solution[1]}")
# 统计函数
data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(f"\n统计函数:")
print(f"平均值: {np.mean(data)}")
print(f"中位数: {np.median(data)}")
print(f"标准差: {np.std(data)}")
print(f"方差: {np.var(data)}")
print(f"最小值: {np.min(data)}")
print(f"最大值: {np.max(data)}")
print(f"百分位数(25%): {np.percentile(data, 25)}")
# 向量化操作
x = np.array([1, 2, 3, 4])
y = np.array([5, 6, 7, 8])
result = np.array([a + b if a > 2 else a * b for a, b in zip(x, y)])
print(f"\n条件运算结果: {result}")
# 广播机制
arr_small = np.array([[1, 2, 3]])
arr_large = np.array([[4], [5], [6]])
broadcasted = arr_small + arr_large
print(f"\n广播结果:\n{broadcasted}")
# 数据可视化示例 - 使用Matplotlib库
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# 设置中文字体支持
plt.rcParams['font.sans-serif'] = ['SimHei', 'Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
# 1. 基础折线图
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(10, 6))
plt.plot(x, y, label='sin(x)', color='blue', linewidth=2)
plt.plot(x, np.cos(x), label='cos(x)', color='red', linewidth=2)
plt.title('三角函数图像', fontsize=16)
plt.xlabel('x', fontsize=12)
plt.ylabel('y', fontsize=12)
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
# 2. 散点图
np.random.seed(42)
x = np.random.randn(100)
y = x + np.random.randn(100) * 0.5
plt.figure(figsize=(10, 6))
plt.scatter(x, y, alpha=0.6, c='blue')
plt.title('散点图示例', fontsize=16)
plt.xlabel('X轴')
plt.ylabel('Y轴')
plt.show()
# 3. 柱状图
categories = ['Python', 'Java', 'C++', 'JavaScript', 'Go']
values = [85, 75, 70, 80, 65]
plt.figure(figsize=(10, 6))
bars = plt.bar(categories, values, color='skyblue', edgecolor='black')
plt.title('编程语言流行度', fontsize=16)
plt.xlabel('编程语言')
plt.ylabel('流行度分数')
# 在柱子上添加数值标签
for bar in bars:
height = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2., height,
f'{height}', ha='center', va='bottom')
plt.show()
# 4. 饼图
labels = ['学习', '工作', '娱乐', '休息']
sizes = [30, 40, 15, 15]
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99']
explode = (0.1, 0, 0, 0) # 突出第一块
plt.figure(figsize=(8, 8))
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90)
plt.title('时间分配饼图', fontsize=16)
plt.axis('equal')
plt.show()
# 5. 直方图
data = np.random.normal(0, 1, 1000)
plt.figure(figsize=(10, 6))
plt.hist(data, bins=30, color='green', alpha=0.7, edgecolor='black')
plt.title('正态分布直方图', fontsize=16)
plt.xlabel('数值')
plt.ylabel('频数')
plt.grid(True, alpha=0.3)
plt.show()
# 6. 箱线图
data1 = np.random.normal(0, 1, 100)
data2 = np.random.normal(1, 1, 100)
data3 = np.random.normal(-1, 1, 100)
plt.figure(figsize=(10, 6))
plt.boxplot([data1, data2, data3], labels=['组A', '组B', '组C'])
plt.title('多组数据箱线图', fontsize=16)
plt.ylabel('数值')
plt.grid(True, alpha=0.3)
plt.show()
# 7. 热力图
data = np.random.randn(10, 10)
plt.figure(figsize=(10, 8))
plt.imshow(data, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.title('热力图示例', fontsize=16)
plt.show()
# 8. 子图
x = np.linspace(0, 2*np.pi, 100)
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
# 子图1: sin
axes[0, 0].plot(x, np.sin(x))
axes[0, 0].set_title('sin(x)')
# 子图2: cos
axes[0, 1].plot(x, np.cos(x))
axes[0, 1].set_title('cos(x)')
# 子图3: tan
axes[1, 0].plot(x, np.tan(x))
axes[1, 0].set_title('tan(x)')
axes[1, 0].set_ylim(-2, 2)
# 子图4: 散点
axes[1, 1].scatter(np.random.rand(50), np.random.rand(50))
axes[1, 1].set_title('散点图')
plt.tight_layout()
plt.show()
# 9. 使用Pandas绘图
df = pd.DataFrame({
'月份': ['1月', '2月', '3月', '4月', '5月', '6月'],
'销售额': [120, 150, 180, 200, 190, 220]
})
plt.figure(figsize=(10, 6))
plt.plot(df['月份'], df['销售额'], marker='o', linewidth=2)
plt.title('月度销售额趋势', fontsize=16)
plt.xlabel('月份')
plt.ylabel('销售额')
plt.grid(True, alpha=0.3)
plt.show()
# 10. 3D图形
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# 创建3D数据
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
surf = ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_title('3D表面图')
plt.colorbar(surf)
plt.show()
# 机器学习基础示例 - 使用Scikit-learn库
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import numpy as np
import pandas as pd
# 1. 线性回归示例
print("=" * 50)
print("线性回归示例")
print("=" * 50)
# 创建模拟数据
np.random.seed(42)
X = np.random.randn(100, 1) * 2
y = 3 * X + 2 + np.random.randn(100, 1) * 0.5 # y = 3x + 2 + noise
# 数据集划分
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# 数据标准化
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# 创建并训练模型
model = LinearRegression()
model.fit(X_train_scaled, y_train)
# 预测
y_pred = model.predict(X_test_scaled)
# 评估模型
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"模型系数: {model.coef_}")
print(f"截距: {model.intercept_}")
print(f"均方误差: {mse:.4f}")
print(f"R²得分: {r2:.4f}")
# 2. 多元线性回归
print("\n" + "=" * 50)
print("多元线性回归")
print("=" * 50)
# 创建多元数据
np.random.seed(42)
n_samples = 100
X_multi = np.random.randn(n_samples, 3) # 3个特征
true_coef = np.array([2, -1.5, 3])
y_multi = X_multi @ true_coef + 5 + np.random.randn(n_samples) * 0.5
# 划分数据集
X_train_multi, X_test_multi, y_train_multi, y_test_multi = train_test_split(
X_multi, y_multi, test_size=0.2, random_state=42
)
# 训练模型
model_multi = LinearRegression()
model_multi.fit(X_train_multi, y_train_multi)
# 预测和评估
y_pred_multi = model_multi.predict(X_test_multi)
r2_multi = r2_score(y_test_multi, y_pred_multi)
print(f"特征系数: {model_multi.coef_}")
print(f"截距: {model_multi.intercept_}")
print(f"R²得分: {r2_multi:.4f}")
# 3. 数据预处理流水线
print("\n" + "=" * 50)
print("数据预处理")
print("=" * 50)
from sklearn.preprocessing import MinMaxScaler, LabelEncoder
from sklearn.impute import SimpleImputer
# 创建含有缺失值的DataFrame
data = pd.DataFrame({
'年龄': [25, 30, np.nan, 35, 28],
'工资': [5000, 6000, 7000, np.nan, 8000],
'部门': ['技术', '市场', '技术', '财务', '市场']
})
print("原始数据:")
print(data)
# 处理缺失值
imputer = SimpleImputer(strategy='mean')
data[['年龄', '工资']] = imputer.fit_transform(data[['年龄', '工资']])
# 标签编码
label_encoder = LabelEncoder()
data['部门_编码'] = label_encoder.fit_transform(data['部门'])
# 数据标准化
minmax_scaler = MinMaxScaler()
data[['年龄_标准化', '工资_标准化']] = minmax_scaler.fit_transform(data[['年龄', '工资']])
print("\n预处理后的数据:")
print(data)
# 4. 特征工程
print("\n" + "=" * 50)
print("特征工程")
print("=" * 50)
# 创建示例数据
df = pd.DataFrame({
'日期': pd.date_range('2024-01-01', periods=10),
'数值': np.random.randn(10)
})
# 时间特征提取
df['年'] = df['日期'].dt.year
df['月'] = df['日期'].dt.month
df['日'] = df['日期'].dt.day
df['星期'] = df['日期'].dt.dayofweek
# 数值特征
df['数值_平方'] = df['数值'] ** 2
df['数值_对数'] = np.log1p(df['数值'].abs())
df['数值_滞后'] = df['数值'].shift(1)
df['数值_移动平均'] = df['数值'].rolling(window=3).mean()
print("特征工程后的数据:")
print(df)
# 5. 模型评估
print("\n" + "=" * 50)
print("模型评估指标")
print("=" * 50)
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import confusion_matrix, classification_report
# 创建分类示例数据
np.random.seed(42)
X_class = np.random.randn(100, 2)
y_class = (X_class[:, 0] + X_class[:, 1] > 0).astype(int)
X_train_class, X_test_class, y_train_class, y_test_class = train_test_split(
X_class, y_class, test_size=0.2, random_state=42
)
# 简单的分类器(基于阈值)
y_pred_class = (X_test_class[:, 0] + X_test_class[:, 1] > 0).astype(int)
# 计算评估指标
accuracy = accuracy_score(y_test_class, y_pred_class)
precision = precision_score(y_test_class, y_pred_class)
recall = recall_score(y_test_class, y_pred_class)
f1 = f1_score(y_test_class, y_pred_class)
print(f"准确率: {accuracy:.4f}")
print(f"精确率: {precision:.4f}")
print(f"召回率: {recall:.4f}")
print(f"F1分数: {f1:.4f}")
print("\n混淆矩阵:")
cm = confusion_matrix(y_test_class, y_pred_class)
print(cm)
print("\n分类报告:")
print(classification_report(y_test_class, y_pred_class))
# 6. 交叉验证
print("\n" + "=" * 50)
print("交叉验证")
print("=" * 50)
from sklearn.model_selection import cross_val_score
# 使用交叉验证评估模型
cv_scores = cross_val_score(model, X_train_scaled, y_train, cv=5)
print(f"交叉验证得分: {cv_scores}")
print(f"平均得分: {cv_scores.mean():.4f}")
print(f"标准差: {cv_scores.std():.4f}")
print("\n" + "=" * 50)
print("示例完成!")
print("=" * 50)