人工智能训练师三级(高级)· 40 道上机真题全面解析 · 150+ 核心语法点 · 考前必备辅导资料
从 CSV、Excel 等文件读取数据到 DataFrame
import pandas as pd data = pd.read_csv('file.csv') data = pd.read_excel('file.xlsx')
查看数据前几行、基本信息、统计描述
data.head() # 前 5 行 data.info() # 基本信息 data.describe() # 统计描述 data.shape # 行列数
使用条件表达式筛选数据
data[data['age'] > 18] data[(data['age'].between(18, 70))] data[data['income'] > data['income'].mean()]
groupby 进行分组,配合 agg、count、mean 等聚合函数
data.groupby('category')['value'].mean() data.groupby(['location', 'type']).agg({'value': ['mean', 'count']}) data.groupby('SensorType')['Value'].agg(['count', 'mean'])
使用 pivot_table 进行多维度分析
data.pivot_table(values='Value', index='Location', columns='SensorType', aggfunc='mean')
检测和处理缺失值
data.isnull().sum() # 统计缺失值 data.dropna() # 删除缺失值 data.fillna(value) # 填充缺失值 data['col'].fillna(method='ffill') # 前向填充 data['col'].fillna(method='bfill') # 后向填充
data.duplicated().sum() # 统计重复值 data.drop_duplicates() # 删除重复值
使用 IQR、条件过滤等方法处理异常值
# IQR 方法 Q1 = data.quantile(0.25) Q3 = data.quantile(0.75) IQR = Q3 - Q1 data_clean = data[~((data < (Q1 - 1.5 * IQR)) | (data > (Q3 + 1.5 * IQR))).any(axis=1)] # 条件过滤 data = data[(data['age'].between(18, 70)) & (data['value'] > 0)]
from sklearn.preprocessing import StandardScaler, MinMaxScaler # Z-score 标准化 scaler = StandardScaler() data[numerical_features] = scaler.fit_transform(data[numerical_features]) # Min-Max 归一化 scaler = MinMaxScaler() data[numerical_features] = scaler.fit_transform(data[numerical_features])
data['age'] = data['age'].astype(int) data['price'] = data['price'].astype(float)
将连续变量转换为分类变量
import numpy as np bins = [0, 18.5, 24, 28, np.inf] labels = ['偏瘦', '正常', '超重', '肥胖'] data['BMIRange'] = pd.cut(data['BMI'], bins, labels=labels, right=False) age_bins = [0, 26, 36, 46, 56, 66, np.inf] age_labels = ['≤25 岁', '26-35 岁', '36-45 岁', '46-55 岁', '56-65 岁', '>65 岁'] data['AgeRange'] = pd.cut(data['Age'], age_bins, labels=age_labels, right=False)
data['RiskLevel'] = np.where(data['days'] > 7, '高风险患者', '低风险患者') data['is_abnormal'] = np.where( ((data['SensorType'] == 'Temperature') & ((data['Value'] < -10) | (data['Value'] > 50))) | ((data['SensorType'] == 'Humidity') & ((data['Value'] < 0) | (data['Value'] > 100))), True, False )
data['col'].mean() # 平均值 data['col'].sum() # 求和 data['col'].count() # 计数 data['col'].min() # 最小值 data['col'].max() # 最大值 data['col'].std() # 标准差 data['col'].value_counts() # 频数统计
使用 pandas 处理日期时间数据
from datetime import datetime # 转换为日期时间类型 data['就诊日期'] = pd.to_datetime(data['就诊日期']) data['诊断日期'] = pd.to_datetime(data['诊断日期']) # 计算日期差值(天数) data['诊断延迟'] = (data['诊断日期'] - data['就诊日期']).dt.days data['病程'] = (datetime(2024, 9, 1) - data['诊断日期']).dt.days
# 修改列名 data.rename(columns={'old_name': 'new_name'}, inplace=True) # 去除列名空格 data.columns = data.columns.str.strip()
# 去除字符串前后空格 df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x) # 字符串分割提取 y = X.apply(lambda x: int(x.split(' ')[0]))
from sklearn.preprocessing import LabelEncoder # LabelEncoder 标签编码 label_encoder = LabelEncoder() data_cleaned['fitness_level'] = label_encoder.fit_transform(data_cleaned['fitness_level']) # get_dummies 独热编码 data_cleaned = pd.get_dummies(data_cleaned, drop_first=True) X = pd.get_dummies(X) # 将分类变量转为数值变量
# 使用众数填充 data_filled = data.apply(lambda x: x.fillna(x.mode()[0])) # 使用特定值填充 data['col'].fillna(value, inplace=True)
# 转换为数值类型,无法转换的变为 NaN df['horsepower'] = pd.to_numeric(df['horsepower'], errors='coerce') # 转换为整数类型 data_cleaned.loc[:, 'Your age'] = pd.to_numeric(data_cleaned['Your age'], errors='coerce') data_cleaned = data_cleaned.dropna(subset=['Your age']) data_cleaned.loc[:, 'Your age'] = data_cleaned['Your age'].astype(int)
# 删除指定列 data_cleaned = data.drop(columns=['序号', '所用时间']) X = data.drop(['SeriousDlqin2yrs', 'Unnamed: 0'], axis=1) cleaned_data = data.drop(columns=['is_abnormal'])
# 选择数值类型列 numeric_cols = data.select_dtypes(include=['float64', 'int64']).columns
# 获取行数 initial_rows = data.shape[0] deleted_rows = initial_rows - data.shape[0] # 统计行数 num_duplicates = duplicates.sum()
from sklearn.linear_model import LogisticRegression, LinearRegression from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier from sklearn.tree import DecisionTreeRegressor import xgboost as xgb from xgboost import XGBRegressor
# Logistic 回归 model = LogisticRegression(max_iter=1000) # 线性回归 pipeline = Pipeline([('scaler', StandardScaler()), ('linreg', LinearRegression())]) # 随机森林(100 棵树) rf_model = RandomForestRegressor(n_estimators=100, random_state=42) # XGBoost(100 棵树,学习率 0.05,最大深度 5) xgb_model = XGBRegressor(n_estimators=1000, learning_rate=0.05, max_depth=5, subsample=0.8, colsample_bytree=0.8, random_state=42) # 决策树回归 dt_model = DecisionTreeRegressor(random_state=42)
# 训练模型 model.fit(X_train, y_train) pipeline.fit(X_train, y_train) rf_model.fit(X_train, y_train) xgb_model.fit(X_train, y_train)
# 预测 y_pred = model.predict(X_test) y_pred = pipeline.predict(X_test) y_pred_rf = rf_model.predict(X_test) y_pred_xgb = xgb_model.predict(X_test)
import pickle import joblib # pickle 保存 with open('model.pkl', 'wb') as file: pickle.dump(model, file) # joblib 保存 joblib.dump(model, 'model.pkl')
from sklearn.metrics import classification_report, accuracy_score from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score # 分类指标 report = classification_report(y_test, y_pred, zero_division=1) accuracy = accuracy_score(y_test, y_pred) # 回归指标 mse = mean_squared_error(y_test, y_pred) # 均方误差 mae = mean_absolute_error(y_test, y_pred) # 平均绝对误差 r2 = r2_score(y_test, y_pred) # 决定系数 # 模型得分 train_score = model.score(X_train, y_train) test_score = model.score(X_test, y_test)
from imblearn.over_sampling import SMOTE # SMOTE 过采样 smote = SMOTE(random_state=42) X_resampled, y_resampled = smote.fit_resample(X_train, y_train)
from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LinearRegression # 创建包含标准化和回归的管道 pipeline = Pipeline([('scaler', StandardScaler()), ('linreg', LinearRegression())]) pipeline.fit(X_train, y_train)
# 定义自变量和因变量 X = data.drop(['SeriousDlqin2yrs', 'Unnamed: 0'], axis=1) y = data['SeriousDlqin2yrs'] # 删除目标列 X = data.drop(columns=[target]) y = data[target] # 选择特定列 X = data[['feature1', 'feature2', 'feature3']]
# 保存预测结果 pd.DataFrame(y_pred, columns=['预测结果']).to_csv('results.txt', index=False) # 保存对比结果 results = pd.DataFrame({'实际值': y_test, '预测值': y_pred}) results.to_csv('results.txt', index=False, sep='\t') # 保存报告到文件 with open('report.txt', 'w') as file: file.write(report) file.write(f'模型准确率:{accuracy:.2f}\n')
import matplotlib.pyplot as plt import matplotlib.font_manager as fm # 设置中文字体 font_path = 'C:/Windows/Fonts/simhei.ttf' my_font = fm.FontProperties(fname=font_path) # 绘制堆叠柱状图 treatment_outcome_distribution.plot(kind='bar', stacked=True) plt.title('标题', fontproperties=my_font) plt.xlabel('X 轴标签', fontproperties=my_font) plt.ylabel('Y 轴标签', fontproperties=my_font) plt.xticks(fontproperties=my_font) plt.yticks(fontproperties=my_font) plt.legend(prop=my_font) plt.show()
import matplotlib.pyplot as plt plt.scatter(data['age'], data['severity']) plt.title('年龄和疾病严重程度的关系', fontproperties=my_font) plt.xlabel('年龄', fontproperties=my_font) plt.ylabel('疾病严重程度', fontproperties=my_font) plt.show()
import matplotlib.pyplot as plt plt.figure(figsize=(10, 6)) plt.pie(exercise_frequency_counts, autopct='%1.1f%%', startangle=90, colors=plt.cm.Paired.colors) plt.title('Distribution of Exercise Frequency') plt.ylabel('') plt.show()
import matplotlib.pyplot as plt import seaborn as sns # 设置图像尺寸 plt.figure(figsize=(12, 8)) # 识别数值列 numeric_cols = data.select_dtypes(include=['float64', 'int64']).columns # 创建子图网格绘制箱线图 for i, col in enumerate(numeric_cols, 1): plt.subplot(3, 4, i) # 3行4列的子图 sns.boxplot(x=data[col]) plt.title(col) plt.tight_layout() # 自动调整子图间距 plt.show()
import matplotlib.pyplot as plt # 绘制单变量直方图 plt.figure(figsize=(10, 6)) plt.hist(data['age'], bins=20, edgecolor='black') plt.title('年龄分布') plt.xlabel('年龄') plt.ylabel('人数') plt.show() # 使用 Pandas 绘制直方图 data['age'].plot(kind='hist', bins=20, figsize=(10, 6)) plt.show()
import pandas as pd import matplotlib.pyplot as plt # 数据框直接绘图 df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # 折线图 df.plot(kind='line', figsize=(10, 6)) # 柱状图 df.plot(kind='bar', stacked=True) # 堆叠柱状图 treatment_outcome = data.groupby('疾病类型')['治疗结果'].value_counts().unstack() treatment_outcome.plot(kind='bar', stacked=True) # 饼图 exercise_counts.plot.pie(autopct='%1.1f%%', startangle=90) plt.show()
import matplotlib.pyplot as plt # 创建子图(方法1) fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12, 8)) axes[0, 0].plot(x, y1) # 左上角 axes[0, 1].scatter(x, y2) # 右上角 axes[1, 0].hist(data) # 左下角 axes[1, 1].pie(counts) # 右下角 # 创建子图(方法2) plt.subplot(2, 2, 1) # 2行2列,第1个子图 plt.plot(x, y) plt.subplot(2, 2, 2) # 2行2列,第2个子图 plt.scatter(x, y) plt.tight_layout() # 自动调整间距 plt.show()
import matplotlib.pyplot as plt import matplotlib.font_manager as fm # Windows 系统 font_path = 'C:/Windows/Fonts/simhei.ttf' my_font = fm.FontProperties(fname=font_path) # macOS 系统 font_path = '/System/Library/Fonts/PingFang.ttc' my_font = fm.FontProperties(fname=font_path) # Linux 系统 font_path = '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc' my_font = fm.FontProperties(fname=font_path) # 使用中文字体 plt.title('中文标题', fontproperties=my_font) plt.xlabel('中文X轴', fontproperties=my_font) plt.ylabel('中文Y轴', fontproperties=my_font) plt.xticks(fontproperties=my_font) plt.yticks(fontproperties=my_font) plt.legend(prop=my_font) plt.show()
# 分组并值计数 treatment_outcome_distribution = data.groupby('疾病类型')['治疗结果'].value_counts().unstack() # 分组求平均值 gender_stats = data.groupby('Gender')[['Speed', 'Distance', 'Time']].mean()
ONNX (Open Neural Network Exchange) 是开放的神经网络交换格式,允许在不同框架之间迁移模型
import onnxruntime as ort # 加载 ONNX 模型创建推理会话 ort_session = ort.InferenceSession('model.onnx') # 获取模型输入输出信息 input_name = ort_session.get_inputs()[0].name output_name = ort_session.get_outputs()[0].name # 获取输入形状 input_shape = ort_session.get_inputs()[0].shape
from PIL import Image import numpy as np # 加载图片并转换为 RGB image = Image.open('image.jpg').convert('RGB') # 加载为灰度图 image = Image.open('image.png').convert('L') # 调整大小(双线性插值) image = image.resize((256, 256), Image.BILINEAR) image = image.resize((28, 28), Image.ANTIALIAS) # MNIST 使用抗锯齿 # 中心裁剪 w, h = image.size left = (w - crop_size) / 2 top = (h - crop_size) / 2 image = image.crop((left, top, left + crop_size, top + crop_size)) # 转换为 numpy 数组 image_array = np.array(image, dtype=np.float32)
# 标准归一化(0-1) image = image_array / 255.0 # ImageNet 均值和标准差归一化 mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225] image = (image - mean) / std # 自定义均值归一化 image_mean = np.array([127, 127, 127]) image = (image - image_mean) / 128
# HWC 转 CHW(高度 - 宽度 - 通道 → 通道 - 高度 - 宽度) image = np.transpose(image, (2, 0, 1)) image = np.transpose(image, [2, 0, 1]) # 添加 batch 维度(NCHW 格式) image = image.reshape((1,) + image.shape) image = np.expand_dims(image, axis=0) # 添加 batch 维度 image = np.expand_dims(image, axis=1) # 添加 channel 维度 # 确保数据类型为 float32 image = image.astype(np.float32)
# 准备输入字典 ort_inputs = {ort_session.get_inputs()[0].name: image_array} # 执行推理 ort_outs = ort_session.run(None, ort_inputs) output = ort_session.run([output_name], {input_name: processed_image})[0]
import scipy.special # 应用 softmax 函数获取概率分布 probabilities = scipy.special.softmax(output, axis=-1) accuracy = scipy.special.softmax(output, axis=-1)
# 获取预测类别索引 predicted_class = np.argmax(ort_outs[0]) predicted_idx = np.argmax(accuracy) # 获取 Top-5 索引(降序排列) top5_idx = np.argsort(probabilities)[-5:][::-1] top5_prob = probabilities[top5_idx] # 获取概率百分比 prob_percentage = accuracy[predicted_idx] * 100
# 从文件加载标签 with open('labels.txt') as f: labels = [line.strip() for line in f.readlines()] # 情感标签映射表 emotion_table = { 'neutral': 0, 'happy': 1, 'sad': 2, 'angry': 3, 'fearful': 4, 'disgusted': 5, 'surprised': 6 } # 获取预测标签 predicted_label = labels[predicted_idx] predicted_emotion = emotion_table[predicted_label]
import cv2 # 读取图片(BGR 格式) orig_image = cv2.imread(img_path) # BGR 转 RGB image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB) # 调整大小 image = cv2.resize(image, (320, 240)) # 绘制矩形框 cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4) # 保存结果图片 cv2.imwrite(os.path.join(result_path, file_path), orig_image)
def predict(width, height, confidences, boxes, prob_threshold, iou_threshold=0.3, top_k=-1): boxes = boxes[0] confidences = confidences[0] picked_box_probs = [] picked_labels = [] for class_index in range(1, confidences.shape[1]): probs = confidences[:, class_index] mask = probs > prob_threshold probs = probs[mask] if probs.shape[0] == 0: continue subset_boxes = boxes[mask, :] box_probs = np.concatenate([subset_boxes, probs.reshape(-1, 1)], axis=1) box_probs = box_utils.hard_nms(box_probs, iou_threshold=iou_threshold, top_k=top_k) picked_box_probs.append(box_probs) picked_labels.extend([class_index] * box_probs.shape[0]) if not picked_box_probs: return np.array([]), np.array([]), np.array([]) picked_box_probs = np.concatenate(picked_box_probs) # 坐标缩放回原图尺寸 picked_box_probs[:, 0] *= width picked_box_probs[:, 1] *= height picked_box_probs[:, 2] *= width picked_box_probs[:, 3] *= height return picked_box_probs[:, :4].astype(np.int32), np.array(picked_labels), picked_box_probs[:, 4]
import os # 检查目录是否存在 if not os.path.exists(result_path): os.makedirs(result_path) # 获取目录下所有文件 listdir = os.listdir(path) # 拼接路径 img_path = os.path.join(path, file_path) # 遍历目录文件 for file_path in listdir: img_path = os.path.join(path, file_path)
import time # 记录开始时间 time_time = time.time() # 执行推理 output = session.run([output_name], {input_name: processed_image}) # 计算耗时 print("cost time:{}".format(time.time() - time_time))
# 形状检查 assert img_data.shape == input_shape, \ f"Expected shape {input_shape}, but got {img_data.shape}"
def preprocess_image(image, resize_size=256, crop_size=224, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]): """图像预处理函数""" image = image.resize((resize_size, resize_size), Image.BILINEAR) w, h = image.size left = (w - crop_size) / 2 top = (h - crop_size) / 2 image = image.crop((left, top, left + crop_size, top + crop_size)) image = np.array(image).astype(np.float32) image = image / 255.0 image = (image - mean) / std image = np.transpose(image, (2, 0, 1)) image = image.reshape((1,) + image.shape) return image def preprocess(image_path): input_shape = (1, 1, 64, 64) img = Image.open(image_path).convert('L') img = img.resize((64, 64), Image.ANTIALIAS) img_data = np.array(img, dtype=np.float32) img_data = np.expand_dims(img_data, axis=0) img_data = np.expand_dims(img_data, axis=1) assert img_data.shape == input_shape return img_data
| 算法 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| 线性回归 | 预测连续值 | 简单、易解释 | 假设线性关系 |
| 逻辑回归 | 二分类问题 | 输出概率、易解释 | 特征需独立 |
| 决策树 | 分类/回归 | 直观、处理非线性 | 易过拟合 |
| 随机森林 | 分类/回归 | 鲁棒性强、效果好 | 训练慢、难解释 |
| XGBoost | 分类/回归 | 精度高、速度快 | 参数多、易过拟合 |
| 章节 | 题号范围 | 题型 | 题量 | 主要考点 | 难度等级 | 分值占比 |
|---|---|---|---|---|---|---|
| 第一章 | 1.1.1 - 1.1.5 | 数据处理编程题 | 5 题 | Pandas 数据读取、清洗、分组聚合、数据透视、分箱、异常值处理 | 中等 | 12.5% |
| 第一章 | 1.2.1 - 1.2.5 | 简答题 | 5 题 | 数据处理流程设计、业务理解 | 中等 | 12.5% |
| 第二章 | 2.1.1 - 2.1.5 | 机器学习编程题 | 5 题 | 数据预处理、特征工程、数据集划分、可视化 | 中等 | 12.5% |
| 第二章 | 2.2.1 - 2.2.5 | 机器学习编程题 | 5 题 | 模型训练(Logistic、线性回归、随机森林、XGBoost、决策树)、模型评估、数据不平衡处理 | 中等 | 12.5% |
| 第三章 | 3.1.1 - 3.1.5 | 简答题 | 5 题 | AI 应用场景设计、系统架构 | 中等 | 12.5% |
| 第三章 | 3.2.1 - 3.2.5 | 深度学习编程题 | 5 题 | ONNX 模型加载、图像预处理、模型推理、目标检测 | 困难 | 12.5% |
| 第四章 | 4.1.1 - 4.1.5 | 简答题 | 5 题 | 综合应用题、方案设计 | 困难 | 12.5% |
| 第四章 | 4.2.1 - 4.2.5 | 简答题 | 5 题 | 综合应用题、方案设计 | 困难 | 12.5% |
pd.read_csv()head()、info()、describe()isnull().sum()、dropna()、fillna()duplicated().sum()、drop_duplicates()astype()、to_numeric()get_dummies()、LabelEncoderStandardScaler、MinMaxScalerto_csv(index=False)from sklearn.xxx import XXXX = data.drop()、y = data[]train_test_split(X, y, test_size=0.2, random_state=42)model = XXX(n_estimators=100, random_state=42)model.fit(X_train, y_train)y_pred = model.predict(X_test)accuracy_score、classification_report、mean_squared_error、r2_scorepickle.dump() 或 joblib.dump()ort.InferenceSession('model.onnx')get_inputs()[0].name、get_outputs()[0].nameImage.open().convert('RGB')session.run([output_name], {input_name: image})scipy.special.softmax(output, axis=-1)np.argmax() 或 np.argsort()[-5:][::-1]labels[predicted_idx]index=Falseastype(int/float)transpose((2,0,1))reshape((1,)+shape) 或 expand_dims(axis=0)astype(np.float32)# ===== 1. Pandas 数据处理模板 ===== import pandas as pd import numpy as np # 读取数据 data = pd.read_csv('data.csv') # 数据探索 print(data.head()) print(data.info()) print(data.describe()) # 缺失值处理 print(data.isnull().sum()) data = data.dropna() # 或 data['col'].fillna(method='ffill', inplace=True) # 重复值处理 print(data.duplicated().sum()) data = data.drop_duplicates() # 异常值处理(IQR 方法) Q1 = data.quantile(0.25) Q3 = data.quantile(0.75) IQR = Q3 - Q1 data = data[~((data < (Q1 - 1.5 * IQR)) | (data > (Q3 + 1.5 * IQR))).any(axis=1)] # 数据分箱 bins = [0, 18, 35, 60, np.inf] labels = ['青年', '中年', '中老年', '老年'] data['age_group'] = pd.cut(data['age'], bins, labels=labels, right=False) # 分组统计 result = data.groupby('category')['value'].agg(['count', 'mean', 'sum']) # 保存结果 data.to_csv('output.csv', index=False) # ===== 2. 机器学习建模模板 ===== from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, classification_report import pickle # 定义特征和目标 X = data.drop(['target'], axis=1) y = data['target'] # 数据集划分 X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) # 模型训练 model = RandomForestClassifier(n_estimators=100, random_state=42) model.fit(X_train, y_train) # 模型预测 y_pred = model.predict(X_test) # 模型评估 accuracy = accuracy_score(y_test, y_pred) print(f"Accuracy: {accuracy:.2f}") print(classification_report(y_test, y_pred)) # 模型保存 with open('model.pkl', 'wb') as f: pickle.dump(model, f) # ===== 3. ONNX 推理模板 ===== import onnxruntime as ort from PIL import Image import numpy as np import scipy.special # 加载模型 session = ort.InferenceSession('model.onnx') input_name = session.get_inputs()[0].name output_name = session.get_outputs()[0].name # 加载并预处理图像 image = Image.open('test.jpg').convert('RGB') image = image.resize((256, 256), Image.BILINEAR) image = np.array(image, dtype=np.float32) / 255.0 image = np.transpose(image, (2, 0, 1)) image = image.reshape((1,) + image.shape) # 模型推理 output = session.run([output_name], {input_name: image})[0] # 获取预测结果 probabilities = scipy.special.softmax(output, axis=-1) predicted_idx = np.argmax(probabilities) predicted_label = labels[predicted_idx] print(f"Predicted: {predicted_label}")
熟练掌握以上 150+ 语法点,上机考试无忧!