美文网首页
ti'tanic记录

ti'tanic记录

作者: 小喵周周 | 来源:发表于2018-01-04 07:51 被阅读0次

    目的是预测test中的survive情况,下面对比小姐姐和小哥哥的。
    小姐姐:
    ①读取并宏观看数据

    data = pd.read_csv('../data/train.csv')
    data.head()
    data['Age'].fillna(data['Age'].median(), inplace=True)#有缺失age
    data.describe()
    

    ②画各种图找到数据间关系

    #第一种是分类已确定不用自己选区间的例如sex,用dataframe可以生成表结构(表头+内容+索引)
    survived_sex = data[data['Survived']==1]['Sex'].value_counts()
    dead_sex = data[data['Survived']==0]['Sex'].value_counts()
    df = pd.DataFrame([survived_sex,dead_sex])
    df.index = ['Survived','Dead']
    df.plot(kind='bar',stacked=True, figsize=(13,8))
    
    #第二种是需要自己选区间的如age,这里我们选30个
    figure = plt.figure(figsize=(13,8))
    plt.hist([data[data['Survived']==1]['Age'],data[data['Survived']==0]['Age']], stacked=True, color = ['g','r'],
             bins = 30,label = ['Survived','Dead'])
    plt.xlabel('Age')
    plt.ylabel('Number of passengers')
    plt.legend()
    
    #第三种做散点图看两个因素结合时的分布
    plt.figure(figsize=(13,8))
    ax = plt.subplot()
    ax.scatter(data[data['Survived']==1]['Age'],data[data['Survived']==1]['Fare'],c='green',s=40)
    ax.scatter(data[data['Survived']==0]['Age'],data[data['Survived']==0]['Fare'],c='red',s=40)
    ax.set_xlabel('Age')
    ax.set_ylabel('Fare')
    ax.legend(('survived','dead'),scatterpoints=1,loc='upper right',fontsize=15,)
    

    今天勉强运行通并得到了结果,遇到了两个Python版本的坑:sort vs sort_values和filter函数需要加List才能有length的问题。

    下一步需要研究各种向量机等,研究提到的sklearn库,分类方法/随机森林等。

    相关文章

      网友评论

          本文标题:ti'tanic记录

          本文链接:https://www.haomeiwen.com/subject/rqulnxtx.html