“东证期货杯”全国大学生统计建模大赛选题2:互联网金融时代下信用评分体系模型的构建源码(附题目,数据)

本文介绍了一次使用ID3算法进行数据挖掘的实战过程,包括数据预处理、构建决策树及分类预测等内容,并提供了完整的Python实现代码。
部署运行你感兴趣的模型镜像

本文为第一次数据挖掘实战,数据预处理部分较乱,源码略去,直接给出处理后数据,所用算法为ID3

# -*- coding: utf-8 -*-
"""
Created on Sun Mar  4 17:08:06 2018

@author: CSH
"""

import math
import operator
from numpy import*
import pandas as pd
import csv

def calcShannonEnt(dataSet):
    numEntries=len(dataSet)
    labelCounts={}
    for featVec in dataSet:
        currentLabel=featVec[-1]
#        labelCounts[currentLabel]=labelCounts.get(currentLabel,0)+1
        if currentLabel not in labelCounts.keys():
            labelCounts[currentLabel]=0
        labelCounts[currentLabel]+=1
    shannonEnt=0.0
    for key in labelCounts:
        prob=float(labelCounts[key])/numEntries
        shannonEnt-=prob*math.log(prob,2)
    return shannonEnt

# 读取csv文件方式1
csvFile = open("qudiaonanrowTrain1.csv", "r")
reader = csv.reader(csvFile)  # 返回的是迭代类型
myDat = []
for item in reader:
#    print(item)
    myDat.append(item[1:])
del(myDat[0]) 
#print(data)
csvFile.close()
#myDat=pd.read_csv('qudiaonanrowTrain1.tsv',encoding='ANSI',sep='\t')
mylabels=['AGENT','IS_LOCAL','VORK_PROVINC','EDU_LEVEL','MARRY_STATUS','SALARY','HAS_FUND']




def splitDataSet(dataSet,axis,value):
    retDataSet=[]
    for featVec in dataSet:
        if featVec[axis]==value:
            reducedFeatVec=featVec[:axis]
            reducedFeatVec.extend(featVec[axis+1:])
            retDataSet.append(reducedFeatVec)
    return retDataSet
#a=splitDataSet(myDat,0,1)
#print(a)

def chooseBestFeatureToSplit(dataSet):
    numFeatures=len(dataSet[0])-1
    baseEntropy=calcShannonEnt(dataSet)
    bestInfoGain=0.0;bestFeature=-1
    for i in range(numFeatures):
        featList=[example[i] for example in dataSet]
        uniqueVals=set(featList)
        newEntropy=0.0
        for value in uniqueVals:
            subDataSet=splitDataSet(dataSet,i,value)
            prob=len(subDataSet)/float(len(dataSet))
            newEntropy+=prob*calcShannonEnt(subDataSet)
        infoGain=baseEntropy-newEntropy
        if (infoGain>bestInfoGain):
            bestInfoGain=infoGain
            bestFeature=i
    return bestFeature
#a=chooseBestFeatureToSplit(myDat)
#print(a)
    
def majorityCnt(classList):
    classCount={}
    for vote in classList:
        if vote not in classCount.keys():classCount[vote]=0
        classCount[vote]+=1
    sortedClassCount=sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)
    return sortedClassCount[0][0]

def createTree(dataSet,labels):
    classList=[example[-1] for example in dataSet]
    if classList.count(classList[0])==len(classList):
        return classList[0]
    if len(dataSet[0])==1:
        return majorityCnt(classList)
    bestFeat=chooseBestFeatureToSplit(dataSet)
    bestFeatLabel=labels[bestFeat]
    myTree={bestFeatLabel:{}}
    del(labels[bestFeat])
    featValues=[example[bestFeat] for example in dataSet]
    uniqueVals=set(featValues)
    for value in uniqueVals:
        subLabels=labels[:]
        myTree[bestFeatLabel][value]=createTree(splitDataSet(dataSet,bestFeat,value),subLabels)
    return myTree
myTree=createTree(myDat[:5000],mylabels)
#print(myTree)
#classLabel=-1   
def classify(inputTree,featLabels,testVec):
#    global classLabel 
    firstSides=list(inputTree.keys())
    firstStr=firstSides[0]
    secondDict=inputTree[firstStr]
    featIndex=featLabels.index(firstStr)
    for key in secondDict.keys():
        if testVec[featIndex]==key:
            if type(secondDict[key]).__name__=='dict':                
                classLabel=classify(secondDict[key],featLabels,testVec)
            else:
                classLabel=secondDict[key]
#                print(classLabel)
    return classLabel
#labels=createDataSet()[1]

myLabels=['AGENT','IS_LOCAL','VORK_PROVINC','EDU_LEVEL','MARRY_STATUS','SALARY','HAS_FUND']
#a=classify(myTree,myLabels,['APP','本地籍','320000.0','本科','已婚','3.0','0.0'])
#a=classify(myTree,myLabels,['wechat', '本地籍', '420000.0', '专科及以下', '未婚', '4.0', '1.0'])
#print(a)
    
# =============================================================================
# def storeTree(inputTree,filename):
#     import pickle
#     fw=open(filename,'wb')
#     pickle.dump(inputTree,fw)
#     fw.close()
# def grabTree(filename):
#     import pickle
#     fr=open(filename,'rb')
#     return pickle.load(fr)
# 
# storeTree(myTree,'classifierStorage.txt')
# b=grabTree('classifierStorage.txt')
# #print(b)
# =============================================================================



#del(myDat[2])
def testing(myTree,data_test,labels):  
    error=0.0;a=0
    testCount=shape(data_test)[0]
    for i in range(len(data_test)): 
        try:
            if classify(myTree,labels,data_test[i])!=data_test[i][-1]:  
                error+=1.0 
        except:
            a+=1
            continue
    print('errorRate %d' %float(error/testCount),a)  
    return float(error)


# =============================================================================
# # 读取csv文件方式2
# with open("mostnan.csv", "r") as csvfile:
#     reader2 = csv.reader(csvfile) # 读取csv文件,返回的是迭代类型
#     data = []
#     for item2 in reader2:
# #        print(item2)
#         data.append(item[1:])
#     del(data[0])
# csvFile.close()
# 
# =============================================================================
testing(myTree,myDat[5000:],myLabels)    

附:链接:https://pan.baidu.com/s/1LkwdWC67uBhB1kuWCikF-Q 密码:xbm3

       相关信用评分模型博客:https://blog.youkuaiyun.com/lll1528238733/article/category/7072659


您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

金融数据论文选题全文共2页,当前为第1页。金融数据论文选题全文共2页,当前为第1页。1、央企金控视角下全面风险管理水平提升探索实践研究 金融数据论文选题全文共2页,当前为第1页。 金融数据论文选题全文共2页,当前为第1页。 2、大数据视角下数字金融信用风险评估与防范 3、中国农村金融发展路径研究 4、浅析互联网金融面临的风险 5、广西绿色金融发展制度保障的重要性分析 6、大数据环境下的互联网金融风险管理策略 7、基于金融功能视角的绿色金融发展探究 8、实体经济发展中利用金融资产的策略分析 9、试析大数据金融发展的有效策略 10、互联网金融背景下企业创新和优化资本运营的策略 11、商贸流通企业供应链金融发展的主要举措 12、试析辽宁省数字普惠金融的发展建议 13、经济新常态下推动金融经济发展的措施 14、互联网金融信用风险的规避与监管对策 15、互联网金融行业信用风险问题的解决方法 16、我国农村合作金融机构法人治理的现状及异化 17、防范互联网金融风险的对策建议 18、我国地方金融监管现状及面临的主要问题 19、青岛市金融创新发展的路径研究 20、居民金融素养对家庭创业收入的影响 21、金融工程对我国金融发展的促进分析 22统计学在金融领域的应用分析 23、河南省金融与经济耦合协调发展研究 金融数据论文选题全文共2页,当前为第2页。金融数据论文选题全文共2页,当前为第2页。24、云南省金融发展与产业结构优化研究 金融数据论文选题全文共2页,当前为第2页。 金融数据论文选题全文共2页,当前为第2页。 25、解析金融科技在农村地区发展的有效对策 26、试析互联网金融背景下中小企业融资策略 27、探析互联网金融税收风险的防范对策 28、基于大数据金融行业发展的建议 29、新时期金融投资中的风险应对策略 30、衍生金融工具套期保值策略分析 31、关于小微金融业务中心信贷风险管理的分析 32、企业金融风险管理中金融工程的运用 33、互联网消费金融发展存在问题的对策分析 34、促进与增加我国农村金融服务供给的对策 35、新形势下如何完善金融管理与服务策略 36、国有企业融资过程中存在的风险研究 37、农村普惠金融背景下产业集聚助力乡村振兴的发展对策 38、浅析唐德影视股权质押融资风险防范措施 39、大数据技术时代互联网金融的风险防控方法 40、新时期金融科技创新审慎监管路径探讨 金融数据论文选题
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值