zoukankan      html  css  js  c++  java
  • Python数据分析------例子1(信用卡欺诈)

    1、读取数据

    data=read_csv(path)
    data.head()
    
    #画图(查看class即分类的数据条形图),函数sort_index()是将dataframe按照行索引来排序输出值
    count_classes=pd.value_counts(data['Class'],sort=True).sort_index()
    count_classes.plot(kind='bar')
    plt.title("*****")
    plt.xlable("class")
    plt.ylable("Frequency")

    2、认识数据

    ①数据特征的量纲差距(即归一化/标准化处理)

    from sklearn.processing import StanarScaler
    
    #将特征'Amount'归一化成新的特征'normAmount',这里的reshape函数是将dataframe转化成array,fit_transform的参数需要是数组。
    #fit_transform()是将数据fit再transform,主要作用是将数据标准化成均值为0,方差为1的数,范围在【-1,1】之间。
    data['normAmount']=StandarScaler().fit_transform(data['Amount'].reshape(-1,1)) #将没有用的数据特征删除 data=data.drop('Amount','time')

    ②数据分布不均衡(比方说分类,0-1分类,0的数据远远大于1的数据)

    处理方式:下采样、过采样

    下采样:将多的数据变得和少的数据一样少。

    过采样:将少的数据变得和多的数据一样多。

    以下是下采样:

     
    #下采样
    #将dataframe分为X和Y,其中不等于Class属性的为X,将属性值Class作为Y
    X=data.ix[:,data.columns!='Class']
    y=data.ix[:,data.columns=='Class']
    
    #少的数据数量确定为number_fraud,也就是随机采样的数量。
    number_fraud = len(data[data.Class == 1])
    #少的数据的索引,转化成array形式,这样方便多的数据采样后的索引进行合并。用函数np.concatenate
    fraud_indices = np.array(data[data.Class == 1].index)
    
    #多的数据索引
    normal_indices = data[data.Class == 0].index
    
    #random中的choice函数,第一个参数就是要采样的数据,第二个参数是采样量,第三个是没有重复替换的数据
    random_normal_indices = np.random.choice(normal_indices, number_records_fraud, replace = False)
    random_normal_indices = np.array(random_normal_indices)
    
    #合并采样后的多数据和少的数据的索引
    under_sample_indices = np.concatenate([fraud_indices,random_normal_indices])
    
    #根据合并后的索引来取数据
    under_sample_data = data.iloc[under_sample_indices,:]
    
    X_undersample = under_sample_data.ix[:, under_sample_data.columns != 'Class']
    y_undersample = under_sample_data.ix[:, under_sample_data.columns == 'Class']

     过采样:SMOTE算法、ADASYN算法

    SMOTE:对于少数类样本a,随机选择一个最近邻的样本b,然后从a和b的连线上随机选取一个点c作为新的少数类样本。

    计算步骤:

    (1)对于少数类中每一个样本x,以欧氏距离为标准计算它到少数类样本集中所有样本的距离,得到其k近邻。

    (2)根据样本不平衡比例设置一个采样比例以确定采样倍率N,对于每一个少数类样本x,从其k近邻中随机选择若干个样本,假设选择的近邻为xn。

    (3)对于每一个随机选出的近邻xn,分别与原样本按照如下的公式构建新的样本。

    X(new)=x+rand(0,1)×(^x-x) 【^x为均值】

    比如:我要将少数类样本数量增大5倍,则N=5,选出x的5近邻,计算它的均值^x,再计算其与x的距离取随机值。

    代码如下:

    #引入不平衡类模块的上采样
    from imblearn.over_sampling import SMOTE
    
    #
    oversampler=SMOTE(random_state=0)
    X,Y=oversampler.fit_sample(X_train,Y_train)

    ADASYN:关注的是在那些基于K最近邻分类器被错误分类的原始样本附近生成新的少数类样本

    ③缺失值、异常值

    3、预处理

    ①交叉验证:切分训练集合测试集。

    from sklearn.cross_validation import train_test_split
    
    #整个数据集的切分【作为后面预测的时候用的数据】
    X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.3, random_state = 0)
    
    # 下采样数据集的切分
    X_train_undersample, X_test_undersample, y_train_undersample, y_test_undersample = train_test_split(X_undersample
                                                                                                       ,y_undersample
                                                                                                       ,test_size = 0.3
                                                                                                       ,random_state = 0)

    4、模型评估(如用召回率来评估)recall=TP/(TP+FN)

  • 相关阅读:
    搭建CentOS在线yum源镜像服务器
    zabbix-使用orabbix来监控oracle11g
    zabbix监控系统-1:系统搭建
    ELK-学习-1:elasticsearch6.3安装和配置
    zabbix-使用percona mysql插件来监控mysql
    zabbix学习基础篇-4:用户群组和用户
    相关性分析
    马尔可夫预测
    该工程中的宏被禁止,请参阅联机帮助
    matlab工具包
  • 原文地址:https://www.cnblogs.com/Lee-yl/p/9126866.html
Copyright © 2011-2022 走看看