machine learning
|python
ctrl + i 可尋找方法或類別的相關資訊
dataset.info() # 資料類別、是否有缺失資料等資訊
養成習慣:
取得dataset後第一步:觀察並分析欄位、特徵
以下皆使用此表格作為範例
此表格可預測顧客是否購買產品
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
as 後面為別名
- numpy –> 提供矩陣數學運算方法
- matplotlib.pyplot –> 提供畫圖方法
長條圖、直線圖等
- pandas –> 提供讀取資料集的方法
通常為csv檔
選取資料集檔案路徑
透過pandas中的read_csv方法來讀取
dataset = pd.read_csv('Data.csv')
python裡,字串用單引號或雙引號都可
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 3].values
:表示所有rows or columns
:-1 表示所有rows or columns但不包含最後一個row or column
缺點:
投入的成本會損失Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
10筆資料內,有2筆資料缺失,若刪除,相當於20%的資料被刪掉Image Not Showing Possible ReasonsLearn More →
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
from sklearn.impute import SimpleImputer
- 使用scikit-learn (sklearn)
提供資料分析與資料處理的方法- 使用sklearn中的impute
提供估算的方法- 使用sklearn中的impute的SimpleImputer類別
提供missing data處理的方法
imputer = SimpleImputer(missing_values=np.nan, strategy="mean", fill_value=None)
- 平均值(mean)或中位數(median) –> 連續型數值
- 最常出現的(most_frequent) –> 分類型欄位
- 常數(constant) –> fill_value要寫值多少
imputer = imputer.fit(x[:, 1:3])
- 透過imputer.fit()進行資料擬合,並設定要進行缺失資料擬合的範圍
x[:, 1:3] –> x[all rows, 1 & 2 columns]
1:3表示索引值1到3,但不包含3(第2及第3行)
x[:, 1:3] = imputer.transform(x[:, 1:3])
- 透過imputer.transform()進行資料轉換,並設定要進行缺失資料轉換的範圍
x[:, 1:3] –> x[all rows, 1 & 2 columns]
1:3表示索引值1到3,但不包含3(第2及第3行)- 將轉換的結果設定回原本的資料集合中
from sklearn.preprocessing import LabelEncoder
- 使用sklearn中的preprocessing
提供預處理的方法- 使用sklearn中的preprocessing的LabelEncoder類別
提供標籤編碼處理的方法
labelencorder_x = LabelEncoder()
- 宣告LabelEncoder物件
針對Country進行標籤編碼(Country為input –>)
x[:, 0] = labelencorder_x.fit_transform(x[:, 0])
- 使用LabelEncoder中的fit_transform()進行資料擬合與轉換,並設定範圍
x[:, 0] –> x[all rows, 0 column]- 將轉換的結果設定回原本的資料集合中
labelencorder_y = LabelEncoder()
y = labelencorder_y.fit_transform(y)
- 應變量能夠自動被識別為類別,所以不需要做虛擬編碼,直接使用LabelEncoder即可
- 使用LabelEncoder中的fit_transform()進行資料擬合與轉換
標籤編碼後有順序之分,此例的Country被分成三類,延展成三行
from sklearn.preprocessing import OneHotEncoder
- 使用sklearn中的preprocessing的OneHotEncoder類別
提供虛擬編碼處理的方法
from sklearn.compose import ColumnTransformer
- 使用sklearn中的compose
提供編寫的方法
ct = ColumnTransformer([("Country", OneHotEncoder(), [0])] , remainder="passthrough")
- 參數設定為須處理虛擬編碼的欄位
[0] –> 索引值為0的行 –> Country- remainder:其他未指定的欄位要如何處理
remainder = "passthrough" –> 剩餘欄位不會被轉換
X = ct.fit_transform(x)
- 使用ColumnTransformer中的fit_transform()進行資料擬合與轉換,並且轉換為array()
from sklearn.model_selection import train_test_split
- 使用sklearn中的model_selection的train_test_split類別
提供分割訓練集合與測試集合的方法
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
- 若給一個array[x],train_test_split會回傳兩組array,分別是測試集合與訓練集合
- test_size = 0.2 –> 測試集合為20%
- random_state = 0 –> 取值不變時,每次得到的結果一樣;取值改變時,每次得到的結果不同;不設定參數時,會隨機選擇樣本,得到的结果也就不同
以此例來說,Age和Salary的範圍不同(Age落在0-100左右,Salary以萬起跳),若方程式為
缺點:丟棄特徵 –> 損失成本
不論範圍如何,為求模型更客觀,都會做特徵縮放
:標準差
:平均值
數值落在0~1之間
from sklearn.preprocessing import StandardScaler
- 使用sklearn中的preprocessing的StandardScaler類別
提供標準化的方法
sc_x = StandardScaler()
x_train = sc_x.fit_transform(x_train)
x_test = sc_x.transform(x_test)
- 宣告StandardScaler()物件
針對自變量進行 - 使用StandardScaler()中的fit_transform()對x_train進行資料擬合與轉換
- 因為sc_x已經被擬合過,故第3行的sc_x不需要再用fit_transform(),可直接使用transform()
- 應變量是用來分辨的(是否購買),故維持分類編碼(0、1)
機器學習-作業1
# Importing the Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the Dataset
dataset = pd.read_csv("Data.csv")
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 3].values
# Missing Data
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values=np.nan, strategy="mean", fill_value=None)
imputer = imputer.fit(x[:, 1:3])
x[:, 1:3] = imputer.transform(x[:, 1:3])
# Categorical Data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
labelencorder_x = LabelEncoder()
x[:, 0] = labelencorder_x.fit_transform(x[:, 0])
ct = ColumnTransformer([("Country", OneHotEncoder(), [0])] , remainder="passthrough")
X = ct.fit_transform(x)
labelencorder_y = LabelEncoder()
y = labelencorder_y.fit_transform(y)
# Splitting the Dataset into the Training set and Test set
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)
# 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)
機器學習-作業3
# Importing the libraries
import numpy as np
#import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv("Customers.csv")
x = dataset.iloc[:, [1, 2, 3, 5, 6, 7]].values
y = dataset.iloc[:, 4].values
dataset.info()
# Missing Data
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values=np.nan, strategy="most_frequent", fill_value=None)
#X = np.reshape(x[:, 3], (-1, 1))
imputer = imputer.fit(x[:, [3]])
x[:, [3]] = imputer.transform(x[:, [3]])
"""
if len(x) == 0 :
print("list is empty")
else : print("list is not empty")
"""
# Categorical Data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
labelencorder_x = LabelEncoder()
x[:, 0] = labelencorder_x.fit_transform(x[:, 0])
x[:, 3] = labelencorder_x.fit_transform(x[:, 3])
ct = ColumnTransformer([("Gender", OneHotEncoder(), [0]), ("Profession", OneHotEncoder(), [3])] , remainder="passthrough")
X = ct.fit_transform(x)
'''
labelencorder_y = LabelEncoder()
y = labelencorder_y.fit_transform(y)
'''
# Splitting the Dataset into the Training set and Test set
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)
# 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)