機器學習演算法總結

大樹2發表於2017-12-14

作者:大樹
更新時間:2017.12.14

email:59888745@qq.com

說明:因內容較多,會不斷更新 xxx學習總結;

回主目錄:2017 年學習記錄和總結

機器學習演算法總結:

線性迴歸

(Linear Regression) (ML分類)

Y=aX+b

利用連續性變數來估計實際數值

通過線性迴歸演算法找出自變數和因變數間的最佳線性關係,圖形上可以確定一條最佳直線

from   sklearn import linear_model

x_train=input_variables_values_training_datasets

y_train=target_variables_values_training_datasets

x_test=input_variables_values_test_datasets

linear   = linear_model.LinearRegression()

linear.fit(x_train,   y_train)

linear.score(x_train,   y_train)

print('Coefficient:   \n', linear.coef_)

print('Intercept:   \n', linear.intercept_)

predicted=   linear.predict(x_test)

房價,呼叫次數和總銷售額

邏輯迴歸(ML分類)

利用已知的自變數來預測一個離散型因變數的值(像二進位制值0/1,是/否,真/假)。簡單來說,它就是通過擬合一個邏輯函式(logit fuction)來預測一個事件發生的概率。所以它預測的是一個概率值,自然,它的輸出值應該在0到1之間.

#Import   Library

from   sklearn.linear_model import LogisticRegression

#   Create logistic regression object

model   = LogisticRegression()

#   Train the model using the training sets and check score

model.fit(X,   y)

model.score(X,   y)

#Equation   coefficient and Intercept

print('Coefficient:   \n', model.coef_)

print('Intercept:   \n', model.intercept_)

#Predict   Output

predicted=   model.predict(x_test)

二進位制值0/1,是/否,真/假

決策樹(ML分類)

可以運用於類別變數(categorical variables)也可以作用於連續變數。這個演算法可以讓我們把一個總體分為兩個或多個群組。分組根據能夠區分總體的最重要的特徵變數/自變數進行

#   Create tree object

model   = tree.DecisionTreeClassifier(criterion='gini')

#   model = tree.DecisionTreeRegressor() for regression

#   Train the model using the training sets and check score

model.fit(X,   y)

model.score(X,   y)

#Predict   Output

predicted=   model.predict(x_test)

把一個總體分為兩個或多個群組解決分類問題

支援向量機(SVM)(ML分類)

將每一個資料作為一個點在一個n維空間上作圖(n是特徵數),每一個特徵值就代表對應座標值的大小。比如說我們有兩個特徵:一個人的身高和髮長。我們可以將這兩個變數在一個二維空間上作圖,圖上的每個點都有兩個座標值(這些座標軸也叫做支援向量)。

#Import   Library

from   sklearn import svm

#   Create SVM classification object

model   = svm.svc()

#   Train the model using the training sets and check score

model.fit(X,   y)

model.score(X,   y)

#Predict   Output

predicted=   model.predict(x_test)

是把不同顏色的小球分到不同空間裡

樸素貝葉斯(ML分類)

假設條件是自變數之間相互獨立。簡言之,樸素貝葉斯假定某一特徵的出現與其它特徵無關.

如何從先驗概率P(c),P(x)和條件概率P(x|c)中計算後驗概率P(c|x)。

#Import   Library

from   sklearn.naive_bayes import GaussianNB

 

#   Create NB classification object model = GaussianNB()

model   = GaussianNB()

#   Train the model using the training sets and check score

model.fit(X,   y)

#Predict   Output

predicted=   model.predict(x_test)

如果一個水果它是紅色的,圓狀的,直徑大概7cm左右,我們可能猜測它為蘋果

天氣變數和目標變數“是否出去玩

                        

KNN(K-鄰近演算法)(ML分類)

找出已知資料中距離未知事件最近的K組資料,最後按照這K組資料裡最常見的類別預測該事件, 距離函式可以是歐式距離,曼哈頓距離,閔氏距離   (Minkowski Distance), 和漢明距離(Hamming Distance)。前三種用於連續變數,漢明距離用於分類變數。如果K=1,那問題就簡化為根據最近的資料分類。K值的選取時常是KNN建模裡的關鍵。

 

#Import   Library

from   sklearn.neighbors import KNeighborsClassifier

 

#Assumed   you have, X (predictor) and Y (target) for training data set and   x_test(predictor) of test_dataset

#   Create KNeighbors classifier object model

 

KNeighborsClassifier(n_neighbors=6)   # default value for n_neighbors is 5

 

#   Train the model using the training sets and check score

model.fit(X,   y)

 

#Predict   Output

predicted=   model.predict(x_test)

分類問題,也可以用於迴歸問題

KNN的計算成本很高.

所有特徵應該標準化數量級,否則數量級大的特徵在計算距離上會有偏移.

在進行KNN前預處理資料,例如去除異常值,噪音等.

K均值演算法(K-Means)(ML聚類非監督式學習)

利用了一定數量的叢集(假設K個叢集)對給定資料進行分類。同一叢集內的資料點是同類的,不同叢集的資料點不同類.

#Import   Library

from   sklearn.cluster import KMeans

 

#Assumed   you have, X (attributes) for training data set and x_test(attributes) of   test_dataset

#   Create KNeighbors classifier object model

k_means   = KMeans(n_clusters=3, random_state=0)

 

#   Train the model using the training sets and check score

model.fit(X)

 

#Predict   Output

predicted=   model.predict(x_test)

解決聚類問題的非監督式學習演算法.

隨機森林(ML分類)

隨機森林是對決策樹集合的特有名稱。隨機森林裡我們有多個決策樹(所以叫“森林”)。為了給一個新的觀察值分類,根據它的特徵,每一個決策樹都會給出一個分類。隨機森林演算法選出投票最多的分類作為分類結果。

#Import Library

from sklearn.ensemble import   RandomForestClassifier

#Assumed you have, X (predictor) and Y   (target) for training data set and x_test(predictor) of test_dataset

 

# Create Random Forest object

model= RandomForestClassifier()

 

# Train the model using the training sets   and check score

model.fit(X, y)

 

#Predict Output

predicted= model.predict(x_test)

 

降維演算法(Dimensionality Reduction Algorithms

怎樣才能從1000或2000個變數裡找到最重要的變數呢?這種情況下降維演算法及其他演算法,如決策樹,隨機森林,PCA,因子分析,相關矩陣,和預設值比例等,就能幫我們解決難題。

#Import   Library

from   sklearn import decomposition

#Assumed   you have training and test data set as train and test

#   Create PCA obeject pca= decomposition.PCA(n_components=k) #default value of k   =min(n_sample, n_features)

#   For Factor analysis

#fa=   decomposition.FactorAnalysis()

#   Reduced the dimension of training dataset using PCA

 

train_reduced   = pca.fit_transform(train)

 

#Reduced   the dimension of test dataset

test_reduced   = pca.transform(test)

 

Gradient Boosing 和 AdaBoost

是在有大量資料時提高預測準確度的boosting演算法。Boosting是一種整合學習方法。它通過有序結合多個較弱的分類器/估測器的估計結果來提高預測準確度。這些boosting演算法在Kaggle,AV Hackthon,   CrowdAnalytix等資料科學競賽中有出色發揮。

#Import   Library

from   sklearn.ensemble import GradientBoostingClassifier

#Assumed   you have, X (predictor) and Y (target) for training data set and   x_test(predictor) of test_dataset

#   Create Gradient Boosting Classifier object

model=   GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1,   random_state=0)

 

# Train   the model using the training sets and check score

model.fit(X,   y)

#Predict   Output

predicted=   model.predict(x_test)

 

 

 

 

相關文章