非平衡數(shù)據(jù)的處理方法

更多大數(shù)據(jù)分析、建模等內(nèi)容請關(guān)注公眾號(hào)《bigdatamodeling》

在松滋等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需搭建網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站制作,松滋網(wǎng)站建設(shè)費(fèi)用合理。

在分類問題中常常遇到一個(gè)比較頭疼的問題,即目標(biāo)變量的類別存在較大偏差的非平衡問題。這樣會(huì)導(dǎo)致預(yù)測結(jié)果偏向多類別,因?yàn)槎囝悇e在損失函數(shù)中所占權(quán)重更大,偏向多類別可以使損失函數(shù)更小。

處理非平衡問題一般有兩種方法,欠抽樣和過抽樣。欠抽樣方法可以生成更簡潔的平衡數(shù)據(jù)集,并減少了學(xué)習(xí)成本。但是它也帶來了一些問題,它會(huì)刪掉一些有用的樣本,尤其當(dāng)非平衡比例較大時(shí),刪掉更多的樣本會(huì)導(dǎo)致原始數(shù)據(jù)的分布嚴(yán)重扭曲,進(jìn)而影響分類器的泛化能力。

因此,后來發(fā)展出了過抽樣方法,它不會(huì)刪除多類別的樣本,而是通過復(fù)制少類別樣本的方法處理非平衡問題。但是,應(yīng)用隨機(jī)過抽樣方法復(fù)制少類別樣本意味著對少類別樣本賦予更高的權(quán)重,容易產(chǎn)生過擬合問題。

2002年,研究者提出了SMOTE(Synthetic Minority Oversampling Technique)方法來替代標(biāo)準(zhǔn)的隨機(jī)過抽樣方法,可以一定程度克服隨機(jī)過抽樣帶來的過擬合問題,提高分類器的泛化能力。SMOTE方法通過在少類別樣本的近鄰中應(yīng)用插值法創(chuàng)造新的少類別樣本,而不再是簡單的復(fù)制或賦予權(quán)重。

SMOTE算法的步驟如下:
(1)選取第i個(gè)少類別樣本在所有少類別樣本中的K個(gè)近鄰
(2)從K個(gè)近鄰中隨機(jī)選擇N個(gè)樣本,和第i個(gè)樣本通過插值法獲取N個(gè)新樣本
(3)重復(fù)步驟(1)和(2)直到所有少類別樣本被遍歷一遍
見下圖:
非平衡數(shù)據(jù)的處理方法

SMOTE算法的偽代碼:

#============================== SMOTE算法偽代碼 ===============================#
Algorithm 1 SMOTE algorithm
1: function SMOTE(T, N, k)
  Input: T; N; k # T:Number of minority class examples 
                 # N:Amount of oversampling 
                 # K:Number of nearest neighbors 
  Output: (N/100) * T  # synthetic minority class samples
  Variables: Sample[][] # array for original minority class samples;
             newindex # keeps a count of number of synthetic samples generated, initialized to 0;
             Synthetic[][] # array for synthetic samples
2:    if N < 100 then
3:        Randomize the T minority class samples
4:        T = (N/100)*T
5:        N = 100
6:    end if
7:    N = (int)N/100 # The amount of SMOTE is assumed to be in integral multiples of 100.
8:    for i = 1 to T do
9:        Compute k nearest neighbors for i, and save the indices in the nnarray
10:       POPULATE(N, i, nnarray)
11:   end for
12: end function

Algorithm 2 Function to generate synthetic samples
1: function POPULATE(N, i, nnarray)
  Input: N; i; nnarray  # N:instances to create
                        # i:original sample index
                        # nnarray:array of nearest neighbors
  Output: N new synthetic samples in Synthetic array
2:     while N != 0 do
3:         nn = random(1,k)
4:         for attr = 1 to numattrs do # numattrs:Number of attributes
5:             Compute: dif = Sample[nnarray[nn]][attr] ? Sample[i][attr]
6:             Compute: gap = random(0, 1)
7:             Synthetic[newindex][attr] = Sample[i][attr] + gap * dif
8:         end for
9:         newindex + +
10:        N ? ?
11:    end while
12: end function

SMOTE算法的python實(shí)現(xiàn):
下面用python實(shí)現(xiàn)一個(gè)SMOTE的最簡單的版本:

####### python3.6 ########
import random
import numpy as np
from sklearn.neighbors import NearestNeighbors

def smote_sampling(samples, N=100, K=5):
   n_samples, n_attrs = samples.shape

   if N<100:
       n_samples = int(N/100*n_samples)
       indx = random.sample(range(len(samples)), n_samples)
       samples = samples[indx]
       N = 100

   N = int(N/100)
   synthetic = np.zeros((n_samples * N, n_attrs))
   newindex = 0
   neighbors=NearestNeighbors(n_neighbors=K+1).fit(samples)

   for i in range(n_samples):
       nnarray=neighbors.kneighbors(samples[i].reshape(1,-1),return_distance=False)[0]
       for j in range(N):
           nn = random.randint(1, K)
           dif = samples[nnarray[nn]]- samples[i]
           gap = random.random()
           synthetic[newindex] = samples[i] + gap * dif
           newindex += 1

   return synthetic

python中imblearn模塊對SMOTE算法的封裝
python 中有封裝好的SMOTE方法,在實(shí)踐中可以直接調(diào)用該方法,具體參數(shù)可以查看官方文檔。簡單應(yīng)用舉例:

from sklearn.datasets import make_classification
from collections import Counter
from imblearn.over_sampling import SMOTE

# 生成數(shù)據(jù)集
X, y = make_classification(n_classes=2, class_sep=2,
                          weights=[0.1, 0.9], n_informative=3, 
                          n_redundant=1, flip_y=0,
                          n_features=20, n_clusters_per_class=1, 
                          n_samples=1000, random_state=9)
# 類別比例
Counter(y)

# SMOTE合成新數(shù)據(jù)
sm = SMOTE(ratio={0: 900}, random_state=1)
X_res, y_res = sm.fit_sample(X, y)

# 新數(shù)據(jù)類別比例
Counter(y_res)

網(wǎng)站欄目:非平衡數(shù)據(jù)的處理方法
網(wǎng)站網(wǎng)址:http://bm7419.com/article44/gijdhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站制作網(wǎng)站設(shè)計(jì)公司、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)