本文基于加州房价数据集,演示如何使用Python从零构建并训练AI模型。内容涵盖数据预处理、线性回归、多层神经网络、模型优化以及Flask API部署,所有代码均可直接运行。
一、环境准备与数据加载
首先安装必需库:- pip install numpy pandas matplotlib seaborn scikit-learn tensorflow
复制代码 加载sklearn内置的加州房价数据集:- from sklearn.datasets import fetch_california_housing
- import pandas as pd
- housing = fetch_california_housing()
- data = pd.DataFrame(housing.data, columns=housing.feature_names)
- data['PRICE'] = housing.target
- print(data.head())
复制代码 数据标准化:使用StandardScaler将特征减去均值除以标准差,使模型收敛更快:- from sklearn.preprocessing import StandardScaler
- scaler = StandardScaler()
- features = data.drop('PRICE', axis=1)
- target = data['PRICE']
- features_scaled = scaler.fit_transform(features)
复制代码
二、训练线性回归模型
划分训练集和测试集(80/20):- from sklearn.model_selection import train_test_split
- X_train, X_test, y_train, y_test = train_test_split(
- features_scaled, target, test_size=0.2, random_state=42)
复制代码 训练并评估:- from sklearn.linear_model import LinearRegression
- from sklearn.metrics import mean_squared_error, r2_score
- model = LinearRegression()
- model.fit(X_train, y_train)
- y_pred = model.predict(X_test)
- mse = mean_squared_error(y_test, y_pred)
- r2 = r2_score(y_test, y_pred)
- print(f'MSE: {mse}, R²: {r2}')
复制代码 MSE越小越好,R²越接近1说明模型拟合度越高。该线性回归模型作为基准。
三、构建深度学习神经网络
使用TensorFlow搭建全连接神经网络:- import tensorflow as tf
- from tensorflow.keras.models import Sequential
- from tensorflow.keras.layers import Dense, Input
- nn_model = Sequential([
- Input(shape=(X_train.shape[1],)),
- Dense(64, activation='relu'),
- Dense(32, activation='relu'),
- Dense(1)
- ])
- nn_model.compile(optimizer='adam', loss='mse', metrics=['mae'])
- nn_model.summary()
复制代码 训练100轮,批大小32,20%验证集:- history = nn_model.fit(
- X_train, y_train,
- epochs=100,
- batch_size=32,
- validation_split=0.2,
- verbose=1)
复制代码 评估测试集:- test_loss, test_mae = nn_model.evaluate(X_test, y_test)
- print(f'Test MSE: {test_loss}, Test MAE: {test_mae}')
复制代码
四、模型优化策略
1. 防止过拟合——添加Dropout层:- from tensorflow.keras.layers import Dropout
- nn_model = Sequential([
- Input(shape=(X_train.shape[1],)),
- Dense(64, activation='relu'),
- Dropout(0.5),
- Dense(32, activation='relu'),
- Dense(1)
- ])
- nn_model.compile(optimizer='adam', loss='mse', metrics=['mae'])
复制代码 2. 早停法——在验证损失不再下降时停止训练:- from tensorflow.keras.callbacks import EarlyStopping
- early_stop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
- history = nn_model.fit(
- X_train, y_train,
- epochs=200,
- batch_size=64,
- validation_split=0.2,
- callbacks=[early_stop])
复制代码 3. 调整学习率:- from tensorflow.keras.optimizers import Adam
- nn_model.compile(optimizer=Adam(learning_rate=0.0001), loss='mse', metrics=['mae'])
复制代码
五、模型保存与Flask部署
保存模型为文件夹:- nn_model.save('my_ai_model')
复制代码 创建Flask API服务(app.py):- from flask import Flask, request, jsonify
- import numpy as np
- from tensorflow.keras.models import load_model
- model = load_model('my_ai_model')
- app = Flask(__name__)
- @app.route('/predict', methods=['POST'])
- def predict():
- data = request.json
- features = np.array(data['features']).reshape(1, -1)
- prediction = model.predict(features)
- return jsonify({'prediction': float(prediction[0][0])})
- if __name__ == '__main__':
- app.run(debug=True)
复制代码 启动后使用curl测试:- curl -X POST -H "Content-Type: application/json" -d '{"features":[0.1,-0.2,0.5,0.3,0.7,-1.1,0.4,0.9]}' http://127.0.0.1:5000/predict
复制代码 返回JSON格式预测值。
通过上述步骤,你已掌握从数据准备、模型训练、优化到部署的完整AI开发流程。实际项目中可根据需求调整网络深度、正则化强度或使用GPU加速(检查GPU可用性:tf.config.list_physical_devices('GPU'))。 |