LFM推荐的例子和代码

参考自邹博的博客!
LFM:将评分矩阵分解为 item-feature 和 user-feature矩阵,feature数量事先人工确定,但是这两个矩阵参数未知,首先随机选取参数,再以此梯度下降迭代即可得到。
这里写图片描述

import pandas as pd
import numpy as np

def lfm(user_item,k,alpha = 0.01,lamda = 0.01):
    """user_item is matrix of user item,k is the number of latent number"""
    if not isinstance(user_item,list):
        raise("user item:{user_item} is not a matrix list!".format(user_item=user_item))
    mat = np.array(user_item)
    user_number,item_number = mat.shape
    # init the user and item latent matrix
    u = np.random.rand(user_number,k)
    v = np.random.rand(item_number,k)
    for it in range(1000):
        for i in range(user_number):
            for j in range(item_number):
                # err
                err = user_item[i][j] - np.dot(u[i],v[j])
                for r in range(k):
                    gu = err * v[j][r] + lamda * u[i][r]
                    gv = err * u[i][r] + lamda * v[j][r]
                    u[i][r] += alpha * gu
                    v[j][r] += alpha * gv
    return u,v
if __name__ == "__main__":
    mat = [[5,5,0,5],[5,0,3,4],[3,4,0,3],[0,0,5,3],[5,4,4,5],[5,4,5,5]]
    u,v = lfm(mat,3)
    print u
    print v
    print np.dot(u,v.T)

初始评分矩阵

[[5,5,0,5],[5,0,3,4],[3,4,0,3],[0,0,5,3],[5,4,4,5],[5,4,5,5]]

迭代后的u v 矩阵


[[ 0.18447051  2.09168616  0.8831042 ]
 [ 1.7912757   1.24549537 -0.48677593]
 [-0.1002325   1.29114151  0.86229546]
 [ 1.38787106 -0.70947744  0.76619629]
 [ 1.26487691  1.27983092  1.02112708]
 [ 1.48243041  1.08592233  1.13930052]]

[[ 1.30296976  2.33056037  0.22352664]
 [-0.38291845  1.51851172  2.39872099]
 [ 2.63593771 -0.74171661  1.47001405]
 [ 1.59681827  1.36696209  1.48520738]]

预测的结果


[[ 5.31255767  5.22393338  0.23298999  4.46541444]
 [ 5.12787282  0.03774717  3.08231911  3.83992351]
 [ 3.07122934  4.06740062  0.04571872  2.88557601]
 [ 0.3261393   0.22909989  5.31089216  2.38430948]
 [ 4.85906868  3.9084925   3.88593604  5.28584437]
 [ 4.71703356  3.81419992  4.77693537  5.54368417]]

结果接近,效果还行。
如果来一个未对item1评分的user1,但是user1和item1的feature我们知道即上面的u1和v1,只需要u1 * v1.T即可得到5.31. 这样就可以有针对性的进行推荐了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是LFM模糊函数的MATLAB代码: ```matlab function [h, t] = lfm_fuzzy(f0, bw, Tp, Fs, N) % LFM_FUZZY - Generates a linear frequency modulated (LFM) fuzzy waveform % with a Gaussian amplitude distribution. % % Syntax: % [h, t] = lfm_fuzzy(f0, bw, Tp, Fs, N) % % Inputs: % f0 - Starting frequency of the LFM waveform (Hz). % bw - Bandwidth of the LFM waveform (Hz). % Tp - Pulse duration (s). % Fs - Sampling frequency (Hz). % N - Number of samples in the waveform. % % Outputs: % h - Generated LFM waveform. % t - Time vector for the waveform. % % Example: % [h, t] = lfm_fuzzy(10e6, 5e6, 10e-6, 100e6, 1024); % plot(t, abs(h)); % % Reference: % Skolnik, M. (2001). Introduction to Radar Systems (3rd ed.). New York: % McGraw-Hill. % % Author: Jianhua Zhou, Ph.D. % Email: [email protected] % Website: https://www.researchgate.net/profile/Jianhua_Zhou2 % Date: 2021.08.17 % Calculate the chirp rate. K = bw / Tp; % Calculate the time-bandwidth product. TB = bw * Tp; % Calculate the standard deviation of the Gaussian amplitude distribution. sigma = TB / (2 * sqrt(log(2))); % Generate the time vector. t = linspace(0, N/Fs, N); % Generate the LFM waveform. h = exp(-1j*(2*pi*f0*t + pi*K.*t.^2)); % Apply the Gaussian amplitude distribution. h = h .* exp(-(t - Tp/2).^2 / (2*sigma^2)); end ``` 这个函数可以生成一个带有高斯幅度分布的线性调频(LFM)“模糊”波形。LFM波形在雷达信号处理中经常使用,而高斯幅度分布则可以模拟目标的反射信号在时间和频率上的变化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值