您好,登錄后才能下訂單哦!
本文實例講述了Python使用sklearn庫實現的各種分類算法簡單應用。分享給大家供大家參考,具體如下:
KNN
from sklearn.neighbors import KNeighborsClassifier import numpy as np def KNN(X,y,XX):#X,y 分別為訓練數據集的數據和標簽,XX為測試數據 model = KNeighborsClassifier(n_neighbors=10)#默認為5 model.fit(X,y) predicted = model.predict(XX) return predicted
SVM
from sklearn.svm import SVC def SVM(X,y,XX): model = SVC(c=5.0) model.fit(X,y) predicted = model.predict(XX) return predicted
SVM Classifier using cross validation
def svm_cross_validation(train_x, train_y): from sklearn.grid_search import GridSearchCV from sklearn.svm import SVC model = SVC(kernel='rbf', probability=True) param_grid = {'C': [1e-3, 1e-2, 1e-1, 1, 10, 100, 1000], 'gamma': [0.001, 0.0001]} grid_search = GridSearchCV(model, param_grid, n_jobs = 1, verbose=1) grid_search.fit(train_x, train_y) best_parameters = grid_search.best_estimator_.get_params() for para, val in list(best_parameters.items()): print(para, val) model = SVC(kernel='rbf', C=best_parameters['C'], gamma=best_parameters['gamma'], probability=True) model.fit(train_x, train_y) return model
LR
from sklearn.linear_model import LogisticRegression def LR(X,y,XX): model = LogisticRegression() model.fit(X,y) predicted = model.predict(XX) return predicted
決策樹(CART)
from sklearn.tree import DecisionTreeClassifier def CTRA(X,y,XX): model = DecisionTreeClassifier() model.fit(X,y) predicted = model.predict(XX) return predicted
隨機森林
from sklearn.ensemble import RandomForestClassifier def CTRA(X,y,XX): model = RandomForestClassifier() model.fit(X,y) predicted = model.predict(XX) return predicted
GBDT(Gradient Boosting Decision Tree)
from sklearn.ensemble import GradientBoostingClassifier def CTRA(X,y,XX): model = GradientBoostingClassifier() model.fit(X,y) predicted = model.predict(XX) return predicted
樸素貝葉斯:一個是基于高斯分布求概率,一個是基于多項式分布求概率,一個是基于伯努利分布求概率。
from sklearn.naive_bayes import GaussianNB from sklearn.naive_bayes import MultinomialNB from sklearn.naive_bayes import BernoulliNB def GNB(X,y,XX): model =GaussianNB() model.fit(X,y) predicted = model.predict(XX) return predicted def MNB(X,y,XX): model = MultinomialNB() model.fit(X,y) predicted = model.predict(XX return predicted def BNB(X,y,XX): model = BernoulliNB() model.fit(X,y) predicted = model.predict(XX return predicted
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python數據結構與算法教程》、《Python加密解密算法與技巧總結》、《Python編碼操作技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》
希望本文所述對大家Python程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。