9.5 程序示例--推荐系统-机器学习笔记-斯坦福吴恩达教授
生活随笔
收集整理的這篇文章主要介紹了
9.5 程序示例--推荐系统-机器学习笔记-斯坦福吴恩达教授
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
程序示例–推薦系統(tǒng)
推薦模型
在推薦模型中,我們將暴露:
- 訓(xùn)練接口 train()
- 預(yù)測接口 predict(Theta, X)
- 獲得推薦接口 getTopRecommends(Theta, X, i, count, rated, items):
并且,使用 scipy.optimize.minimize 來最小化代價函數(shù):
# coding: utf8 # recommender_system/recommender.py import numpy as np from scipy.optimize import minimize, check_grad from pydash import py_def getRecommender(Y, R, params=None, n=10, theLambda=10, maxIter=100):"""訓(xùn)練方法Args:Y 評價矩陣R 是否評價矩陣params 是否具有初始化參數(shù)n 商品特征數(shù)theLambda 正規(guī)化參數(shù)maxIter 最大迭代次數(shù)Returns:train 訓(xùn)練方法predict 預(yù)測方法"""# 商品數(shù),用戶數(shù)nm, nu = Y.shape# normalize YMeanmu = np.mean(Y, axis=0)mu = np.zeros((Y.shape[0], 1), dtype=np.float)for i in range(nm):totalRates = np.sum(Y[i])validCount = len(np.nonzero(R[i])[0])mu[i] = totalRates / validCountY = Y - mudef unroll(Theta, X):"""參數(shù)折疊Args:Theta 用戶偏好矩陣X 商品內(nèi)容矩陣Returns:vector 折疊后的參數(shù)"""return np.hstack((X.A.T.flatten(), Theta.A.T.flatten()))def roll(vector):"""參數(shù)回復(fù)Args:vector 參數(shù)向量Returns:Theta 用戶偏好矩陣X 商品內(nèi)容矩陣"""X = np.mat(vector[:nm * n].reshape(n, nm).T)Theta = np.mat(vector[nm * n:].reshape(n, nu).T)return Theta, Xdef initParams():"""初始化參數(shù)Returns:Theta 用戶對內(nèi)容的偏好矩陣X 商品內(nèi)容矩陣"""Theta = np.mat(np.random.rand(nu, n))X = np.mat(np.random.rand(nm, n))return Theta, Xdef regParams(param):"""正規(guī)化參數(shù)Args:param 參數(shù)Return:regParam 正規(guī)化后的參數(shù)"""return theLambda * 0.5 * np.sum(np.power(param, 2))def J(params):"""代價函數(shù)Args:params 參數(shù)向量nu 用戶數(shù)nm 商品數(shù)n 特征數(shù)Return:J 預(yù)測代價"""# 參數(shù)展開Theta, X = roll(params)# 計算誤差rows, cols = np.nonzero(R)# 預(yù)測H = predict(Theta, X)Diff = H - YDiff[R != 1] = 0error = 0.5 * np.sum(np.power(Diff, 2))# 正規(guī)化 ThetaregTheta = regParams(Theta)# 正規(guī)化 xregX = regParams(X)return error + regTheta + regXdef gradient(params):"""梯度下降A(chǔ)rgs:params 參數(shù)向量Returns:grad 梯度向量"""Theta, X = roll(params)ThetaGrad = np.mat(np.zeros(Theta.shape))XGrad = np.mat(np.zeros(X.shape))error = predict(Theta, X) - Yerror[R != 1] = 0ThetaGrad = error.T * X + theLambda * ThetaXGrad = error * Theta + theLambda * Xreturn unroll(ThetaGrad, XGrad)def train():"""訓(xùn)練方法Returns:Theta 用戶的偏好矩陣X 商品的內(nèi)容矩陣"""# 初始化參數(shù)if not params:Theta, X = initParams()else:Theta = params['Theta']X = params['X']# 最小化目標(biāo)函數(shù)res = minimize(J, x0=unroll(Theta, X), jac=gradient,method='CG', options={'disp': True, 'maxiter': maxIter})Theta, X = roll(res.x)return Theta, Xdef predict(Theta, X):"""預(yù)測Args:Theta 用戶對內(nèi)容的偏好矩陣X 商品內(nèi)容矩陣Return:h 預(yù)測"""return X * Theta.T + mudef getTopRecommends(Theta, X, i, count, rated, items):"""獲得推薦Args:Theta ThetaX Xi 用戶下標(biāo)count 獲得推薦的數(shù)目rated 已經(jīng)評價的類目iditems 商品清單Returns:topRecommends 推薦項目"""predictions = predict(Theta, X)[:, i]return py_(items) \.map(lambda item, idx: (item, predictions[idx])) \.sort_by(lambda item: item[1], reverse = True) \.take(count) \.value()return train, predict, getTopRecommends測試
我們選取了一份電影打分?jǐn)?shù)據(jù),來訓(xùn)練推薦系統(tǒng)。并且,我們新建了一個用戶,并且評價了一些電影:
New user ratings: Rated 4 for Toy Story (1995) Rated 3 for Twelve Monkeys (1995) Rated 5 for Usual Suspects, The (1995) Rated 4 for Outbreak (1995) Rated 5 for Shawshank Redemption, The (1994) Rated 3 for While You Were Sleeping (1995) Rated 5 for Forrest Gump (1994) Rated 2 for Silence of the Lambs, The (1991) Rated 4 for Alien (1979) Rated 5 for Die Hard 2 (1990) Rated 5 for Sphere (1998)系統(tǒng)的訓(xùn)練結(jié)果如下:
Traing Result: Warning: Maximum number of iterations has been exceeded.Current function value: 71102.253824Iterations: 100Function evaluations: 156Gradient evaluations: 156系統(tǒng)向新用戶推薦的內(nèi)容為:
Top recommendations for you: Predicting rating 4.5 for movie Great Day in Harlem, A (1994) Predicting rating 4.1 for movie Someone Else's America (1995) Predicting rating 3.9 for movie Aiqing wansui (1994) Predicting rating 3.7 for movie Entertaining Angels: The Dorothy Day Story (1996) Predicting rating 3.7 for movie Marlene Dietrich: Shadow and Light (1996) Predicting rating 3.5 for movie They Made Me a Criminal (1939) Predicting rating 3.5 for movie Santa with Muscles (1996) Predicting rating 3.5 for movie Tokyo Fist (1995) Predicting rating 3.4 for movie Star Kid (1997) Predicting rating 3.3 for movie Window to Paris (1994)總結(jié)
以上是生活随笔為你收集整理的9.5 程序示例--推荐系统-机器学习笔记-斯坦福吴恩达教授的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 9.4 均值标准化-机器学习笔记-斯坦福
- 下一篇: 10.1 掌握大数据-机器学习笔记-斯坦