完整代码(Google Colab / Jupyter Notebook 均可)python numpy np matplotlibpyplot plt sklearnsvm SVC sklearnmetrics accuracy_score confusion_matrix ConfusionMatrixDisplay sklearndatasets fetch_openml skimagefeature hog skimagetransform resize tqdm tqdm X y fetch_openml version return_X_y as_frame parserX Xastypenpfloat32 y yastypenpint32X_train y_train X yX_test y_test X y images pixels_per_cell 输入: images shape (N, 784) 展平的图像 输出: HOG特征向量 (N, 特征维度) """ features img tqdmimages desc img_2d imgreshape hog_feat hog img_2d pixels_per_cellpixels_per_cell cells_per_block visualize feature_vector featuresappendhog_feat nparrayfeaturesX_train_hog extract_hog_featuresX_trainX_test_hog extract_hog_featuresX_testclf SVCkernel C random_stateclffitX_train_hog y_traintrain_pred clfpredictX_train_hogtrain_acc accuracy_scorey_train train_predtest_pred clfpredictX_test_hogtest_acc accuracy_scorey_test test_prederror_indices npwheretest_pred y_testnum_show error_indicesselected nprandomchoiceerror_indices num_show replacefig axes pltsubplots figsize i idx selected row col i i img X_testidxreshape axesrow colimshowimg cmap axesrow colset_title axesrow colaxispltsuptitle fontsizeplttight_layoutpltshowcm confusion_matrixy_test test_preddisp ConfusionMatrixDisplayconfusion_matrixcmdispplotcmapplttitlepltshow joblib joblibdumpclf 运行后你会看到什么(预期输出示例)text 训练集大小: 10000, 测试集大小: 2000 提取HOG特征: 100%|██████████| 10000/10000 [00:12<00:00, 789.23it/s] 提取HOG特征: 100%|██████████| 2000/2000 [00:02<00:00, 801.45it/s] HOG特征维度: 324 训练集准确率: 0.8921 测试集准确率: 0.6734 准确率大约在 67% 左右——这就是不用深度学习的天花板。 这段代码让你学到什么(比准确率重要)
动手实验(必做,5分钟)运行完上面代码后,修改下面任意一个参数,观察准确率变化:
下一步预告(第二圈要做什么)现在你用SVM只能达到67%,而且对旋转、倾斜、手写风格变化极其敏感。第二圈我们会用PyTorch搭一个简单的CNN,不做任何特征工程,直接喂像素进去,准确率直接跳到95%+。 今晚的任务:把上面代码跑通,截一张“混淆矩阵”的图保存下来。等第二圈CNN跑完后,把两张混淆矩阵放在一起对比——那会是你对“深度学习为什么有效”最震撼的一次体验。 完整代码(Google Colab / Jupyter Notebook 均可)python numpy np matplotlibpyplot plt sklearnsvm SVC sklearnmetrics accuracy_score confusion_matrix ConfusionMatrixDisplay sklearndatasets fetch_openml skimagefeature hog skimagetransform resize tqdm tqdm X y fetch_openml version return_X_y as_frame parserX Xastypenpfloat32 y yastypenpint32X_train y_train X yX_test y_test X y images pixels_per_cell 输入: images shape (N, 784) 展平的图像 输出: HOG特征向量 (N, 特征维度) """ features img tqdmimages desc img_2d imgreshape hog_feat hog img_2d pixels_per_cellpixels_per_cell cells_per_block visualize feature_vector featuresappendhog_feat nparrayfeaturesX_train_hog extract_hog_featuresX_trainX_test_hog extract_hog_featuresX_testclf SVCkernel C random_stateclffitX_train_hog y_traintrain_pred clfpredictX_train_hogtrain_acc accuracy_scorey_train train_predtest_pred clfpredictX_test_hogtest_acc accuracy_scorey_test test_prederror_indices npwheretest_pred y_testnum_show error_indicesselected nprandomchoiceerror_indices num_show replacefig axes pltsubplots figsize i idx selected row col i i img X_testidxreshape axesrow colimshowimg cmap axesrow colset_title axesrow colaxispltsuptitle fontsizeplttight_layoutpltshowcm confusion_matrixy_test test_preddisp ConfusionMatrixDisplayconfusion_matrixcmdispplotcmapplttitlepltshow joblib joblibdumpclf 运行后你会看到什么(预期输出示例)text 训练集大小: 10000, 测试集大小: 2000 提取HOG特征: 100%|██████████| 10000/10000 [00:12<00:00, 789.23it/s] 提取HOG特征: 100%|██████████| 2000/2000 [00:02<00:00, 801.45it/s] HOG特征维度: 324 训练集准确率: 0.8921 测试集准确率: 0.6734 准确率大约在 67% 左右——这就是不用深度学习的天花板。 这段代码让你学到什么(比准确率重要)
动手实验(必做,5分钟)运行完上面代码后,修改下面任意一个参数,观察准确率变化:
下一步预告(第二圈要做什么)现在你用SVM只能达到67%,而且对旋转、倾斜、手写风格变化极其敏感。第二圈我们会用PyTorch搭一个简单的CNN,不做任何特征工程,直接喂像素进去,准确率直接跳到95%+。 今晚的任务:把上面代码跑通,截一张“混淆矩阵”的图保存下来。等第二圈CNN跑完后,把两张混淆矩阵放在一起对比——那会是你对“深度学习为什么有效”最震撼的一次体验。 |
Powered by Discuz! X3.5
© 2001-2026 Discuz! Team.