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

Example Analysis of logical regression Classification of Python Spam

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

Share

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

This article mainly introduces "the example analysis of logical regression classification of Python spam". In the daily operation, I believe that many people have doubts about the example analysis of logical regression classification of Python spam. The editor has consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "logical regression classification example analysis of Python spam". Next, please follow the editor to study!

Process the dataset as follows

The main results are as follows: (1) separate the part containing only the characteristic column as X and the part containing only the target column as Y.

(2) split the data set into training set and test set (70% and 30%).

Establish a logical regression model

The models were established by LogisticRegression respectively.

Result comparison

(1) output the prediction results of the first five samples of the test set.

(2) calculate the classification accuracy of the model on the test set (= the number of correctly classified samples / the total number of samples in the test set)

(3) find out the samples which can not be predicted correctly by the model from the test set.

(4) compare the prediction performance of the parameters penalty on the test set (calculate score) by taking'L1','L2 'and' elasticnet', 'none', respectively.

It is known before to split eigenvalues and target data, and the prediction and model score results are also methods that directly use the model. The following is mainly to test the accuracy and find out the samples that cannot be predicted correctly, as well as the model scores under different penalties, which are mainly used in the Numpy module and list list functions. The code is as follows:

Y_train_pred = model.predict (x_train) # In [26]: train_accu = np.equal (print (y_train.shape) Y_train_pred.shape) # In [27]: # calculate the classification accuracy of the model on the test set (= number of correctly classified samples / total number of samples in the test set) accuracy = list (train_accu) .count (True) / len (list (y_train)) print ("accuracy is" +% f "% float (accuracy*100) +"% ") # In [28]: # find out the samples from the test set that the model cannot correctly predict. Test_accu = list (np.equal (yearly predacious index)) I = 0len_test = len (test_accu) False_index = [] [False_index.append (I) for i in range (0cet lenient test) if test_ accu [I] = = False] print (x _ test [false _ test]) # In [29]: # compare their prediction performance on the test set (calculating score) by taking'L1','L2 'and' elasticnet', 'none', for parameter penalty respectively. Model2 = LogisticRegression (multi_class='ovr',penalty='l1',max_iter=10000,solver='saga') .fit (x_train, y_train) print (score of "penalty='l1'" +':', model2.score (x_test, y_test)) # In [33]: # pairs of parameters penalty take'L1','L2 'and' elasticnet', 'none', to compare their prediction performance on the test set (calculate score). Model2 = LogisticRegression (multi_class='ovr',penalty='l2',max_iter=10000) .fit (x_train, y_train) print (score of "penalty='l2'" +', model2.score (x_test, y_test)) # In [31]: model4 = LogisticRegression (multi_class='ovr',penalty='elasticnet',max_iter=10000,solver='saga',l1_ratio=0). Fit (x_train, y_train) # the problem of some parameters generated by model establishment is generally a problem of sklearn version. If you ignore it, just upgrade and update the version. Solver specifies the range of penalty parameters print ("penalty='elasticnet'" + 'score:', model4.score (x_test, y_test)) # In [32]: model5 = LogisticRegression (multi_class='ovr',penalty='none',max_iter=10000,solver='lbfgs') .fit (x_train, y_train) print ("penalty='none'" + 'score:', model5.score (x_test, y_test)). The study on "sample analysis of logical regression classification of Python spam" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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