Python AI项目实战全教程:图片分类+文本情感分析(附完整代码)

你在学习Python AI开发时,最核心的需求是把理论落地到实际项目中——图片分类和文本情感分析是AI领域最基础也最实用的两个方向,覆盖计算机视觉、自然语言处理两大核心场景。本文从环境搭建到代码实现,全程手把手带你完成两个实战项目,所有代码可直接运行,零基础也能快速上手。

一、前置准备:AI开发环境搭建

1.1 核心库安装

Python AI项目依赖三大核心库:TensorFlow/PyTorch(深度学习框架)、Pandas/Numpy(数据处理)、Matplotlib(可视化),优先用pip安装(建议用虚拟环境避免版本冲突):

# 基础数据处理库
pip install numpy pandas matplotlib
# 深度学习框架(二选一,TensorFlow更适合新手)
pip install tensorflow==2.15.0  # 稳定版
# 或PyTorch(需适配CUDA,无GPU则装CPU版)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
# 文本处理专用库
pip install nltk scikit-learn
# 图片处理库
pip install pillow

1.2 验证环境

安装完成后,在Python终端验证:

import tensorflow as tf
import torch
import numpy as np
print("TensorFlow版本:", tf.__version__)
print("PyTorch版本:", torch.__version__)
print("Numpy版本:", np.__version__)
# 输出对应版本号即环境正常

二、实战项目1:图片分类(基于TensorFlow/Keras)

2.1 项目背景

图片分类是计算机视觉入门核心,本文以经典的MNIST手写数字分类为例(数据集包含0-9手写数字图片,共7万张),用CNN(卷积神经网络)实现98%以上的分类准确率。

2.2 完整代码实现

# 步骤1:导入库
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt

# 步骤2:加载并预处理MNIST数据集
# 加载数据集(自动下载,首次运行需等待)
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
# 归一化:将像素值从0-255缩放到0-1(提升模型收敛速度)
train_images = train_images.reshape((60000, 28, 28, 1)) / 255.0
test_images = test_images.reshape((10000, 28, 28, 1)) / 255.0

# 步骤3:构建CNN模型
model = models.Sequential([
    # 卷积层1:提取基础特征(32个3×3卷积核)
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D((2, 2)),  # 池化层:降维,保留关键特征
    # 卷积层2:提取更复杂特征
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    # 全连接层:分类决策
    layers.Flatten(),  # 展平为一维数组
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')  # 10分类,输出概率
])

# 步骤4:编译模型
model.compile(optimizer='adam',  # 优化器
              loss='sparse_categorical_crossentropy',  # 损失函数(适用于整数标签)
              metrics=['accuracy'])  # 评估指标:准确率

# 步骤5:训练模型
history = model.fit(train_images, train_labels, 
                    epochs=5,  # 训练轮数
                    batch_size=64,  # 批次大小
                    validation_split=0.1)  # 用10%训练数据做验证

# 步骤6:模型评估
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f"测试集准确率:{test_acc:.4f}")  # 正常可达98%以上

# 步骤7:可视化训练过程
plt.plot(history.history['accuracy'], label='训练准确率')
plt.plot(history.history['val_accuracy'], label='验证准确率')
plt.xlabel('训练轮数')
plt.ylabel('准确率')
plt.legend()
plt.show()

# 步骤8:单张图片预测示例
import random
# 随机选一张测试图片
idx = random.randint(0, len(test_images)-1)
test_img = test_images[idx:idx+1]
# 预测
pred = model.predict(test_img)
pred_label = tf.argmax(pred, axis=1).numpy()[0]
true_label = test_labels[idx]
print(f"预测标签:{pred_label},真实标签:{true_label}")

# 显示图片
plt.imshow(test_img.reshape(28,28), cmap='gray')
plt.title(f"Pred: {pred_label}, True: {true_label}")
plt.axis('off')
plt.show()

2.3 关键知识点解析

  1. 数据集预处理reshape为4维张量(样本数×高度×宽度×通道数),归一化是因为神经网络对0-1区间的数值更敏感;
  2. CNN结构:卷积层(Conv2D)负责提取图片边缘、纹理等特征,池化层(MaxPooling2D)降低计算量,全连接层(Dense)负责分类;
  3. 模型编译adam优化器是主流选择,softmax激活函数将输出转为0-1的概率值,对应10个数字的分类概率。

2.4 扩展:自定义图片分类

若想分类自己的图片(如猫狗、水果),只需替换数据集:

  1. 将图片按类别放在不同文件夹(如train/cattrain/dog);
  2. tf.keras.utils.image_dataset_from_directory加载数据;
  3. 调整输入形状(如(128,128,3)对应彩色图片)和输出类别数。

三、实战项目2:文本情感分析(基于NLTK+Scikit-learn)

3.1 项目背景

文本情感分析是NLP入门核心,本文以IMDB电影评论情感分类为例(正面/负面评论),用朴素贝叶斯模型实现85%以上的分类准确率。

3.2 完整代码实现

# 步骤1:导入库
import nltk
import pandas as pd
import numpy as np
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report

# 步骤2:下载NLTK所需资源(首次运行)
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')

# 步骤3:加载数据集(IMDB数据集可从Keras获取)
from tensorflow.keras.datasets import imdb
# 加载数据(保留前10000个高频词)
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000)

# 步骤4:将数字编码转回文本(IMDB数据默认是数字编码)
# 获取词库映射
word_index = imdb.get_word_index()
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
# 解码函数
def decode_review(text):
    return ' '.join([reverse_word_index.get(i - 3, '?') for i in text])  # 3是特殊符号占位

# 解码训练/测试数据
train_texts = [decode_review(x) for x in x_train]
test_texts = [decode_review(x) for x in x_test]

# 步骤5:文本预处理函数
def preprocess_text(text):
    # 1. 分词
    tokens = word_tokenize(text.lower())  # 转小写+分词
    # 2. 去除停用词(如the、a、is等无意义词)
    stop_words = set(stopwords.words('english'))
    tokens = [t for t in tokens if t not in stop_words and t.isalpha()]  # 只保留字母
    # 3. 词形还原(如running→run)
    lemmatizer = WordNetLemmatizer()
    tokens = [lemmatizer.lemmatize(t) for t in tokens]
    # 拼接为字符串
    return ' '.join(tokens)

# 批量预处理
print("开始文本预处理...")
train_texts_clean = [preprocess_text(text) for text in train_texts[:10000]]  # 取1万条加快速度
test_texts_clean = [preprocess_text(text) for text in test_texts[:2000]]
print("预处理完成!")

# 步骤6:特征提取(TF-IDF)
tfidf = TfidfVectorizer(max_features=5000)  # 保留5000个高频词
X_train = tfidf.fit_transform(train_texts_clean).toarray()
X_test = tfidf.transform(test_texts_clean).toarray()
y_train_small = y_train[:10000]
y_test_small = y_test[:2000]

# 步骤7:训练朴素贝叶斯模型
model = MultinomialNB()
model.fit(X_train, y_train_small)

# 步骤8:模型评估
y_pred = model.predict(X_test)
acc = accuracy_score(y_test_small, y_pred)
print(f"测试集准确率:{acc:.4f}")
# 详细分类报告
print(classification_report(y_test_small, y_pred, target_names=['负面评论', '正面评论']))

# 步骤9:自定义文本预测
def predict_sentiment(text):
    # 预处理
    text_clean = preprocess_text(text)
    # 特征提取
    text_tfidf = tfidf.transform([text_clean]).toarray()
    # 预测
    pred = model.predict(text_tfidf)[0]
    return '正面评论' if pred == 1 else '负面评论'

# 测试示例
test_text1 = "This movie is amazing! The plot is great and acting is perfect."
test_text2 = "Worst movie ever, I wasted 2 hours of my life."
print(f"评论1情感:{predict_sentiment(test_text1)}")
print(f"评论2情感:{predict_sentiment(test_text2)}")

3.3 关键知识点解析

  1. 文本预处理
    • 分词(word_tokenize):将句子拆分为单个单词;
    • 停用词去除:去掉无情感意义的词汇,减少噪声;
    • 词形还原:将词汇还原为原型,统一特征(如ran→run);
  2. TF-IDF特征:将文本转为数值向量,TF表示词频,IDF表示词的重要性,越重要的词权重越高;
  3. 朴素贝叶斯模型:适合文本分类的轻量级模型,计算速度快,对小数据集效果好。

3.4 扩展:中文情感分析

若想处理中文文本,只需替换预处理步骤:

  1. jieba库分词(pip install jieba);
  2. 加载中文停用词表(可从网上下载);
  3. 替换数据集为中文评论(如淘宝评论、微博评论)。

四、常见问题与优化技巧

4.1 模型准确率低?

  • 图片分类:增加训练轮数、调整网络结构(如增加卷积层)、数据增强(tf.keras.layers.RandomFlip等);
  • 文本分析:增加训练数据、调整TF-IDF参数、改用更复杂的模型(如LSTM、BERT)。

4.2 运行速度慢?

  • 图片分类:使用GPU加速(安装CUDA版TensorFlow/PyTorch)、减小批次大小;
  • 文本分析:减少处理的文本数量、降低TF-IDF的max_features值。

4.3 数据集获取?

  • 公开数据集:MNIST、IMDB、CIFAR-10(图片)、豆瓣评论(中文文本);
  • 自定义数据集:用爬虫爬取(如BeautifulSoup),或从Kaggle、天池等平台下载。
posted @ 2026-01-07 22:05  python农工  阅读(32)  评论(0)    收藏  举报