Get the App
SLTechnology News&Howtos  ›  Development  › 

Python Machine Learning how to recognize numbers by sklearn

Shulou Source: shulou.com Published: 2022-06-01 02:54:07 09月10日 Update

This article mainly introduces the python machine learning sklearn how to achieve digital recognition related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this python machine learning sklearn how to achieve digital recognition article will have a harvest, let's take a look.

Data processing data separation

Because we open our learning data set, the last item is our real value, as anyone who has read Xiao Tang's previous article knows, the old rule is to split first, the front features are put together, the back real values are put together, and because the data is not listed, we choose to use iloc [] to achieve separation.

Def shuju (tr_path,ts_path,sep='\ t'): train=pd.read_csv (tr_path,sep=sep) test=pd.read_csv (ts_path,sep=sep) # feature and result separation train_features=train.iloc [:,:-1] .values train_labels=train.iloc [:,-1] .values test_features = test.iloc [:,:-1] .values test_labels = test.iloc [:,-1] .values return train_features,test_features Train_labels,test_labels training data

We use the sklearn function directly here, by selecting the model, and then directly generating its recognition rules.

# training data def train_tree (* data): x_train, x_test, y_train, y_test=data clf=DecisionTreeClassifier () clf.fit (learning model prediction score: {: .4f}) print ("learning model prediction score: {: .4f}") .format (clf.score (x game forecast performance yearly train)) print ("actual model prediction performance: {: .4f}" .format (clf.score (x_test)) Y_test)) # return return clf data Visualization of Learning Model

To make our observations more intuitive, we can also use matplotlib to make observations

Def plot_imafe (test,test_labels,preds): plt.ion () plt.show () for i in range (50): label,pred=test_ labels [I], preds [I] title=' actual values: {}, predict {} '.format (label,pred) img= test [I] .reshape (281,28) plt.imshow (img) Cmap= "binary") plt.title (title) plt.show () print ('done')

Result

Complete code import pandas as pdfrom sklearn.tree import DecisionTreeClassifierimport matplotlib.pyplot as pltdef shuju (tr_path,ts_path,sep='\ t'): train=pd.read_csv (tr_path,sep=sep) test=pd.read_csv (ts_path,sep=sep) # feature and result separation train_features=train.iloc [:,:-1] .values train_labels=train.iloc [:,-1] .values test_features = test.iloc [:,:-1] .values test_labels = test.iloc [: -1] .values return train_features,test_features,train_labels,test_labels# training data def train_tree (* data): x_train, x_test, y_train, y_test=data clf=DecisionTreeClassifier () clf.fit. ("Learning Model Forecast score: {: .4f}" .format (clf.score (x_train) Y_train)) print ("actual model prediction scores: {: .4f}" .format (clf.score (x_test, y_test)) # returns the learning model return clfdef plot_imafe (test,test_labels,preds): plt.ion () plt.show () for i in range (50): label,pred=test_ labels [I], preds [I] title=' actual values: {}, predict {} '.format (label) Pred) img= [I] .reshape (2828) plt.imshow (img,cmap= "binary") plt.title (title) plt.show () print ('done') train_features,test_features,train_labels,test_labels=shuju (r "C:\ Users\ twy\ PycharmProjects\ 1\ train_images.csv", r "C:\ Users\ twy\ PycharmProjects\ 1\ test_images.csv") clf=train_tree (train_features,test_features,train_labels Test_labels) preds=clf.predict (test_features) plot_imafe (test_features,test_labels,preds) on "how to recognize numbers by python machine learning sklearn" ends here. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "python machine learning sklearn how to identify numbers". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Learning data models numbers machines reality scores characteristics knowledge results training content articles selection intuition code value functions and at the same time easy to operate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia Shulou Information Linux MySQL