大数据分析统计
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
pm25files = ['PM2.5_2021.csv', 'PM2.5_2022.csv', 'PM2.5_2023.csv']
pm10files = ['PM10_2021.csv', 'PM10_2022.csv', 'PM10_2023.csv']
def read_csv_file(files):
# 每个文件都有表头,要注意去掉
df = pd.read_csv(files[0])
for file in files[1:]:
df_temp = pd.read_csv(file)
df = pd.concat([df, df_temp])
# 将日期列解析为 Pandas 的日期时间对象
df['Date'] = pd.to_datetime(df['Date'])
monthly_avg_pm = df.groupby(df['Date'].dt.to_period('M'))['1004ABJ'].mean()
print(monthly_avg_pm)
# 初始化列表,用于存储年月和对应的值
month_list = []
pm_values_list = []
# 迭代每个组,并将年月和对应的值添加到列表中
for month, pm_value in monthly_avg_pm.items():
month_list.append(str(month))
pm_values_list.append(pm_value)
return month_list, pm_values_list
# 读取 CSV 文件
month_list1, pm_values_list1 = read_csv_file(pm25files)
month_list2, pm_values_list2 = read_csv_file(pm10files)
# 创建拆线图
plt.figure(figsize=(10, 6))
plt.plot(month_list1, pm_values_list1, marker='o', linestyle='-', label='PM2.5')
plt.plot(month_list2, pm_values_list2, marker='o', linestyle='-', label='PM10')
# 设置图形标题和坐标轴标签
plt.title('Monthly Average PM2.5 & PM10 Trend')
plt.xlabel('Year-Month')
plt.ylabel('Average PM2.5 && PM10 Value')
# 旋转 x 轴标签,以避免重叠
plt.xticks(rotation=45)
# 显示网格线
plt.grid(True)
# 显示图形
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# 将日期时间对象转换为时间戳
timestamp_list = [datetime.fromisoformat(date_str+"-01").timestamp() for date_str in month_list1]
x_data = np.array(timestamp_list)
y_data = np.array(pm_values_list1)
# 将 x_data 转换为二维数组
x_data = x_data.reshape(-1, 1)
# 创建线性回归模型对象
model = LinearRegression()
# 拟合数据
model.fit(x_data, y_data)
# 获取拟合得到的斜率和截距
slope = model.coef_[0]
intercept = model.intercept_
timestamp_list2 = [datetime.fromisoformat(date_str+"-01").timestamp() for date_str in month_list2]
x_data2 = np.array(timestamp_list2)
y_data2 = np.array(pm_values_list2)
# 将 x_data 转换为二维数组
x_data2 = x_data.reshape(-1, 1)
# 创建线性回归模型对象
model2 = LinearRegression()
# 拟合数据
model2.fit(x_data, y_data)
# 获取拟合得到的斜率和截距
slope2 = model2.coef_[0]
intercept2 = model2.intercept_
# 计算拟合直线上的点
fit_line_y = slope * x_data + intercept
fit_line_y2 = slope2 * x_data2 + intercept2
# 绘制原始数据和拟合直线
plt.scatter(x_data, y_data, label='Original Data')
plt.plot(x_data, fit_line_y, color='red', label='Fitted Line')
plt.scatter(x_data2, y_data2, label='Original Data')
plt.plot(x_data2, fit_line_y2, color='yellow', label='Fitted Line')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Linear Trend Fit')
plt.legend()
plt.grid(True)
plt.show()