2024华数杯数学建模C题Python代码+可视化结果数据教学
C题题目:老外游中国
导入库:
import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
确定BS点:
# 查看评分中包含的所有可能值
directory = '附件'
for filename in os.listdir(directory):
if filename.endswith('.csv'):
city_data = pd.read_csv(os.path.join(directory, filename))
unique_values = city_data['评分'].unique()
print(unique_values)
确定最大评分值:
# 确定最大评分值
max_value = -np.inf
for filename in os.listdir(directory):
if filename.endswith('.csv'):
city_data = pd.read_csv(os.path.join(directory, filename))
# 去掉NaN和'--'值
filtered_scores = city_data['评分'].replace('--', np.nan).dropna()
# 将评分转换为float类型
filtered_scores = filtered_scores.astype(float)
# 找到当前文件中的最大值
max_value_in_file = filtered_scores.max()
# 更新全局最大值
if max_value_in_file > max_value:
max_value = max_value_in_file
print("最大评分值为:", max_value)
全国获得评分为5.0的景点数量: 2563
# 统计各个城市对应的BS景点数
city_bs_count = {}
for filename in os.listdir(directory):
if filename.endswith('.csv'):
city_data = pd.read_csv(os.path.join(directory, filename))
city_name = filename.split('.')[0] # 假设文件名是城市名
# 去掉NaN和'--'值
filtered_scores = city_data['评分'].replace('--', np.nan).dropna()
# 将评分转换为float类型
filtered_scores = filtered_scores.astype(float)
# 统计评分为5.0的数量
bs_count = (filtered_scores == 5.0).sum()
# 将结果存放在字典中
city_bs_count[city_name] = bs_count
print("每个城市的BS景点数量:", city_bs_count)
这里缺了一段,需要的看文末
进行可视化:
import matplotlib.font_manager as fm
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体
plt.rcParams['axes.unicode_minus'] = False # 解决坐标轴负数显示问题
# 可视化:最高评分景点数量统计
plt.figure(figsize=(12, 6))
bars = plt.bar(range(len(sorted_city_bs_count)), sorted_city_bs_count.values(), tick_label=list(sorted_city_bs_count.keys()))
plt.xlabel('City')
plt.ylabel('Number of BS Scored Sites')
plt.title('Number of BS Scored Sites in Each City')
plt.xticks(rotation=90)
# 调整x轴刻度间隔为10
plt.xticks(ticks=range(0, len(sorted_city_bs_count), 10), labels=list(sorted_city_bs_count.keys())[::10], rotation=90)
plt.show()
其中更详细的思路、各题目思路、代码、讲解视频、成品论文及其他相关内容,可以点击下面的名片: