疏锦行Python打卡 DAY 31 文件的规范拆分和写法

import pandas as pd
 
def load_heart_data(file_path: str) -> pd.DataFrame:
    return pd.read_csv(file_path)
import pandas as pd
from sklearn.preprocessing import StandardScaler
 
def handle_missing_values(df: pd.DataFrame) -> pd.DataFrame:
    
    numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
    df[numeric_cols] = df[numeric_cols].fillna(df[numeric_cols].median())
    return df
 
def scale_features(df: pd.DataFrame) -> pd.DataFrame:
    scaler = StandardScaler()
    numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
    df[numeric_cols] = scaler.fit_transform(df[numeric_cols])
    return df
from sklearn.ensemble import RandomForestClassifier
import joblib
 
def train_heart_model(X_train, y_train, model_path: str):
    
    model = RandomForestClassifier(n_estimators=100, random_state=42)
    model.fit(X_train, y_train)
    joblib.dump(model, model_path)  # 保存模型
    return model
import matplotlib.pyplot as plt
import seaborn as sns
 
def plot_correlation_heatmap(df: pd.DataFrame):
    plt.figure(figsize=(12, 8))
    sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
    plt.title('Feature Correlation Heatmap')
    return plt

打卡:@浙大疏锦行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值