kaggle竞赛实战1

我们最终的目标是要打比赛、进大厂,因此在熟悉了基本模型后先来看看比赛怎么做的,本文以Elo Merchant Category Recommendation | Kaggle 为样例进行介绍。

首先注意使用kaggle要全程“科学上网”,否则在注册、下载数据等环节都会出现页面挑不出来的情况

这一部分讲数据的缺失值、异常值及样本数据一致性情况探索,具体代码如下:


# In[1]:


import os
import numpy as np
import pandas as pd


# In[11]:


pd.read_excel('d:/Data_Dictionary.xlsx',header=2,sheet_name='train')#读取数据,去掉头两行(空行),先看看大概数据情况


# In[12]:


import gc #进行内存管理的


# In[22]:


train=pd.read_csv('d:/train.csv')


# In[23]:


test=pd.read_csv('d:/test.csv')


# In[24]:


#数据质量分析,判断训练和验证集是否取自同一总体,从而决定是用特征工程还是trick,如果分布不一致,则在训练集上容易过拟合
#先看数据集是否cardid独一无二
train['card_id'].nunique()==train.shape[0]#nunique用于看不同id个数


# In[25]:


test['card_id'].nunique()==test.shape[0]


# In[39]:


train['card_id'].nunique()+test['card_id'].nunique()==len(set(train['card_id']).union(set(test['card_id'])))#判断


# In[40]:


train.isnull().sum()#看缺失值情况


# In[41]:


test.isnull().sum()


# In[51]:


statistics=train['target'].describe()#看统计情况,找异常值


# In[43]:


statics


# In[44]:


#连续变量用概率直方图来观察
import seaborn as sns


# In[45]:


import matplotlib.pyplot as plt


# In[46]:


sns.set()


# In[48]:


sns.histplot(train['target'])#绘制密度曲线,找异常值


# In[49]:


#看下异常值数量,可能是特殊用户的标记,不能直接删掉
(train['target']<-30).sum()


# In[52]:


#关于如何确定异常值,也可以用3倍方差准则
statistics.loc['mean']-3*statistics.loc['std']


# In[54]:


#规律一致性分析:两个集合分布规律是否一致
#先单变量分析,看每个变量在每个区间内的样本数分布图是否一致
features=['first_active_month','feature_1','feature_2','feature_3']
train_count=train.shape[0]
test_count=test.shape[0]


# In[56]:


for feature in features:
    (train[feature].value_counts().sort_index()/train_count).plot()
    (test[feature].value_counts().sort_index()/test_count).plot()
    plt.legend(['train','test'])#画标签
    plt.xlabel(feature)
    plt.ylabel('ratio')
    plt.show()


 

  • 27
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值