DAY 8

知识点见示例代码

  • 字典的简单介绍
  • 标签编码
  • 连续特征的处理:归一化和标准化

对心脏病数据集的特征用上述知识完成,一次性用所有的处理方式完成预处理,尝试手动完成,多敲几遍代码。

#认识字典
dict = {'name': 'zixuan', 'age':23,'college':'UCL'}
dict
dict['name']
 
#数据预处理
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
dt = pd.read_csv(r'data.csv')
dt.head()
dt.info()
dt.isnull().sum()
 
#缺失值处理
discrete_features = []
continuous_features = []
for feature in dt.columns:
    if dt[feature].dtype == 'object':
        discrete_features.append(feature)
    else:
        continuous_features.append(feature)
print(f'离散特征:{discrete_features}')
print(f'连续特征:{continuous_features}')
 
for features in discrete_features:
    mode_value = dt[features].mode()[0]
    dt[features].fillna(mode_value,inplace=True)
    print(f"列 '{features}' 使用众数 {mode_value} 填补空值")
 
for features in continuous_features:
    median_value = dt[features].median()
    dt[features].fillna(median_value,inplace=True)
    print(f"列 '{features}' 使用中位数 {median_value} 填补空值")
 
#离散特征做标签编码/独热编码
dt["Home Ownership"].value_counts()
dt['Years in current job'].value_counts()
dt['Purpose'].value_counts()
dt['Term'].value_counts()
mapping = {
    'Home Ownership': {
        'Own Home': 0,
        'Rent': 1,
        'Have Mortgage': 2,
        'Home Mortgage': 3
    },
    'Term': {
        'Short Term': 0,
        'Long Term': 1
    },
    'Purpose': {
        'debt_consolidation': 2,
        'buy house': 1,
        'business loan': 1,
        'major purchase': 1,
        'small business': 1,
        'other': 0,
        'home improvements': 0,
        'buy a car': 0,
        'medical bills': 0,
        'take a trip': 0,
        'wedding': 0,
        'moving': 0,
        'educational expenses': 0,
        'vacation': 0,
        'renewable energy': 0
    },
    'Years in current job': {
        '10+ years': 0,
        '9 years': 1,
        '8 years': 1,
        '7 years': 2,
        '6 years': 2,
        '5 years': 3,
        '4 years': 3,
        '3 years': 4,
        '2 years': 4,
        '< 1 year': 5
    }
}
 
dt["Home Ownership"] = dt["Home Ownership"].map(mapping["Home Ownership"])
dt["Term"] = dt["Term"].map(mapping["Term"])
dt["Purpose"] = dt["Purpose"].map(mapping["Purpose"])
dt["Years in current job"] = dt["Years in current job"].map(mapping["Years in current job"])
dt.head()
 
#连续特征做归一化/标准化
print(f'连续特征:{continuous_features}')
 
def manual_normalize(dt):
    min_val = dt.min()
    max_val = dt.max()
    normalized_data = (dt - min_val) / (max_val - min_val)
    return normalized_data
dt['Annual Income'] = manual_normalize(dt['Annual Income'])
dt['Tax Liens'] = manual_normalize(dt['Tax Liens'])
dt['Number of Open Accounts'] = manual_normalize(dt['Number of Open Accounts'])
dt['Years of Credit History'] = manual_normalize(dt['Years of Credit History'])
dt['Maximum Open Credit'] = manual_normalize(dt['Maximum Open Credit'])
dt['Number of Credit Problems'] = manual_normalize(dt['Number of Credit Problems'])
dt['Months since last delinquent'] = manual_normalize(dt['Months since last delinquent'])
dt['Bankruptcies'] = manual_normalize(dt['Bankruptcies'])
dt['Current Loan Amount'] = manual_normalize(dt['Current Loan Amount'])
dt['Current Credit Balance'] = manual_normalize(dt['Current Credit Balance'])
dt['Monthly Debt'] = manual_normalize(dt['Monthly Debt'])
dt['Credit Score'] = manual_normalize(dt['Credit Score'])
 
dt.head(10)

@浙大疏锦行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值