阿里金融大数据竞赛数据预处理部分代码

本文详细介绍了阿里金融大数据竞赛中的数据预处理步骤,包括处理end_date中的重复数据,清理无效列,整合宏观数据,以及构建预测数据集和训练集。通过这些操作,为后续的模型建立打下坚实基础。
摘要由CSDN通过智能技术生成

 step1:处理end_date中重复的数据,并清除没有意义的几列

import pandas as pd
import seaborn as sns
from datetime import datetime
import os
import warnings
import math
warnings.filterwarnings("ignore")


mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题



########################第一步 清洗数据######################################################
######1.处理END_DATE重复的数据,并清除表中无用的几列

#读取数据 DF1、DF2、DF3分别为banlanceSheet incomeSheet cashFlowSheet
dfFile = "E:\\阿里云大数据竞赛\\金融算法大赛\\data\\[Add April May] FDDC_financial_data_20180613\\[New] Financial Data_20180613\\Balance Sheet.xls"
pwd = os.getcwd()
os.chdir(os.path.dirname(dfFile))
DF1 = pd.read_excel(os.path.basename(dfFile),sheetname=None) 
dfFile = "E:\\阿里云大数据竞赛\\金融算法大赛\\data\\[Add April May] FDDC_financial_data_20180613\\[New] Financial Data_20180613\\Income Statement.xls"
os.chdir(os.path.dirname(dfFile))
DF2 = pd.read_excel(os.path.basename(dfFile),sheetname=None) 
dfFile = "E:\\阿里云大数据竞赛\\金融算法大赛\\data\\[Add April May] FDDC_financial_data_20180613\\[New] Financial Data_20180613\\Cash Flow Statement.xls"
os.chdir(os.path.dirname(dfFile))
DF3 = pd.read_excel(os.path.basename(dfFile),sheetname=None) 
#sheet=4 ['General Business' 'Insurance' 'Securities' 'Bank']
os.chdir(pwd)

#sheetNames存储四个表的名字['General Business' 'Insurance' 'Securities' 'Bank']
sheetNames=[]
for x in DF1:
    sheetNames.append(x)
    
#函数1:clearEndDate  ########函数用作清除表内对于同一个股票存在的多个END_DATE重复的问题(新的报表修改老的报表的数据),保留最新的报表数据
def clearEndDate(dataframe,columnNames):   #columnNames[1] 对应股票代码 columnNames[5]对应END_DATE
    rows=dataframe[columnNames[1]].size
    temp=dataframe.groupby(columnNames[1])[columnNames[5]].apply(lambda x:x.sort_values(ascending=True)).reset_index()
    #如果不存在重复的END_DATE,temp这个dataframe对象中不会出现level_1这个列
    if 'level_1' not in temp.columns:          
        dataframe.sort_index(by=[columnNames[1]],ascending=True,inplace=True)
        dataframe.reset_index(drop=True,inplace=True)
        return dataframe
    
    #如果存在重复的END_DATE,temp这个dataframe对象中会出现level_1这个列,level_1为原表里对应行的index
    ind=list(temp['level_1'])
    temp=dataframe.loc[ind]
    temp.reset_index(drop=True,inplace=True)
    dropIndex=[]  #存储要被删除的数据行的index
    for i in range(1,rows):
        if(temp[columnNames[1]][i-1]==temp[columnNames[1]][i] and temp[columnNames[5]][i-1]==temp[columnNames[5]][i]):    #如果两行数据的ID和截止日期相同,保留最后一行的数据
            dropIndex.append(i-1)
    temp.drop(dropIndex,axis=0,inplace=True)
    temp.reset_index(drop=True,inplace=True)
    return temp


for x in sheetNames:
    df_balance=DF1[x]    
    df_income=DF2[x]   
    df_cashflow=DF3[x]
    
    columnNames=list(df_balance.columns)   #经测试,这3*4个表的前八个列名都是相同的
    deleteCol1=[0,2,3,4,6,8]               #banlance表无用的列,保留股票代码,END_DATE,会计区间的信息 
    deleteCol2=[0,2,3,4,6,8]               #cashflow表无用的列,和banlance表一样
    saveCols3=['TICKER_SYMBOL','END_DATE','FISCAL_PERIOD','REVENUE']  #income表的保留项,分别为股票代码,截止日期,会计区间,营业收入
    
    #对三张表进行清洗(调用函数1)
    df_balance=clearEndDate(df_balance,columnNames)       
    df_cashflow=clearEndDate(df_cashflow,columnNames)
    df_income=df_income[saveCols3]
    df_income=clearEndDate(df_income,columnNames)
    
    #对balance表和cashflow表中包含无用信息的列进行删除
    df_balance.drop(df_balance.columns[deleteCol1],axis=1,inplace=True)
    df_cashflow.drop(df_cashflow.columns[deleteCol1],axis=1,inplace=True)
    
    #将缺失值填0
    df_balance.fillna(0,inplace=True)
    
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AliDMCompetition 阿里巴巴大数据竞赛(http://102.alibaba.com/competition/addDiscovery/index.htm ) 数据说明 提供的原始文件有大约4M左右,涉及1千多天猫用户,几千个天猫品牌,总共10万多条的行为记录。 用户4种行为类型(Type)对应代码分别为: 点击:0 购买:1 收藏:2 购物车:3 提交格式 参赛者将预测的用户存入文本文件中,格式如下: user_id \t brand_id , brand_id , brand_id \n 上传的结果文件名字不限(20字以内),文件必须为txt格式。 预测结果 真实购买记录一共有3526条 TODO 注意调整正负样本比例 在LR的基础上做RawLR。按照天猫内部的思路来。 在LR的基础上做MRLR,样本提取要更加合理。 在UserCF和ItemCF上加上时间因子的影响。 利用UserCF做好的用户聚类、ItemCF做好的品牌聚类来做细化的LR,或者在聚类 上做LFM 在ItemCF的思路上挖掘频繁项集/购买模式,如购买品牌A和商品后往往会购买 品牌B的商品 LFM 数据集特征 某一商品在购买前的一段时间内会出现大量点击次数,购买完成后的一段时间内也会出现大量点击次数 用户在本月有过行为的商品极少出现在下个月的购买列表里 根据观察推断:用户浏览商品的行为可分为两类: 无目的浏览,可能会在浏览过程中对某些中意的商品进行购买,数据表现为有大量点击次数<=2的行为记录,但很少有购买行为 有目的的查找商品,可能是事先有需求的情况,数据表现为一段时间内点击商品数很少, 但点击过的商品大多数都进行了购买 参考论文 See https://www.google.com.hk/search?q=data+mining+time+series&ie=utf-8&oe=utf-8&aq=t for more. Chapter 1 MINING TIME SERIES DATA - ResearchGate 模型列表 LR(model=LinearSVC(C=10, loss='l1'), alpha=0.7, degree=1) | TOTAL VISITED BOUGHT FAVO CART NEW | Pred # 1438 1436 626 71 12 | % 100% 99.861% 43.533% 4.937% 0.834% | Real # 1311 250 89 10 1 | % 100% 19.069% 6.789% 0.763% 0.076% Hit # 76 Precision 5.285118% Recall 5.797101% F1 Score 5.529283% LR(model=LogisticRegression(penalty='l1'), alpha=0.7, degree=1) | TOTAL VISITED BOUGHT FAVO CART NEW | Pred # 1472 1470 615 68 14 | % 100% 99.864% 41.780% 4.620% 0.951% | Real # 1311 250 89 10 1 | % 100% 19.069% 6.789% 0.763% 0.076% Hit # 74 Precision 5.027174% Recall 5.644546% F1 Score 5.318002% 这个模型在数据变成2次后,Precision ~ 16%,同时F1 ~ 3% LR(model=Perceptron(penalty='l1'), alpha=0.7, degree=1) | TOTAL VISITED BOUGHT FAVO CART NEW | Pred # 3145 3140 1023 130 26 | % 100% 99.841% 32.528% 4.134% 0.827% | Real # 1311 250 89 10 1 | % 100% 19.069% 6.789% 0.763% 0.076% Hit # 113 Precis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值