想做一个公司的网站去哪可以做,做网站asp,蚌埠网站建设公司,少儿编程价格表文章目录 【文章系列】【前言】【比赛简介】【正文】#xff08;一#xff09;数据获取#xff08;二#xff09;数据分析1. 缺失值2. 重复值3. 属性类型分析4. 类别分析5. 分析目标数值占比 #xff08;三#xff09;属性分析1. 对年龄Age分析#xff08;1#xff09;… 文章目录 【文章系列】【前言】【比赛简介】【正文】一数据获取二数据分析1. 缺失值2. 重复值3. 属性类型分析4. 类别分析5. 分析目标数值占比 三属性分析1. 对年龄Age分析1直方图分析2创建新属性 2. 对支出属性进行分析1直方图分析2创建新属性 3. 对离散属性进行分析1直方图分析 4. 对定性属性进行分析1属性分析2根据PassengerId划分group3对Cabin属性进一步划分4根据Name的姓氏划分出家庭组 四归纳总结五写在最后 【文章系列】
第一章 初探Kaggle竞赛————Kaggle竞赛系列_Titanic比赛
第二章 知识补充_随机森林————数学建模系列_随机森林
第三章 知识补充_LightGBM————集成学习之Boosting方法系列_LightGBM
第四章 再战Kaggle竞赛————Kaggle竞赛系列_SpaceshipTitanic比赛
第五章 重温回顾_学习金牌方法_数据分析————Kaggle竞赛系列_SpaceshipTitanic金牌方案分析_数据分析
第六章 重温回顾_学习金牌方法_数据处理————Kaggle竞赛系列_SpaceshipTitanic金牌方案分析_数据处理
【前言】 Spaceship Titanic比赛类似Titanic比赛只是增加了更多的属性以及更大的数据量仍是一个二分类问题。 今天要分析的是一篇大神的解决方案看完后觉得干货满满由衷地敬佩他们对数据分析的细致程度对比之下只觉得之前自己的分析仅仅是表面功夫单纯靠着模型的强大能力去完成任务。看来以后还是得不断地向各位前辈大佬学习完善自己的解决方案 项目代码 Spaceship Titanic: A complete guide | Kaggle 我的解决方案Kaggle竞赛系列_SpaceshipTitanic比赛 【比赛简介】 Spaceship Titanic比赛是一个在Kaggle上举办的机器学习挑战参赛者的任务是预测Spaceship Titanic在与时空异常碰撞时哪些乘客被传送到了另一个维度。这个比赛提供了从飞船损坏的计算机系统中恢复的一组个人记录参赛者需要使用这些数据来进行预测。 【正文】
一数据获取 参考之前的Titanic比赛的数据获取操作 Kaggle_Titanic比赛 二数据分析 train.csv - 用于训练数据的大约三分之二约8700名乘客的个人记录。PassengerId - 每位乘客的唯一标识。每个标识的格式为gggg_pp其中gggg表示乘客所在的团体pp是他们在团体中的编号。一个团体中的人通常是家庭成员但并不总是如此。HomePlanet - 乘客出发的星球通常是他们的永久居住星球。CryoSleep - 表示乘客是否选择在航程期间被置于悬浮动状态。处于冷冻睡眠状态的乘客被限制在他们的舱房内。Cabin - 乘客所住的舱房号码。格式为deck/num/side其中side可以是P港口或S舷侧。Destination - 乘客将要下船的星球。Age - 乘客的年龄。VIP - 乘客是否支付额外费用获得特殊的VIP服务。RoomService, FoodCourt, ShoppingMall, Spa, VRDeck - 乘客在太空巨轮泰坦尼克号的许多豪华设施上的费用金额。Name - 乘客的名字和姓氏。Transported - 乘客是否被传送到另一个维度。这是目标也就是您要预测的列。test.csv - 剩余三分之一约4300名乘客的个人记录用作测试数据。您的任务是预测这一集合中乘客的Transported值。sample_submission.csv - 一个以正确格式的提交文件。PassengerId - 测试集中每位乘客的标识。Transported - 目标。对于每位乘客预测True或False。 1. 缺失值
print(TRAIN SET MISSING VALUES:)
print(train.isna().sum())
print()
print(TEST SET MISSING VALUES:)
print(test.isna().sum())2. 重复值
print(fDuplicates in train set: {train.duplicated().sum()}, ({np.round(100*train.duplicated().sum()/len(train),1)}%))
print()
print(fDuplicates in test set: {test.duplicated().sum()}, ({np.round(100*test.duplicated().sum()/len(test),1)}%))3. 属性类型分析
train.nunique()4. 类别分析
train.dtypes一般而言连续型属性被存储为float和double类型离散型属性被存储为object和int类型。
6个连续性属性(RoomService,FoodCourt,ShoppingMall,Spa,VRDeck,Age)4个离散属性(HomePlanet,CryoSleep,Destination,VIP)剩余3个描述性/定性属性(PassengerId,Name,Cabin)
5. 分析目标数值占比
# 特征尺寸
plt.figure(figsize(6,6))# 饼状图
train[Transported].value_counts().plot.pie(explode[0.1,0.1], autopct%1.1f%%, shadowTrue, textprops{fontsize:16}).set_title(Target distribution)分布平衡因此不需要采用过采样、欠采样方法。
三属性分析
1. 对年龄Age分析
1直方图分析
直方图关键参数hueTarget属性
# 特征尺寸
plt.figure(figsize(10,4))# 直方图
sns.histplot(datatrain, xAge, hueTransported, binwidth1, kdeTrue)plt.title(Age distribution)
plt.xlabel(Age (years))注意到
0-18岁的青少年更容易被转移。18-25岁的人被转移的可能性比不被转移的可能性要小。25岁以上的人被转移的可能性和没有被转移的可能性差不多。
2创建新属性
根据直方图False与True的高度关系对年龄划分为多层并依次建立一个新特征。
# 新特征-训练集
train[Age_group]np.nan
train.loc[train[Age]12,Age_group]Age_0-12
train.loc[(train[Age]12) (train[Age]18),Age_group]Age_13-17
train.loc[(train[Age]18) (train[Age]25),Age_group]Age_18-25
train.loc[(train[Age]25) (train[Age]30),Age_group]Age_26-30
train.loc[(train[Age]30) (train[Age]50),Age_group]Age_31-50
train.loc[train[Age]50,Age_group]Age_51# 新特征-测试集
test[Age_group]np.nan
test.loc[test[Age]12,Age_group]Age_0-12
test.loc[(test[Age]12) (test[Age]18),Age_group]Age_13-17
test.loc[(test[Age]18) (test[Age]25),Age_group]Age_18-25
test.loc[(test[Age]25) (test[Age]30),Age_group]Age_26-30
test.loc[(test[Age]30) (test[Age]50),Age_group]Age_31-50
test.loc[test[Age]50,Age_group]Age_51# 新特征的分布图
plt.figure(figsize(10,4))
gsns.countplot(datatrain, xAge_group, hueTransported, order[Age_0-12,Age_13-17,Age_18-25,Age_26-30,Age_31-50,Age_51])
plt.title(Age group distribution)2. 对支出属性进行分析
1直方图分析
# 支出特征
exp_feats[RoomService, FoodCourt, ShoppingMall, Spa, VRDeck]# 绘图
figplt.figure(figsize(10,20))
for i, var_name in enumerate(exp_feats):# 左图axfig.add_subplot(5,2,2*i1)sns.histplot(datatrain, xvar_name, axesax, bins30, kdeFalse, hueTransported)ax.set_title(var_name)# 右图核密度估计axfig.add_subplot(5,2,2*i2)sns.histplot(datatrain, xvar_name, axesax, bins30, kdeTrue, hueTransported)plt.ylim([0,100])ax.set_title(var_name)
fig.tight_layout() # 改善外观
plt.show()注意到
大多数人都没有花钱因此可以创建一个二元属性对是否有支出进行划分。有少数的异常值。被运输的人往往花费更少。RoomService、Spa和VRDeck与FoodCourt和ShoppingMall有着不同的分布
2创建新属性
创建一个新属性记录5个支出属性的总开支。再创建一个二元属性对是否有支出进行划分。
# 新特征-训练集
train[Expenditure]train[exp_feats].sum(axis1)
train[No_spending](train[Expenditure]0).astype(int)# 新特征-测试集
test[Expenditure]test[exp_feats].sum(axis1)
test[No_spending](test[Expenditure]0).astype(int)# 新特征的分布图
figplt.figure(figsize(12,4))
plt.subplot(1,2,1)
sns.histplot(datatrain, xExpenditure, hueTransported, bins200)
plt.title(Total expenditure (truncated))
plt.ylim([0,200])
plt.xlim([0,20000])plt.subplot(1,2,2)
sns.countplot(datatrain, xNo_spending, hueTransported)
plt.title(No spending indicator)
fig.tight_layout()3. 对离散属性进行分析
1直方图分析
离散属性——countplot
# 离散特征
cat_feats[HomePlanet, CryoSleep, Destination, VIP]# 绘图
figplt.figure(figsize(10,16))
for i, var_name in enumerate(cat_feats):axfig.add_subplot(4,1,i1)sns.countplot(datatrain, xvar_name, axesax, hueTransported)ax.set_title(var_name)
fig.tight_layout() # 改善外观
plt.show()注意到
VIP似乎不是一个有用的功能对目标分割差不多相等。相比之下CryoSleep似乎是一个非常有用的功能。因此我们可以考虑去掉VIP列以防止过拟合。
4. 对定性属性进行分析
1属性分析
# 定性特征
qual_feats[PassengerId, Cabin ,Name]# 预览定性特征
train[qual_feats].head()注意到
Passengerld采用gggg_pp的形式其中gggg表示乘客所在的旅行团pp表示他们在旅行团中的编号。Cabin的形式为deck/num/side其中side可以是P代表左舷也可以是S代表右舷。
因此我们可以从Passengerld特征中提取群组和群组大小。从Cabin特征中提取甲板、数量和侧面。从Name特征中提取姓氏来识别家庭。
2根据PassengerId划分group
# 新特征-分组
train[Group] train[PassengerId].apply(lambda x: x.split(_)[0]).astype(int)
test[Group] test[PassengerId].apply(lambda x: x.split(_)[0]).astype(int)# 绘图
plt.figure(figsize(20,4))
plt.subplot(1,2,1)
sns.histplot(datatrain, xGroup, hueTransported, binwidth1)
plt.title(Group)发现分组过多不好进行独热编码因此新建一个属性根据分组的人数记录每个分组的数量。
# 新特征-分组大小
train[Group_size]train[Group].map(lambda x: pd.concat([train[Group], test[Group]]).value_counts()[x])
test[Group_size]test[Group].map(lambda x: pd.concat([train[Group], test[Group]]).value_counts()[x])plt.subplot(1,2,2)
sns.countplot(datatrain, xGroup_size, hueTransported)
plt.title(Group size)
fig.tight_layout()发现除了1人组更不容易被传输其他人数的分组基本上都是更容易被传输因此可以将其他人数的分组划分在一起。为此我们可以再新建一个属性来判断该数据是否是1人组是否独自出行。
# 新特征
train[Solo](train[Group_size]1).astype(int)
test[Solo](test[Group_size]1).astype(int)# 新特质分布图
plt.figure(figsize(10,4))
sns.countplot(datatrain, xSolo, hueTransported)
plt.title(Passenger travelling solo or not)
plt.ylim([0,3000])3对Cabin属性进一步划分
# 现在用离群值替换NaN(这样我们就可以拆分特征)
train[Cabin].fillna(Z/9999/Z, inplaceTrue)
test[Cabin].fillna(Z/9999/Z, inplaceTrue)# 新特征-训练集
train[Cabin_deck] train[Cabin].apply(lambda x: x.split(/)[0])
train[Cabin_number] train[Cabin].apply(lambda x: x.split(/)[1]).astype(int)
train[Cabin_side] train[Cabin].apply(lambda x: x.split(/)[2])# 新特征-测试集
test[Cabin_deck] test[Cabin].apply(lambda x: x.split(/)[0])
test[Cabin_number] test[Cabin].apply(lambda x: x.split(/)[1]).astype(int)
test[Cabin_side] test[Cabin].apply(lambda x: x.split(/)[2])# 把Nan的放回去(我们稍后会填充这些)
train.loc[train[Cabin_deck]Z, Cabin_deck]np.nan
train.loc[train[Cabin_number]9999, Cabin_number]np.nan
train.loc[train[Cabin_side]Z, Cabin_side]np.nan
test.loc[test[Cabin_deck]Z, Cabin_deck]np.nan
test.loc[test[Cabin_number]9999, Cabin_number]np.nan
test.loc[test[Cabin_side]Z, Cabin_side]np.nan# 丢弃Cabin属性(我们不再需要它了)
train.drop(Cabin, axis1, inplaceTrue)
test.drop(Cabin, axis1, inplaceTrue)# 新特征的分布图
figplt.figure(figsize(10,12))
plt.subplot(3,1,1)
sns.countplot(datatrain, xCabin_deck, hueTransported, order[A,B,C,D,E,F,G,T])
plt.title(Cabin deck)plt.subplot(3,1,2)
sns.histplot(datatrain, xCabin_number, hueTransported,binwidth20)
plt.vlines(300, ymin0, ymax200, colorblack)
plt.vlines(600, ymin0, ymax200, colorblack)
plt.vlines(900, ymin0, ymax200, colorblack)
plt.vlines(1200, ymin0, ymax200, colorblack)
plt.vlines(1500, ymin0, ymax200, colorblack)
plt.vlines(1800, ymin0, ymax200, colorblack)
plt.title(Cabin number)
plt.xlim([0,2000])plt.subplot(3,1,3)
sns.countplot(datatrain, xCabin_side, hueTransported)
plt.title(Cabin side)
fig.tight_layout()注意到Cabin_number被按照300的数量进行划分每个划分的False与True的高度都不一样。这意味着我们可以压缩这个特征被压缩成一个分类特征其他注意事项:客舱甲板的“T”似乎是一个异常值(只有5个样本)。
# 新特征-训练集
train[Cabin_region1](train[Cabin_number]300).astype(int) # 独热编码
train[Cabin_region2]((train[Cabin_number]300) (train[Cabin_number]600)).astype(int)
train[Cabin_region3]((train[Cabin_number]600) (train[Cabin_number]900)).astype(int)
train[Cabin_region4]((train[Cabin_number]900) (train[Cabin_number]1200)).astype(int)
train[Cabin_region5]((train[Cabin_number]1200) (train[Cabin_number]1500)).astype(int)
train[Cabin_region6]((train[Cabin_number]1500) (train[Cabin_number]1800)).astype(int)
train[Cabin_region7](train[Cabin_number]1800).astype(int)# 新特征-测试集
test[Cabin_region1](test[Cabin_number]300).astype(int) # 独热编码
test[Cabin_region2]((test[Cabin_number]300) (test[Cabin_number]600)).astype(int)
test[Cabin_region3]((test[Cabin_number]600) (test[Cabin_number]900)).astype(int)
test[Cabin_region4]((test[Cabin_number]900) (test[Cabin_number]1200)).astype(int)
test[Cabin_region5]((test[Cabin_number]1200) (test[Cabin_number]1500)).astype(int)
test[Cabin_region6]((test[Cabin_number]1500) (test[Cabin_number]1800)).astype(int)
test[Cabin_region7](test[Cabin_number]1800).astype(int)# 新特征的分布图
plt.figure(figsize(10,4))
train[Cabin_regions_plot](train[Cabin_region1]2*train[Cabin_region2]3*train[Cabin_region3]4*train[Cabin_region4]5*train[Cabin_region5]6*train[Cabin_region6]7*train[Cabin_region7]).astype(int)
sns.countplot(datatrain, xCabin_regions_plot, hueTransported)
plt.title(Cabin regions)
train.drop(Cabin_regions_plot, axis1, inplaceTrue)4根据Name的姓氏划分出家庭组
根据形式划分出家庭组再根据家庭组的大小划分出一个新的二元属性。
# 现在用离群值替换NaN(这样我们就可以拆分特征)
train[Name].fillna(Unknown Unknown, inplaceTrue)
test[Name].fillna(Unknown Unknown, inplaceTrue)# 新特征-姓氏
train[Surname]train[Name].str.split().str[-1]
test[Surname]test[Name].str.split().str[-1]# 新特征-家庭大小
train[Family_size]train[Surname].map(lambda x: pd.concat([train[Surname],test[Surname]]).value_counts()[x])
test[Family_size]test[Surname].map(lambda x: pd.concat([train[Surname],test[Surname]]).value_counts()[x])# 把Nan的放回去(我们稍后会填充这些)
train.loc[train[Surname]Unknown,Surname]np.nan
train.loc[train[Family_size]100,Family_size]np.nan
test.loc[test[Surname]Unknown,Surname]np.nan
test.loc[test[Family_size]100,Family_size]np.nan# 删除Name(我们不再需要它了)
train.drop(Name, axis1, inplaceTrue)
test.drop(Name, axis1, inplaceTrue)# 新特征分布图
plt.figure(figsize(12,4))
sns.countplot(datatrain, xFamily_size, hueTransported)
plt.title(Family size)四归纳总结
数据分析流程总结 五写在最后
至此我们已经完成了对数据集的初步分析下一篇我们会对该数据集进行预处理。
Kaggle竞赛系列_SpaceshipTitanic金牌方案分析_数据处理