我有一段代码:import pandas as pd import numpy as np from scipy import stats import…
- User
我有一段代码:import pandas as pd import numpy as np from scipy import stats import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler, StandardScaler
读取微测站数据
micro_station_data = pd.read_excel(r'C:\Users\123\Desktop\micro_station_data.xlsx', skiprows=1)
读取国测站数据
national_station_data = pd.read_excel(r'C:\Users\123\Desktop\national_station_data.xls', skiprows=1)
数据预处理
def preprocess_data(data): # 将“时段”列转换为datetime格式 data['时段'] = pd.to_datetime(data['时段'], format ='%Y/%m/%d %H:%M:%S') # 对所有值为-99.0(E)和0.0(E)的数据进行空值处理 columns_to_replace = ['SO2', 'CO(mg/m3)', 'NO2', 'O3-1H', 'PM10', 'PM2.5', 'NO', 'NOX', '湿度', '温度', '风速', '风向', '大气压'] data.loc[:, columns_to_replace] = data.loc[:, columns_to_replace].replace([-99.0, 0.0], pd.NA) # 将非数字字符串替换为NaN值 data.loc[:, columns_to_replace] = data.loc[:, columns_to_replace].apply(pd.to_numeric, errors='coerce') # 使用各列的非空值均值替换空值 data = data.fillna(data.mean()) # 处理第一列数据 data['时段'] = data['时段'].dt.strftime('%Y-%m-%d %H:%M:%S') return data
对微测站数据进行预处理
preprocessed_micro_station_data = preprocess_data(micro_station_data)
对国测站数据进行预处理
preprocessed_national_station_data = preprocess_data(national_station_data)
Z-Score异常值检测
def detect_outliers(data): # 移除非数值型数据 numeric_data = data.select_dtypes(include=['int', 'float']) # 计算Z-Score z_scores = [] threshold = 3 # 检测异常值 outliers = [] for col in numeric_data.columns: try: z_score = stats.zscore(numeric_data[col]) z_scores.append(z_score) except TypeError: continue z_scores = np.array(z_scores) for i, row in enumerate(z_scores): for j, score in enumerate(row): if not np.isnan(score) and abs(score) > threshold: outliers.append((i, j))
return outliers对预处理后的微测站数据进行异常值检测
micro_station_outliers = detect_outliers(preprocessed_micro_station_data)
归一化
def normalize_data(data): scaler = MinMaxScaler() columns_to_normalize = ['SO2', 'CO(mg/m3)', 'NO2', 'O3-1H', 'PM10', 'PM2.5', 'NO', 'NOX', '湿度', '温度', '风速', '风向', '大气压'] normalized_data = scaler.fit_transform(data[columns_to_normalize]) # 将归一化后的数据与原数据进行合并 data[columns_to_normalize] = normalized_data return data
标准化
def standardize_data(data): scaler = StandardScaler() columns_to_standardize = ['SO2', 'CO(mg/m3)', 'NO2', 'O3-1H', 'PM10', 'PM2.5', 'NO', 'NOX', '湿度', '温度', '风速', '风向', '大气压'] standardized_data = scaler.fit_transform(data[columns_to_standardize]) # 将标准化后的数据与原数据进行合并 data[columns_to_standardize] = standardized_data return data
对预处理后的微测站数据进行归一化
normalized_micro_station_data = normalize_data(preprocessed_micro_station_data)
对预处理后的国测站数据进行标准化
standardized_national_station_data = standardize_data(preprocessed_national_station_data) import numpy as np from sklearn.metrics import mean_squared_error
- Assistant
这段代码主要包括以下内容:
- 导入所需的库:pandas、numpy、scipy、matplotlib.pyplot、sklearn.preprocessing中的MinMaxScaler和StandardScaler。
- 读取微测站数据和国测站数据的Excel文件。
- 定义数据预处理函数preprocess_data,对数据进行预处理,主要包括将“时段”列转换为datetime格式、对特定数值进行空值处理、将非数字字符串替换为NaN值、使用各列的非空值均值替换空值,并处理第一列数据。
- 对微测站数据和国测站数据分别进行预处理。
- 定义异常值检测函数detect_outliers,使用Z-Score方法检测异常值。
- 对预处理后的微测站数据进行异常值检测。
- 定义归一化函数normalize_data和标准化函数standardize_data,分别使用MinMaxScaler和StandardScaler对数据进行归一化和标准化。
- 对预处理后的微测站数据进行归一化和国测站数据进行标准化。
- 定义校准模型CalibrationModel,包括初始化校准系数和校准数据的方法,以及评估校准模型误差的方法。
- 定义遗传算法、粒子群算法和蚁群算法,分别使用遗传算法、粒子群算法和蚁群算法对数据进行校准,返回最好的校准模型。
- 定义选择进化学习算法函数select_calibration_algorithm,根据选择的算法调用相应的校准函数。
- 使用粒子群算法对微测站数据进行校准,得到校准后的微测站数据。
- 使用进一步校正函数further_calibration对校准后的微测站数据进行进一步校正。
- 输出校准后和进一步校正后的微测站数据到Excel文件。
- 统计校准后微测站数据的均值和标准差,并进行可视化展示。
- User
该代码报错:E:\PYTHON\lib\tkinter_init_.py:839: UserWarning: Glyph 28287 (\N{CJK UNIFIED IDEOGRAPH-6E7F}) missing from current font. func(*args) E:\PYTHON\lib\tkinter_init_.py:839: UserWarning: Glyph 24230 (\N{CJK UNIFIED IDEOGRAPH-5EA6}) missing from current font. func(*args) E:\PYTHON\lib\tkinter_init_.py:839: UserWarning: Glyph 28201 (\N{CJK UNIFIED IDEOGRAPH-6E29}) missing from current font. func(*args) E:\PYTHON\lib\tkinter_init_.py:839: UserWarning: Glyph 39118 (\N{CJK UNIFIED IDEOGRAPH-98CE}) missing from current font. func(*args) E:\PYTHON\lib\tkinter_init_.py:839: UserWarning: Glyph 36895 (\N{CJK UNIFIED IDEOGRAPH-901F}) missing from current font. func(*args) E:\PYTHON\lib\tkinter_init_.py:839: UserWarning: Glyph 21521 (\N{CJK UNIFIED IDEOGRAPH-5411}) missing from current font. func(*args) E:\PYTHON\lib\tkinter__.py:839: UserWarning: Glyph 22823 (\N{CJK UNIFIED IDEOGRAPH-5927}) missing from current font. func(*args) E:\PYTHON\lib\tkinter__.py:839: UserWarning: Glyph 27668 (\N{CJK UNIFIED IDEOGRAPH-6C14}) missing from current font. func(*args) E:\PYTHON\lib\tkinter__.py:839: UserWarning: Glyph 21387 (\N{CJK UNIFIED IDEOGRAPH-538B}) missing from current font. func(*args) Traceback (most recent call last): File "E:\PYTHON\pythonProject4\shenduyouxian.py", line 247, in plt.show() File "E:\PYTHON\pythonProject4\venv\lib\site-packages\matplotlib\pyplot.py", line 446, in show return .py", line 1458, in mainloop self.tk.mainloop(n) KeyboardInterrupt
- Assistant
这个报错是因为matplotlib在显示图形时使用了一些中文字符,但是当前的字体库中缺失这些字符导致的。解决这个问题可以尝试以下