python機器學習速成|1|資料匯入

夜神moon發表於2018-10-17

主要任務:
①完成常見的資料匯入操作,包括資料匯入,缺失值填充
②完成常見的機器學習資料準備,包括特徵二值化和訓練集測試集的劃分等

# -*- coding: utf-8 -*-
"""
Created on Wed Oct 17 00:26:22 2018

@author: Administrator
"""
%reset -f
%clear
# In[*]
## 第1步:匯入庫
#Day 1: Data Prepocessing

#Step 1: Importing the libraries
import numpy as np
import pandas as pd
import os
os.chdir("E:multimlcoad")
# In[*]
#Step 2: Importing dataset
dataset = pd.read_csv(`coad_messa.csv`,header=0,index_col=0)

X = dataset.iloc[ : , :-1].values
Y = dataset.iloc[ : , 6].values
# In[*]
print("Step 2: Importing dataset")
print("X")
print(X)
print("Y")
print(Y)

這一步主要是匯入資料,我們的前6列為用來預測的輸入資料,包括gender, stage等等,我們將其設定為X,而輸出資料,預測目標為患者的特徵,可以是腫瘤或者正常等等,我們將其設定為Y。

 Step 2: Importing dataset
X
[[61.  0.  1.  1.  1.  1.]
 [67.  1.  3.  1.  2.  3.]
 [42.  0.  2.  2.  1.  1.]
 ...
 [44.  0.  2.  1.  2.  1.]
 [82.  1.  2.  1.  2.  1.]
 [52.  0.  2.  2.  1.  1.]]
> Y
[0. 0. 0. 1. 1. 1. 0. 1. 0. 1. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 0. 0. 0.
 0. 0. 1. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 0. 0. 0. 0. 0. 1. 1. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 1. 1. 1. 1. 0. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 1. 1.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1.
 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0.
 0. 1. 0. 0. 0. 0. 1. 1. 1. 0. 0. 1.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preprocessing import Imputer
imputer = Imputer(missing_values = "NaN", strategy = "mean", axis = 0)
imputer = imputer.fit(X[ : , 1:3])
X[ : , 1:3] = imputer.transform(X[ : , 1:3])
# In[*]
print("---------------------")
print("Step 3: Handling the missing data")
print("step2")
print("X")
print(X)
# In[*]
#Step 4: Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[ : , 2] = labelencoder_X.fit_transform(X[ : , 2])
# In[*]
#Creating a dummy variable
onehotencoder = OneHotEncoder(categorical_features = [2])
X = onehotencoder.fit_transform(X).toarray()
labelencoder_Y = LabelEncoder()
Y =  labelencoder_Y.fit_transform(Y)
# In[*]
print("---------------------")
print("Step 4: Encoding categorical data")
print("X")
print(X)
print("Y")
print(Y)

這一步主要是將其中的資料二值化,因為我們使用的資料包括性別,眾所周知,性別是男性或者女性,雖然我們可以簡單的將其設定為0和1或者將其設定為1,2.但是

對於一些特徵工程方面,有時會用到LabelEncoder和OneHotEncoder。比如kaggle中對於性別,sex,一般的屬性值是male和female。兩個值。那麼不靠譜的方法直接用0表示male,用1表示female 了。上面說了這是不靠譜的。所以要用one-hot編碼。首先我們需要用LabelEncoder把sex這個屬性列裡面的離散屬性用數字來表示,就是上面的過程,把male,female這種不同的字元的屬性值,用數字表示。

# In[*]
#Step 5: Splitting the datasets into training sets and Test sets
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split( X , Y ,
                                                    test_size = 0.2, 
                                                    random_state = 0)

# In[*]
print("---------------------")
print("Step 5: Splitting the datasets into training sets and Test sets")
print("X_train")
print(X_train)
print("X_test")
print(X_test)
print("Y_train")
print(Y_train)
print("Y_test")
print(Y_test)
# In[*]
#Step 6: Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)
# In[*]
print("---------------------")
print("Step 6: Feature Scaling")
print("X_train")
print(X_train)
print("X_test")
print(X_test)

最終我們將資料劃分成訓練集(80%)和測試集(20%)


相關文章