99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀缺失數(shù)據(jù)處理-插值法
缺失數(shù)據(jù)處理-插值法
2017-12-04
收藏

缺失數(shù)據(jù)處理-插值法

數(shù)據(jù)挖掘中,原始海量的數(shù)據(jù)中存在著大量不完整、不一致、有異常、偏離點(diǎn)的數(shù)據(jù)。這些問題數(shù)據(jù)輕則影響數(shù)據(jù)挖掘執(zhí)行效率,重則影響執(zhí)行結(jié)果。因此數(shù)據(jù)預(yù)處理工作必不可少,而其中常見工作的就是數(shù)據(jù)集的缺失值處理。

數(shù)據(jù)缺失值處理可分兩類。一類是刪除缺失數(shù)據(jù),一類是進(jìn)行數(shù)據(jù)插補(bǔ)。前者比較簡單粗暴,但是這種方法最大的局限就是它是以減少歷史數(shù)據(jù)來換取數(shù)據(jù)的完備,會造成資源的大量浪費(fèi),尤其在數(shù)據(jù)集本身就少的情況下,刪除記錄可能會直接影響分析結(jié)果的客觀性和準(zhǔn)確性

本文介紹數(shù)據(jù)常用的插補(bǔ)方法。對拉格朗日插值法和滑動平均窗口法進(jìn)行重點(diǎn)介紹和實現(xiàn)。

介紹

常用的插值方法如下:


這里只對插值法和窗口滑動平均進(jìn)行介紹。
滑動平均窗口法
概念:
??一個列表a 中的第 i 個位置數(shù)據(jù)為缺失數(shù)據(jù),則取前后 window 個數(shù)據(jù)的平均值,作為插補(bǔ)數(shù)據(jù)。
示例:

a = [3,4,5,6,None,4,5,2,5] 、 window = 3
則 None位置的數(shù)據(jù)為:(4+5+6+4+5+2)/6 = 2.67
拉格朗日插值法
概念

??根據(jù)數(shù)學(xué)概念可知,對于平面上已知的n個點(diǎn)(無兩點(diǎn)在一條直線上)可以找到一個n-1次的多項式,使此多項式通過這n個點(diǎn)。

??因此我們需先求得多項式函數(shù)L(x),然后將缺失值對應(yīng)的點(diǎn)x帶入插值多項式得到缺失值的近似值L(x)。多項式函數(shù)L(x)的求法如下:

實現(xiàn)
代碼

# coding:utf-8
# 拉格朗日插值代碼
import pandas as pd  # 導(dǎo)入數(shù)據(jù)分析庫Pandas
from scipy.interpolate import lagrange  # 導(dǎo)入拉格朗日插值函數(shù)

# 構(gòu)建原始數(shù)據(jù)
data = pd.DataFrame([
    ["2015/3/1", 59],
    ["2015/2/28", 2618.2],
    ["2015/2/27", 2608.4],
    ["2015/2/26", 2651.9],
    ["2015/2/25", 3442.1],
    ["2015/2/24", 3393.1],
    ["2015/2/23", 3136.6],
    ["2015/2/22", 3744.1],
    ["2015/2/21", ],
    ["2015/2/20", 4060.3],
    ["2015/2/19", 3614.7],
    ["2015/2/18", 3295.5],
    ["2015/2/16", 2332.1],
    ["2015/2/15", 2699.3],
    ["2015/2/14", ],
    ["2015/2/13", 3036.8],
    ["2015/2/12", 1865],
    ["2015/2/11", 3014.3],
    ["2015/2/10", 2742.8],
    ["2015/2/9", 2173.5],
    ["2015/2/8", 3161.8],
    ["2015/2/7", 3023.8],
    ["2015/2/6", 2998.1],
], columns=[u'日期', u'銷量'])

# 設(shè)置異常值,把銷量大于5000和銷量小于400的異常值替換為None
data[u'銷量'][(data[u'銷量'] < 400) | (data[u'銷量'] > 5000)] = None

# 把要處理的數(shù)據(jù)取出來,pandas中dataframe格式單獨(dú)取出一列就是series數(shù)據(jù)格式
tmp_data_1 = data[u'銷量'].copy()
tmp_data_2 = data[u'銷量'].copy()


def ployinterp_column(series, pos, window=5):
    """
    :param series: 列向量
    :param pos: 被插值的位置
    :param window: 為取前后的數(shù)據(jù)個數(shù)
    :return:
    """
    y = series[list(range(pos - window, pos)) + list(range(pos + 1, pos + 1 + window))]  # 取數(shù)
    y = y[y.notnull()]  # 剔除空值
    return lagrange(y.index, list(y))(pos)  # 插值并返回插值結(jié)果


def sma_mothod(series, pos, window=5):
    """
    :param series: 列向量
    :param pos: 被插值的位置
    :param window: 為取前后的數(shù)據(jù)個數(shù)
    :return:
    """
    y = series[list(range(pos - window, pos)) + list(range(pos + 1, pos + 1 + window))]  # 取數(shù)
    y = y[y.notnull()]
    return reduce(lambda a, b: a + b, y) / len(y)

for j in range(len(tmp_data_1)):
    if (tmp_data_1.isnull())[j]:  # 如果為空即插值。
        tmp_data_1[j] = ployinterp_column(tmp_data_1, j)
        print j, data.loc[j, u'日期'], tmp_data_1[j]

print

for j in range(len(tmp_data_2)):
    if (tmp_data_2.isnull())[j]:  # 如果為空即插值。
        tmp_data_2[j] = sma_mothod(tmp_data_2, j)
        print j, data.loc[j, u'日期'], tmp_data_2[j]
輸出

0 2015/3/1 -291.4
8 2015/2/21 4275.25476248
14 2015/2/14 3680.66999227

0 2015/3/1 2942.74
8 2015/2/21 3236.97
14 2015/2/14 2883.43
分析
對比之下,滑動窗口方法的輸出都還比較合理。但顯而易見的是拉格朗日插值對0位置的數(shù)據(jù)處理的很不好,插值為 -291.4。擬合點(diǎn)的數(shù)據(jù)格式為(x,y),具體數(shù)據(jù):(1, 2618.2), (2, 2608.4),(3, 2651.9),(4, 3442.1), (5, 3393.1)。我們把拉格朗日多項式打印出來:
??L(x) = -94.97 x^4 + 1065 x^3 - 3991 x^2 + 5930 x^1 - 291.4
??把 x= 0 帶入得到 L(x),就得到了 -291.4。這里x=0就是L(x)的截距。直觀感覺就不太合理,猜測就是拉格朗日插值法對邊緣數(shù)據(jù)敏感(即插值需要左右兩邊數(shù)據(jù)提供信息,在缺失左邊數(shù)據(jù)信息情況下,得到的結(jié)果就不太合理),日后求證!


數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }