機(jī)器學(xué)習(xí)之Python數(shù)據(jù)缺失處理

注:數(shù)據(jù)導(dǎo)入見:Python學(xué)習(xí)之?dāng)?shù)據(jù)導(dǎo)入

1、讀取數(shù)據(jù)(X:獨(dú)立數(shù)據(jù)、Y:聯(lián)動(dòng)數(shù)據(jù))



處理之前的數(shù)據(jù)

#導(dǎo)入包

import numpy as np #矩陣

import matplotlib.pyplot as plt #數(shù)據(jù)展示、可視化

import pandas as pd? ? #數(shù)據(jù)預(yù)處理

#import dataset

datasets = pd.read_csv('Data.csv')

#missing data 丟失數(shù)據(jù)處理 1、去最大值 最小值,2、平均數(shù) 3、刪除

X = datasets.iloc[:,:-1].values? #取出獨(dú)立變量

Y = datasets.iloc[:,3].values

#數(shù)據(jù)預(yù)處理,補(bǔ)充缺失數(shù)據(jù)

from sklearn.preprocessing import Imputer

#mean 缺失的用平均數(shù)填充

#怎么處理數(shù)據(jù)

imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)

#處理哪里的數(shù)據(jù)

imputer = imputer.fit( X[:, 1:3])

X[:,1:3] = imputer.transform( X[:,1:3])

#查看補(bǔ)充缺失數(shù)據(jù)之后的數(shù)據(jù)

X

解釋:“imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0):

NaN:缺失數(shù)據(jù)

strategy:缺失數(shù)據(jù)處理方式,平均值,

If “mean”, then replace missing values using the mean along the axis.

If “median”, then replace missing values using the median along the axis.

If “most_frequent”, then replace missing using the most frequent value along the axis.

axis:

Ifaxis=0, then impute along columns.Ifaxis=1, then impute along rows.

2、查看補(bǔ)充缺失數(shù)據(jù)之后的數(shù)據(jù)

補(bǔ)充缺失之后的數(shù)據(jù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,948評(píng)論 18 139
  • 前言:?jiǎn)诬囎鳛楫?dāng)下最火的共享經(jīng)濟(jì)項(xiàng)目獲得了全世界人民的關(guān)注,我們看到它帶來便捷出行的時(shí)候卻沒有發(fā)現(xiàn)它的丑陋一面(當(dāng)...
    高斯不模糊閱讀 1,248評(píng)論 5 0
  • 從小到大的教育似乎沒有那么的著急,記得上幼兒園,就是爸爸騎著自行車載著我,到了小賣部,買了一支鉛筆,一個(gè)橡皮,一本...
    天涯Michael閱讀 610評(píng)論 0 0