本章节为 数值数据 处理总结,其中包括数值特征、Map类别转换、One-hot Encoding、数值数据基本描述、二值特征、多项式特征、数值区统计归类特征、分位数切分、对数变换、日期相关特征、时间相关特征的介绍。
文本介绍关于数据分析工作中常用的 使用Python进行数据预处理 的方法总结。通过对图片数据、数值数字、文本数据、特征提取、特征处理等方面讲解作为一名数据分析师常用的数据处理套路。
import pandas as pd import numpy as np
# 读取观察数据 vg_df = pd.read_csv('datasets/vgsales.csv', encoding = "ISO-8859-1") vg_df[['Name', 'Platform', 'Year', 'Genre', 'Publisher']].iloc[1:7]
# 离散变量字段取唯一值 genres = np.unique(vg_df['Genre']) genres >>> array(['Action', 'Adventure', 'Fighting', 'Misc', 'Platform', 'Puzzle','Racing', 'Role-Playing', 'Shooter', 'Simulation', 'Sports','Strategy'], dtype=object) # 离散变量变换转换 from sklearn.preprocessing import LabelEncoder gle = LabelEncoder() genre_labels = gle.fit_transform(vg_df['Genre']) genre_labels >>> array([10, 4, 6, ..., 6, 5, 4]) # 类别变量字典映射 genre_mappings = {index: label for index, label in enumerate(gle.classes_)} genre_mappings >>> {0: 'Action', 1: 'Adventure', 2: 'Fighting', 3: 'Misc', 4: 'Platform', 5: 'Puzzle', 6: 'Racing', 7: 'Role-Playing', 8: 'Shooter', 9: 'Simulation', 10: 'Sports', 11: 'Strategy'} # 数据DF 切片操作 vg_df['GenreLabel'] = genre_labels vg_df[['Name', 'Platform', 'Year', 'Genre', 'GenreLabel']].iloc[1:7]
poke_df = pd.read_csv('datasets/Pokemon.csv', encoding='utf-8') poke_df = poke_df.sample(random_state=1, frac=1).reset_index(drop=True) np.unique(poke_df['Generation']) >>> array(['Gen 1', 'Gen 2', 'Gen 3', 'Gen 4', 'Gen 5', 'Gen 6'], dtype=object) # 构建MAP转换字典 gen_ord_map = {'Gen 1': 1, 'Gen 2': 2, 'Gen 3': 3, 'Gen 4': 4, 'Gen 5': 5, 'Gen 6': 6} # 哑变量转换 poke_df['GenerationLabel'] = poke_df['Generation'].map(gen_ord_map) poke_df[['Name', 'Generation', 'GenerationLabel']].iloc[4:10]
# 提取需要转换的数据 poke_df[['Name', 'Generation', 'Legendary']].iloc[4:10]
# 使用MAP将类别变量转换成数值 from sklearn.preprocessing import OneHotEncoder, LabelEncoder gen_le = LabelEncoder() gen_labels = gen_le.fit_transform(poke_df['Generation']) poke_df['Gen_Label'] = gen_labels leg_le = LabelEncoder() leg_labels = leg_le.fit_transform(poke_df['Legendary']) poke_df['Lgnd_Label'] = leg_labels poke_df_sub = poke_df[['Name', 'Generation', 'Gen_Label', 'Legendary', 'Lgnd_Label']] poke_df_sub.iloc[4:10]
# 在原有DF中创建 One-hot Encoding 字段 gen_ohe = OneHotEncoder() gen_feature_arr = gen_ohe.fit_transform(poke_df[['Gen_Label']]).toarray() gen_feature_labels = list(gen_le.classes_) print (gen_feature_labels) gen_features = pd.DataFrame(gen_feature_arr, columns=gen_feature_labels) >>> ['Gen 1', 'Gen 2', 'Gen 3', 'Gen 4', 'Gen 5', 'Gen 6'] leg_ohe = OneHotEncoder() leg_feature_arr = leg_ohe.fit_transform(poke_df[['Lgnd_Label']]).toarray() leg_feature_labels = ['Legendary_'+str(cls_label) for cls_label in leg_le.classes_] print (leg_feature_labels) leg_features = pd.DataFrame(leg_feature_arr, columns=leg_feature_labels) >>> ['Legendary_False', 'Legendary_True'] # 进行转换 poke_df_ohe = pd.concat([poke_df_sub, gen_features, leg_features], axis=1) columns = sum([['Name', 'Generation', 'Gen_Label'],gen_feature_labels, ['Legendary', 'Lgnd_Label'],leg_feature_labels], []) poke_df_ohe[columns].iloc[4:10]
poke_df = pd.read_csv('datasets/Pokemon.csv', encoding='utf-8') poke_df.head()
poke_df[['HP', 'Attack', 'Defense']].head()
poke_df[['HP', 'Attack', 'Defense']].describe()
watched = np.array(popsong_df['listen_count']) watched[watched >= 1] = 1 popsong_df['watched'] = watched popsong_df.head(10)
# 基于阈值判断转换类别 from sklearn.preprocessing import Binarizer bn = Binarizer(threshold=0.9) pd_watched = bn.transform([popsong_df['listen_count']])[0] popsong_df['pd_watched'] = pd_watched popsong_df.head(11)
atk_def = poke_df[['Attack', 'Defense']] atk_def.head()
# 2 次多项式的次数为 [1,a,b,a方,ab,b方] from sklearn.preprocessing import PolynomialFeatures pf = PolynomialFeatures(degree=2, interaction_only=False, include_bias=False) res = pf.fit_transform(atk_def) res
intr_features = pd.DataFrame(res, columns=['Attack', 'Defense', 'Attack^2', 'Attack x Defense', 'Defense^2']) intr_features.head(5)
fcc_survey_df = pd.read_csv('datasets/fcc_2016_coder_survey_subset.csv', encoding='utf-8') fcc_survey_df[['ID.x', 'EmploymentField', 'Age', 'Income']].head()
# 构建年龄直方图 fig, ax = plt.subplots() fcc_survey_df['Age'].hist(color='#A9C5D3') ax.set_title('Developer Age Histogram', fontsize=12) ax.set_xlabel('Age', fontsize=12) ax.set_ylabel('Frequency', fontsize=12)
# 以年龄除以10为阶段进行划分 fcc_survey_df['Age_bin_round'] = np.array(np.floor(np.array(fcc_survey_df['Age']) / 10.)) fcc_survey_df[['ID.x', 'Age', 'Age_bin_round']].iloc[1071:1076]
fcc_survey_df[['ID.x', 'Age', 'Income']].iloc[4:9]
# 构建直方图 fig, ax = plt.subplots() fcc_survey_df['Income'].hist(bins=30, color='#A9C5D3') ax.set_title('Developer Income Histogram', fontsize=12) ax.set_xlabel('Developer Income', fontsize=12) ax.set_ylabel('Frequency', fontsize=12)
# 四分位区分 quantile_list = [0, .25, .5, .75, 1.] quantiles = fcc_survey_df['Income'].quantile(quantile_list) quantiles
# 四分卫可视化 fig, ax = plt.subplots() fcc_survey_df['Income'].hist(bins=30, color='#A9C5D3') for quantile in quantiles: qvl = plt.axvline(quantile, color='r') ax.legend([qvl], ['Quantiles'], fontsize=10) ax.set_title('Developer Income Histogram with Quantiles', fontsize=12) ax.set_xlabel('Developer Income', fontsize=12) ax.set_ylabel('Frequency', fontsize=12)
# 基于分位数的数据描述,添加对应的标签 quantile_labels = ['0-25Q', '25-50Q', '50-75Q', '75-100Q'] fcc_survey_df['Income_quantile_range'] = pd.qcut(fcc_survey_df['Income'], q=quantile_list) fcc_survey_df['Income_quantile_label'] = pd.qcut(fcc_survey_df['Income'], q=quantile_list, labels=quantile_labels) fcc_survey_df[['ID.x', 'Age', 'Income', 'Income_quantile_range', 'Income_quantile_label']].iloc[4:9]
fcc_survey_df['Income_log'] = np.log((1+ fcc_survey_df['Income'])) fcc_survey_df[['ID.x', 'Age', 'Income', 'Income_log']].iloc[4:9]
# 数值数据取LOG后 可视化直方图 income_log_mean = np.round(np.mean(fcc_survey_df['Income_log']), 2) fig, ax = plt.subplots() fcc_survey_df['Income_log'].hist(bins=30, color='#A9C5D3') plt.axvline(income_log_mean, color='r') ax.set_title('Developer Income Histogram after Log Transform', fontsize=12) ax.set_xlabel('Developer Income (log scale)', fontsize=12) ax.set_ylabel('Frequency', fontsize=12) ax.text(11.5, 450, r'$\mu$='+str(income_log_mean), fontsize=10)
import datetime import numpy as np import pandas as pd from dateutil.parser import parse import pytz time_stamps = ['2015-03-08 10:30:00.360000+00:00', '2017-07-13 15:45:05.755000-07:00', '2012-01-20 22:30:00.254000+05:30', '2016-12-25 00:30:00.000000+10:00'] df = pd.DataFrame(time_stamps, columns=['Time']) df
# 转换日期类型 ts_objs = np.array([pd.Timestamp(item) for item in np.array(df.Time)]) df['TS_obj'] = ts_objs ts_objs
# 提取日期中的字段信息构建新的日期分类字段 df['Year'] = df['TS_obj'].apply(lambda d: d.year) df['Month'] = df['TS_obj'].apply(lambda d: d.month) df['Day'] = df['TS_obj'].apply(lambda d: d.day) df['DayOfWeek'] = df['TS_obj'].apply(lambda d: d.dayofweek) df['DayName'] = df['TS_obj'].apply(lambda d: d.weekday_name) df['DayOfYear'] = df['TS_obj'].apply(lambda d: d.dayofyear) df['WeekOfYear'] = df['TS_obj'].apply(lambda d: d.weekofyear) df['Quarter'] = df['TS_obj'].apply(lambda d: d.quarter) df[['Time', 'Year', 'Month', 'Day', 'Quarter', 'DayOfWeek', 'DayName', 'DayOfYear', 'WeekOfYear']]
df['Hour'] = df['TS_obj'].apply(lambda d: d.hour) df['Minute'] = df['TS_obj'].apply(lambda d: d.minute) df['Second'] = df['TS_obj'].apply(lambda d: d.second) df['MUsecond'] = df['TS_obj'].apply(lambda d: d.microsecond) #毫秒 df['UTC_offset'] = df['TS_obj'].apply(lambda d: d.utcoffset()) #UTC时间位移 df[['Time', 'Hour', 'Minute', 'Second', 'MUsecond', 'UTC_offset']]
# 按照早晚切分时间 hour_bins = [-1, 5, 11, 16, 21, 23] bin_names = ['Late Night', 'Morning', 'Afternoon', 'Evening', 'Night'] df['TimeOfDayBin'] = pd.cut(df['Hour'], bins=hour_bins, labels=bin_names) df[['Time', 'Hour', 'TimeOfDayBin']]