首页 分享 2025年数维杯C题完整求解思路讲解+代码分享

2025年数维杯C题完整求解思路讲解+代码分享

来源:花匠小妙招 时间:2025-11-06 14:34

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.dates as mdates

from datetime import datetime, timedelta

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score

from sklearn.model_selection import train_test_split

import matplotlib

# Set font properties to avoid issues with Chinese characters

matplotlib.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans', 'Arial Unicode MS']

matplotlib.rcParams['axes.unicode_minus'] = False

#1. Read data

data = pd.read_excel('西安.xlsx')

# 2. Process time column, convert to datetime format

data['time'] = pd.to_datetime(data['time'], format='%d.%m.%Y %H:%M')

# 3. Extract features and labels

X = data[['T', 'P0', 'U', 'Ff']].values  # Features: temperature T, pressure P0, relative humidity U, wind speed Ff

y = data['WW'].values  # Labels: WW (1 for rain, 0 for no rain)

# 4. Split data into training and testing sets (80% training, 20% testing)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 5. Train Random Forest model

model = RandomForestClassifier(n_estimators=100, random_state=42)

model.fit(X_train, y_train)

# 6. Make predictions

y_pred = model.predict(X_test)

# 7. Evaluate model performance

conf_matrix = confusion_matrix(y_test, y_pred)

print('Confusion Matrix:')

print(conf_matrix)

# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print(f'Accuracy: {accuracy:.4f}')

# Calculate precision, recall and F1 score

precision = precision_score(y_test, y_pred, zero_division=0)

recall = recall_score(y_test, y_pred, zero_division=0)

f1 = f1_score(y_test, y_pred, zero_division=0)

print(f'Precision: {precision:.4f}')

print(f'Recall: {recall:.4f}')

print(f'F1 Score: {f1:.4f}')

# Simulate 2026 Qingming Festival meteorological data based on 2018-2025 data

# Get statistics from training data

X_all = data[['T', 'P0', 'U', 'Ff']].values

mean_T = np.mean(X_all[:, 0])  # Mean of temperature T

std_T = np.std(X_all[:, 0])    # Standard deviation of temperature T

mean_P0 = np.mean(X_all[:, 1]) # Mean of pressure P0

std_P0 = np.std(X_all[:, 1])   # Standard deviation of pressure P0

mean_U = np.mean(X_all[:, 2])  # Mean of humidity U

std_U = np.std(X_all[:, 2])    # Standard deviation of humidity U

mean_Ff = np.mean(X_all[:, 3]) # Mean of wind speed Ff

std_Ff = np.std(X_all[:, 3])   # Standard deviation of wind speed Ff

# Generate simulated data for 24 hours of Qingming Festival 2026

n_hours = 24  # 24 hours

np.random.seed(42)  # For reproducibility

# Simulate data using normal distribution

sim_T = mean_T + std_T * np.random.randn(n_hours)   # Simulated temperature T

sim_P0 = mean_P0 + std_P0 * np.random.randn(n_hours) # Simulated pressure P0

sim_U = mean_U + std_U * np.random.randn(n_hours)   # Simulated humidity U

sim_Ff = mean_Ff + std_Ff * np.random.randn(n_hours) # Simulated wind speed Ff

# Combine simulated meteorological data

X_2026_sim = np.column_stack((sim_T, sim_P0, sim_U, sim_Ff))

# Use the trained model to predict rainfall for 2026 simulated data

y_pred_2026 = model.predict(X_2026_sim)

# Generate time series for Qingming Festival 2026 (April 4, 2026, 24 hours)

start_time = datetime(2026, 4, 4)

time_2026 = [start_time + timedelta(hours=i) for i in range(24)]

# Visualize the rainfall prediction for 2026

plt.figure(figsize=(12, 6))

plt.plot(time_2026, y_pred_2026, '-o', linewidth=2, markersize=6)

plt.title('Rainfall Prediction for Qingming Festival 2026', fontsize=14)

plt.xlabel('Time (hours)', fontsize=12)

plt.ylabel('Rainfall Status (1: Rain, 0: No Rain)', fontsize=12)

plt.grid(True)

# Format the x-axis to show hours

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))

plt.xticks(rotation=45)

plt.tight_layout()

# Save the figure

plt.savefig('rainfall_prediction_2026.png', dpi=300)

plt.show()

# Additional visualization: Feature importance

feature_names = ['Temperature', 'Pressure', 'Humidity', 'Wind Speed']

feature_importance = model.feature_importances_

# Create a bar chart of feature importance

plt.figure(figsize=(10, 6))

plt.bar(feature_names, feature_importance)

plt.title('Feature Importance for Rainfall Prediction', fontsize=14)

plt.xlabel('Features', fontsize=12)

plt.ylabel('Importance', fontsize=12)

plt.grid(axis='y', linestyle='--', alpha=0.7)

plt.tight_layout()

# Save the figure

plt.savefig('feature_importance.png', dpi=300)

plt.show()

# Additional visualization: Prediction confidence

# Get prediction probabilities for the 2026 data

y_prob_2026 = model.predict_proba(X_2026_sim)[:, 1]  # Probability of rain

plt.figure(figsize=(12, 6))

plt.plot(time_2026, y_prob_2026, '-o', linewidth=2, markersize=6, color='orange')

plt.axhline(y=0.5, color='r', linestyle='--', alpha=0.7)

plt.title('Rainfall Prediction Probability for Qingming Festival 2026', fontsize=14)

plt.xlabel('Time (hours)', fontsize=12)

plt.ylabel('Probability of Rain', fontsize=12)

plt.grid(True)

plt.ylim(0, 1)

# Format the x-axis to show hours

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))

plt.xticks(rotation=45)

plt.tight_layout()

# Save the figure

plt.savefig('rainfall_probability_2026.png', dpi=300)

plt.show()

# Create a more comprehensive visualization combining weather parameters and rain prediction

fig, axs = plt.subplots(5, 1, figsize=(14, 15), sharex=True)

# Plot temperature

axs[0].plot(time_2026, sim_T, '-o', color='red')

axs[0].set_ylabel('Temperature (°C)', fontsize=12)

axs[0].grid(True)

axs[0].set_title('Simulated Weather Parameters and Rainfall Prediction for Qingming Festival 2026', fontsize=14)

# Plot pressure

axs[1].plot(time_2026, sim_P0, '-o', color='purple')

axs[1].set_ylabel('Pressure (hPa)', fontsize=12)

axs[1].grid(True)

# Plot humidity

axs[2].plot(time_2026, sim_U, '-o', color='blue')

axs[2].set_ylabel('Humidity (%)', fontsize=12)

axs[2].grid(True)

# Plot wind speed

axs[3].plot(time_2026, sim_Ff, '-o', color='green')

axs[3].set_ylabel('Wind Speed (m/s)', fontsize=12)

axs[3].grid(True)

# Plot rainfall prediction

axs[4].bar(time_2026, y_pred_2026, color='gray', alpha=0.7)

axs[4].plot(time_2026, y_prob_2026, '-o', color='orange', linewidth=2)

axs[4].set_ylabel('Rain Prediction', fontsize=12)

axs[4].set_xlabel('Time (hours)', fontsize=12)

axs[4].set_ylim(0, 1.1)

axs[4].axhline(y=0.5, color='r', linestyle='--', alpha=0.7)

axs[4].grid(True)

axs[4].legend(['Probability', 'Prediction (0/1)'], loc='upper right')

# Format the x-axis to show hours

for ax in axs:

    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))

plt.xticks(rotation=45)

plt.tight_layout()

# Save the figure

plt.savefig('comprehensive_weather_prediction_2026.png', dpi=300)

plt.show()

% 1. 读取数据

data = readtable('西安.xlsx'); % 读取 Excel 数据

% 2. 处理时间列,将其转换为 MATLAB 可处理的日期格式

data.time = datetime(data.time, 'InputFormat', 'dd.MM.yyyy HH:mm');

% 3. 提取特征和标签

X = data{:, {'T', 'P0', 'U', 'Ff'}}; % 特征:温度T,气压P0,相对湿度U,风速Ff

y = data.WW; % 标签:WW(1为有雨,0为无雨)

% 4. 划分训练集和测试集(80% 训练,20% 测试)

cv = cvpartition(length(y), 'HoldOut', 0.2);

X_train = X(training(cv), :);

y_train = y(training(cv));

X_test = X(test(cv), :);

y_test = y(test(cv));

% 5. 使用随机森林进行建模

mdl = fitcensemble(X_train, y_train, 'Method', 'Bag', 'NumLearningCycles', 100, 'Learner', 'Tree');

% 6. 进行预测

y_pred = predict(mdl, X_test);

% 7. 评估模型性能(使用混淆矩阵,精度等指标)

confMat = confusionmat(y_test, y_pred); % 混淆矩阵

disp('Confusion Matrix:');

disp(confMat);

% 计算准确率

accuracy = sum(y_pred == y_test) / length(y_test);

disp(['Accuracy: ', num2str(accuracy)]);

% 计算精确度,召回率和F1分数

precision = confMat(2, 2) / (confMat(2, 2) + confMat(1, 2)); % 精确度

recall = confMat(2, 2) / (confMat(2, 2) + confMat(2, 1)); % 召回率

f1_score = 2 * (precision * recall) / (precision + recall); % F1分数

disp(['Precision: ', num2str(precision)]);

disp(['Recall: ', num2str(recall)]);

disp(['F1 Score: ', num2str(f1_score)]);

% 假设我们想根据2018-2025年数据模拟2026年的气象数据

% 先获取2018-2025年的数据(训练集的部分)

X_train = data{:, {'T', 'P0', 'U', 'Ff'}}; % 特征:温度T,气压P0,相对湿度U,风速Ff

% 从训练数据中提取每一列特征的均值和标准差

mean_T = mean(X_train(:, 1)); % 温度T均值

std_T = std(X_train(:, 1)); % 温度T标准差

mean_P0 = mean(X_train(:, 2)); % 气压P0均值

std_P0 = std(X_train(:, 2)); % 气压P0标准差

mean_U = mean(X_train(:, 3)); % 湿度U均值

std_U = std(X_train(:, 3)); % 湿度U标准差

mean_Ff = mean(X_train(:, 4)); % 风速Ff均值

std_Ff = std(X_train(:, 4)); % 风速Ff标准差

% 模拟2026年清明节的24小时气象数据(根据均值和标准差生成)

n_hours = 24; % 24小时

% 使用正态分布来模拟数据(这里的模拟假设与历史数据特征一致)

sim_T = mean_T + std_T * randn(n_hours, 1); % 模拟温度T

sim_P0 = mean_P0 + std_P0 * randn(n_hours, 1); % 模拟气压P0

sim_U = mean_U + std_U * randn(n_hours, 1); % 模拟湿度U

sim_Ff = mean_Ff + std_Ff * randn(n_hours, 1); % 模拟风速Ff

% 结合模拟的气象数据

X_2026_sim = [sim_T, sim_P0, sim_U, sim_Ff];

% 使用训练好的模型对2026年模拟的气象数据进行降雨预测

y_pred_2026 = predict(mdl, X_2026_sim); % 预测2026年24小时的降雨状态

% 生成时间序列(2026年清明节的24小时)

time_2026 = datetime(2026, 4, 4, 0, 0, 0) + hours(0:23); % 从2026年4月4日0点开始,24小时的时间序列

figure;

plot(time_2026, y_pred_2026, '-o', 'LineWidth', 2, 'MarkerSize', 6);

title('2026年清明节24小时降雨预测', 'FontSize', 14, 'FontName', 'Microsoft YaHei'); % 设置标题字体

xlabel('时间 (小时)', 'FontSize', 12, 'FontName', 'Microsoft YaHei'); % 设置X轴标签字体

ylabel('降雨状态(1:有雨,0:无雨)', 'FontSize', 12, 'FontName', 'Microsoft YaHei'); % 设置Y轴标签字体

grid on;

% 美化图表

xtickformat('HH:mm'); % 格式化X轴时间显示

xtickangle(45); % 让X轴的时间标签倾斜45度,便于阅读

set(gca, 'FontSize', 12, 'FontName', 'Microsoft YaHei'); % 设置坐标轴字体

相关知识

2024数学建模国赛C题【农作物的种植策略】思路详解
2024 年高教社杯全国大学生数学建模竞赛 C 题 农作物的种植策略 详细思路+matlab代码+python代码+论文范例
【2024国赛C题】【农作物的种植策略】2024 年全国大学生数学建模比赛思路、代码更新中.....
[已更新代码思路]2024数学建模国赛高教社杯C题:农作物的种植策略 思路代码文章助攻手把手保姆级
2023年APMCM亚太杯数学建模竞赛B题思路解析
【C语言】求水仙花数(完整代码)
2024年全国大学生数学建模C题解题思路
2024 年高教社杯全国大学生数学建模竞赛 C 题 农作物的种植策略(详细思路+matlab代码+python代码+论文范例)
Scratch实现植物大战僵尸游戏:完整代码与素材包分享
高中生物遗传学题目的解题思路及方法,快来收藏!

网址: 2025年数维杯C题完整求解思路讲解+代码分享 https://www.huajiangbk.com/newsview2460045.html

所属分类:花卉
上一篇: 东湖樱花园开放时间2025最佳观
下一篇: 东京樱花几月开 东京樱花盛开时间

推荐分享