import pandas as pd from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from sklearn.preprocessing import LabelEncoder from sklearn.metrics import accuracy_score
# 预处理:对分类特征进行编码 label_encoders = {} for column in train_data.columns: if train_data[column].dtype == object: le = LabelEncoder() train_data[column] = le.fit_transform(train_data[column].astype(str)) label_encoders[column] = le if column in test_data.columns: test_data[column] = le.transform(test_data[column].astype(str))
# 预处理:对分类特征进行编码 label_encoders = {} for column in train_data.columns: if train_data[column].dtype == object: le = LabelEncoder() train_data[column] = le.fit_transform(train_data[column].astype(str)) label_encoders[column] = le if column in test_data.columns: test_data[column] = le.transform(test_data[column].astype(str))