Python機器學習筆記(九):Scikit-Learn演算法快速套用手冊(監督學習-分類篇)

Logistic Regression

# Logistic Regressionfrom sklearn import datasetsfrom sklearn import metricsfrom sklearn.linear_model import LogisticRegressionfrom sklearn import svm# load the iris datasetsdataset = datasets.load_iris()# fit a logistic regression model to the datamodel = LogisticRegression()model.fit(dataset.data, dataset.target)print(model)# make predictionsexpected = dataset.targetpredicted = model.predict(dataset.data)# summarize the fit of the modelprint(metrics.classification_report(expected, predicted))print(metrics.confusion_matrix(expected, predicted))#一次使用多種不同的Kernel
for k in ['linear','poly','rbf','sigmoid']:
clf = svm.SVR(kernel=k)
clf.fit(X_train, y_train)
confidence = clf.score(X_test, y_test)
print(k,confidence)

Naive Bayes

# Gaussian Naive Bayesfrom sklearn import datasetsfrom sklearn import metricsfrom sklearn.naive_bayes import GaussianNB# load the iris datasetsdataset = datasets.load_iris()# fit a Naive Bayes model to the datamodel = GaussianNB()model.fit(dataset.data, dataset.target)print(model)# make predictionsexpected = dataset.targetpredicted = model.predict(dataset.data)# summarize the fit of the modelprint(metrics.classification_report(expected, predicted))print(metrics.confusion_matrix(expected, predicted))

k-Nearest Neighbor

# k-Nearest Neighborfrom sklearn import datasetsfrom sklearn import metricsfrom sklearn.neighbors import KNeighborsClassifier# load iris the datasetsdataset = datasets.load_iris()# fit a k-nearest neighbor model to the datamodel = KNeighborsClassifier()model.fit(dataset.data, dataset.target)print(model)# make predictionsexpected = dataset.targetpredicted = model.predict(dataset.data)# summarize the fit of the modelprint(metrics.classification_report(expected, predicted))print(metrics.confusion_matrix(expected, predicted))print(model.score(expected, predicted))

Classification and Regression Trees

# Decision Tree Classifierfrom sklearn import datasetsfrom sklearn import metricsfrom sklearn.tree import DecisionTreeClassifier# load the iris datasetsdataset = datasets.load_iris()# fit a CART model to the datamodel = DecisionTreeClassifier()model.fit(dataset.data, dataset.target)print(model)# make predictionsexpected = dataset.targetpredicted = model.predict(dataset.data)# summarize the fit of the modelprint(metrics.classification_report(expected, predicted))print(metrics.confusion_matrix(expected, predicted))

Support Vector Machines

# Support Vector Machinefrom sklearn import datasetsfrom sklearn import metricsfrom sklearn.svm import SVC# load the iris datasetsdataset = datasets.load_iris()# fit a SVM model to the datamodel = SVC()model.fit(dataset.data, dataset.target)print(model)# make predictionsexpected = dataset.targetpredicted = model.predict(dataset.data)# summarize the fit of the modelprint(metrics.classification_report(expected, predicted))print(metrics.confusion_matrix(expected, predicted))print(model.score(expected, predicted))

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store