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

How to analyze and practice the principle of FM algorithm

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how to analyze and practice the principle of FM algorithm, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Brief introduction

FM was proposed by Steffen Rendle in 2010. The core of FM algorithm is feature combination, so as to reduce manual participation in feature combination. For FM, its advantages can be divided into three points:

FM can handle scenarios with highly sparse data, while SVM cannot

FM has linear computational complexity, while SVM depends on support vector.

FM can work in any eigenvector of real numbers.

FM principle

The data structure of FM is as follows

FM generates new meanings through the combination of different features. However, feature combination also brings some problems:

Pairwise combinations of features can easily lead to dimensional disaster.

The combined features may not be effective, and there may be feature redundancy.

After the combination, the feature samples are very sparse, if there is no corresponding combination in the original sample, then the parameters can not be learned, then the combination appears to be invalid.

Although there are these shortcomings, but it does not affect the status of FM in the field of advertising recommendation, each algorithm has a popular past, with an awe attitude to learn is no problem. Next, let's take a look at the algorithm principle of FM.

Objective function

We know that the objective function of the linear model is:

Classification model

FM can be used for classification. For convenience, we use the iris data set in sklearn as experimental data, the target equal to 2 as positive samples and the rest as negative samples, and use train_test_split method to divide the training set and test set, then build the classification model through FM, and verify the effect of FM through the test set. The complete Demo code is as follows

From sklearn.datasets import load_irisfrom sklearn.model_selection import train_test_splitfrom pyfm import pylibfmfrom sklearn.feature_extraction import DictVectorizer

Def load_data (): "calls the iris dataset of sklearn Select the positive and negative samples and construct the segmentation training test data set "" iris_data = load_iris () X = iris_data ['data'] y = iris_data [' target'] = = 2 data = [{v: k for k, v in dict (zip (I, range (len (I). Items ()} for i in X] X training test data set "", y_test = train_test_split (data,y Test_size=0.3, random_state=0) return Xanthology, y_test, y_test

Y_test = load_data ()

V = DictVectorizer () X_train = v.fit_transform (X_train) X_test = v.transform (X_test)

Fm = pylibfm.FM (num_factors=2, num_iter=200, verbose=True, task= "classification", initial_learning_rate=0.001, learning_rate_schedule= "optimal")

Fm.fit (X_train, y_train)

Y_preds = fm.predict (X_test) y_preds_label = y_preds > 0.5from sklearn.metrics import log_loss,accuracy_scoreprint ("Validation log loss:% .4f"% log_loss (y_test, y_preds)) print ("accuracy:% .4f"% accuracy_score (y_test, y_preds_label)) experimental results

Through the above code, the results are as follows (Note: the results of each experiment are not necessarily the same):

Training log loss: 0.12161Validation log loss: 0.1868accuracy: 0.9778 the above content is how to analyze and practice the principle of FM algorithm. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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

Internet Technology

Wechat

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

12
Report