各分类算法:
KNN
from sklearn.neighbors import KNeighborsClassifierimport numpy as npdef 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 SVCdef SVM(X,y,XX): model = SVC(c=5.0) model.fit(X,y) predicted = model.predict(XX) return predicted
LR
from sklearn.linear_model import LogisticRegressiondef LR(X,y,XX): model = LogisticRegression() model.fit(X,y) predicted = model.predict(XX) return predicted
决策树
from sklearn.tree import DecisionTreeClassifierdef CTRA(X,y,XX): model = DecisionTreeClassifier() model.fit(X,y) predicted = model.predict(XX) return predicted
朴素贝叶斯:一个是基于高斯分布求概率,一个是基于多项式分布求概率。
from sklearn.naive_bayes import GaussianNBfrom sklearn.naive_bayes import MultinomialNBdef GNB(X,y,XX): model =GaussianNB() model.fit(X,y) predicted = model.predict(XX) return predicteddef MNB(X,y,XX): model = MultinomialNB() model.fit(X,y) predicted = model.predict(XX return predicted