Machine learning -- common use of sklearn

# Naive Bayes
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB() #train the model
clf.fit(features_train,labels_train)     
pred = clf.pred(features_test) #Give test variables and predict results

from sklearn.metrics import accuracy_score
accuracy = accuracy_score(pred, labels_test) # prediction accuracy

#-------------------------------------------------------------------------------------------
# SVM support vector machine, can reduce the running time by reducing the size of the training set (sacrificing accuracy)
from sklearn.svm import SVC

clf = SVC(C=10000,kernel='rbf') # The size of C determines the degree of fitting, kernel is a kernel function, such as linear\rbf\polynomial, etc.
t0 = time() # The bigger the C, the better, but it may overfit
clf.fit(features_train,labels_train)
print "train time is :" , round(time()-t0, 3), "s"    

t0 = time()
pred = clf.predict(features_test)
print "fit time is :" , round(time()-t0,3), "s"
#print pred[10],pred[26],pred[50] # Get the prediction result of the 10th/26th/50th

from sklearn.metrics import accuracy_score
acc = accuracy_score(pred, labels_test)
print acc # accuracy

#--------------------------------------------------------------------------------------------




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325792513&siteId=291194637