Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python Machine Learning how to recognize numbers by sklearn

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report