

本项目旨在基于多种健康指标构建机器学习模型,对个体进行糖尿病与否的分类。使用的数据集是“Diabetes Binary Health Indicators BRFSS 2015”,来自美国疾病控制与预防中心(CDC)。
数据集通过以下代码加载: python df = pd.read_csv("diabetes_binary_health_indicators_BRFSS2015.csv")
生成的概况报告提供了数据集的全面概览,包括分布、缺失值、相关性等: python profile = ProfileReport(df, title="Profiling Report") profile.to_file("analysis_report.html")
基本探索包括数据集的前几行、列信息、统计摘要、信息概览、缺失值、重复行和唯一值数量以及相关性矩阵: python print("First few rows of the dataset:") df.head()
print("Columns in the dataset:") df.columns
print("Statistical summary of the dataset:") df.describe().T
print("Information about the dataset:") df.info()
print("Number of missing values in each column:") df.isnull().sum()
print("Number of duplicated rows in the dataset:") df.duplicated().sum()
print("Number of unique values in each column:") df.nunique()
print("Correlation matrix:") df.corr(numeric_only=True)
可视化探索包括相关性热图、糖尿病二元分类的类分布图以及与糖尿病二元分类的相关性图: python plt.figure(figsize=(16,10)) sns.heatmap(df.corr(), annot=True) plt.show()
sns.countplot(x=Diabetes_binary, data=df) plt.title("Class Distribution of Diabetes_binary") plt.show()
plt.figure(figsize=(12, 8)) df.corr()[Diabetes_binary].sort_values().plot(kind=bar) plt.title(Correlation with Diabetes_binary) plt.show()
数据集不含缺失值,但有重复行需要处理。
数据集被分割为训练集和测试集: python X = df.drop(columns=Diabetes_binary) y = df[Diabetes_binary] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
使用多种缩放器处理数据:
使用SMOTE处理不平衡数据: python smote = SMOTE(random_state=42) X_train_res, y_train_res = smote.fit_resample(X_train, y_train)
构建了多种分类模型,包括:
python pipeline = Pipeline([ (scaler, StandardScaler()), (classifier, RandomForestClassifier()) ])
param_grid = { classifier__C: [0.1, 1, 10], classifier__penalty: [l2] }
grid_search = GridSearchCV(pipeline, param_grid, cv=5) grid_search.fit(X_train_res, y_train_res)
模型评估使用以下指标:
python from sklearn.metrics import classification_report, confusion_matrix y_pred = grid_search.predict(X_test) print(classification_report(y_test, y_pred)) print(confusion_matrix(y_test, y_pred))
本项目成功使用多种机器学习模型对个体进行糖尿病与否的分类。集成方法表现最佳,处理类别不平衡对提升模型性能至关重要。