数据分析之数据划分以及模型搭建

1.获得数据的特征和标注:features,label
2.切割数据:训练集,验证集,测试集 6:2:2
3.训练模型
def models(features,label):
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import accuracy_score,recall_score,f1_score
    f_v=features.values
    l_v=label
    x_t,x_yz,y_t,y_yz=train_test_split(f_v,l_v,test_size=0.2)
    x_xl,x_cs,y_xl,y_cs=train_test_split(x_t,y_t,test_size=0.2)
    获得训练集,验证集,测试集:x_xl,x_yz,x_cs
    
    models=[]
    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.naive_bayes import GaussianNB,BernoulliNB
    models.append(("KNN",KNeighborsClassifier(n_neighbors=45)))
    models.append(("GaussionNB",GaussianNB()))
    models.append(("BernoulliNB",BernoulliNB()))
    
    for clf_name,clf in models:
        clf.fit(x_xl,y_xl)
        x_list=[(x_xl,y_xl),(x_cs,y_cs),(x_yz,y_yz)]
        for i in range(len(x_list)):
            x_part=x_list[i][0]
            y_part=x_list[i][1]
            y_pre=clf.predict(x_part)
            print(i)
            print(clf_name,"ACC:",accuracy_score(y_pre,y_part))
            print(clf_name,"REC:",recall_score(y_pre,y_part))
            print(clf_name,"F1:",f1_score(y_pre,y_part))




 

你可能感兴趣的:(菜鸟计划)