机器学习常用的六种分类方法,Python代码详细都在这里!

机器学习常用的六种分类方法,Python代码详细都在这里!
六种常用分类方法包括两种线性分类及四种非线性分类法,分别是:
一、线性判别分析

from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.preprocessing import StandardScaler
from sklearn import svm
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from numpy import *
import
def lda():
    #data=np.loadtxt(open("D:\\卞朝法文件\\Myfile\\红边位置.txt"))
    # 处理数据
    # data = pd.DataFrame(data)
    x,y = Data[:,:-1],Data[:,-1]
    x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.25)#random_state=10
    # 特征工程标准化
    std = StandardScaler()
    x_train = std.fit_transform(x_train)
    x_test = std.transform(x_test)
    # y_train=std.fit_transform(y_train.reshape(-1, 1))
    # y_test=std.transform(y_test.reshape(-1, 1))

    # 用线性判别分析分类
    classifer = LinearDiscriminantAnalysis()
    classifer.fit(x_train, y_train)
    predictions = classifer.predict(x_test)
    #返回值为准确率,可以设为预测值
    return classifer.score(x_test,y_test)
#由于随机种子大小不同,数据集划分不确定性,so取函数运行100次均值 
start = datetime.datetime.now()
a1=[]
for i in range(1,100):
    k1=lda()
    a1.append(k1)
b1=mean(a1)
print("线性判别分析准确率均值:",b1) 
#记录运行时间
end = datetime.datetime.now()
print("运行时长",end - start)

二、逻辑回归分类
仅需改变上述代码中类及函数名即可

classifer = LogisticRegression()

三、支持向量机高斯核函数分类
仅需改变上述代码中类及函数名即可

rbf_clf = svm.SVC(C=0.8, kernel='rbf', gamma=20, decision_function_shape='ovr')#C=0.8

四、朴素贝叶斯分类
仅需改变上述代码中类及函数名即可

mlt=GaussianNB()

五、K近邻分类
仅需改变上述代码中类及函数名即可

 Knn=KNeighborsClassifier(n_neighbors=7)

六、决策树分类
仅需改变上述代码中类及函数名即可

classifer = DecisionTreeClassifier()

如果为您解答了问题,就关注点赞以及评论哦,谢谢您的支持!

你可能感兴趣的:(python,机器学习)