14 lines of code python with you through the whole process of learning machine

Iris classification problem is an entry-level machine learning problem, we can use a few simple lines of code to learn the whole process of machine learning through the python sklearn library will eventually give comparative predictions and actual values, and give the correct rate score.
Let's sort out the basic idea of this program.
An import library sklearn, including the need to use the data set dataset.load_iris (), and SVM classifier tool, the model used in the data set split tool.
Second, using python command to import data, and set up training and test sets.
Third, create svm.LinearSVC classification, training data input training set a good model in place clf.
The use of classifiers clf fit method of fitting training
five, predict method using a classifier clf set of test data to predict
six real results predicted results and test sets of comparative test set and get method using the score of clf forecast accuracy.

Specific code as follows:

from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import train_test_split
iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=0)
clf = svm.LinearSVC()
clf.fit(X_train,y_train)
y_predict = clf.predict(X_test)
comparison = ['predict: '+str(a)+' realcat: '+str(b) for a,b in zip(y_predict,y_test)]
for comp in comparison:
	print(comp)
print(f'Score:{clf.score(X_test,y_test)}')

The resulting output is as follows:
Here Insert Picture Description
The model made the prediction accuracy of 93%, can be said to be very Ollie given _

Published 170 original articles · won praise 9 · views 4537

Guess you like

Origin blog.csdn.net/weixin_41855010/article/details/104488256