线性分类器——parameter learning
来源:互联网 发布:php apc缓存 编辑:程序博客网 时间:2024/06/07 02:02
本内容整理自coursera,欢迎交流转载。
1 如何确定系数矩阵
我们可以用likelihood l(w)来观测系数矩阵的分类质量。
2 Data likelihood
MLE(maximum likelihood estimation)
注意:这里计算概率时需要注意真值y是+1还是-1,相应的我们在计算的时候需要使用对应的概率。
计算完上述内容之后,我们给出评价模型好坏的公式:
我们希望可以得到尽可能大的
3 梯度上升(gradient ascend method)
回忆之前学过的梯度下降法,类比之我们可以得到梯度上升法的一般形式是:
4 log-likelihood 的导数
其中:
之后,我们描述一下算法流程:
首先,init
4-** 导数公式如何得到(选学)
首先,复习一些基础代数知识。
我们已经得到:
现在,为了简化之后的运算,我们规定:
这里我们使用了一个性质,那就是对一个函数做对数变换,不影响函数取最大值的点,不影响我们求救最优系数矩阵。
首先,根据前面提到的基础代数知识,我们得到:
有之前一篇博客提到过得:
得知:
由此化简
则:
然后把所有的相加得到:
5 如何确定步长η
因此我们可以先确定一个比较大的和比较小的
如果可以的话,我们可以一开始选择一个比较大的
6 代码实现
可以在这里下载代码和数据文件.
import graphlabproducts = graphlab.SFrame('amazon_baby_subset.gl/')import jsonwith open('important_words.json', 'r') as f: # Reads the list of most frequent words important_words = json.load(f)important_words = [str(s) for s in important_words]#去掉标点符号def remove_punctuation(text): import string return text.translate(None, string.punctuation) products['review_clean'] = products['review'].apply(remove_punctuation)#为每个important words 建立一列,表示出现的次数for word in important_words: products[word] = products['review_clean'].apply(lambda s : s.split().count(word))#问题1products['contains_perfect'] = products['perfect']>=1products['contains_perfect']sum(products['contains_perfect'])import numpy as np#转化为numpy array格式def get_numpy_data(data_sframe, features, label): data_sframe['intercept'] = 1 features = ['intercept'] + features features_sframe = data_sframe[features] feature_matrix = features_sframe.to_numpy() label_sarray = data_sframe[label] label_array = label_sarray.to_numpy() return(feature_matrix, label_array)# Warning: This may take a few minutes...feature_matrix, sentiment = get_numpy_data(products, important_words, 'sentiment') '''produces probablistic estimate for P(y_i = +1 | x_i, w).estimate ranges between 0 and 1.'''import mathdef predict_probability(feature_matrix, coefficients): # Take dot product of feature_matrix and coefficients # YOUR CODE HERE score=np.dot(feature_matrix, coefficients) # Compute P(y_i = +1 | x_i, w) using the link function # YOUR CODE HERE predictions = 1/(1+math.e**(-score)) # return predictions return predictionsdef feature_derivative(errors, feature): # Compute the dot product of errors and feature derivative =np.dot(errors, feature) # Return the derivative return derivativedef compute_log_likelihood(feature_matrix, sentiment, coefficients): indicator = (sentiment==+1) scores = np.dot(feature_matrix, coefficients) logexp = np.log(1. + np.exp(-scores)) # Simple check to prevent overflow mask = np.isinf(logexp) logexp[mask] = -scores[mask] lp = np.sum((indicator-1)*scores - logexp) return lpfrom math import sqrtdef logistic_regression(feature_matrix, sentiment, initial_coefficients, step_size, max_iter): coefficients = np.array(initial_coefficients) # make sure it's a numpy array for itr in xrange(max_iter): # Predict P(y_i = +1|x_i,w) using your predict_probability() function # YOUR CODE HERE predictions = predict_probability(feature_matrix,coefficients) # Compute indicator value for (y_i = +1) indicator = (sentiment==+1) # Compute the errors as indicator - predictions errors = indicator - predictions for j in xrange(len(coefficients)): # loop over each coefficient # Recall that feature_matrix[:,j] is the feature column associated with coefficients[j]. # Compute the derivative for coefficients[j]. Save it in a variable called derivative # YOUR CODE HERE derivative = feature_derivative(errors,feature_matrix[:,j]) # add the step size times the derivative to the current coefficient ## YOUR CODE HERE coefficients[j]+=step_size*derivative # Checking whether log likelihood is increasing if itr <= 15 or (itr <= 100 and itr % 10 == 0) or (itr <= 1000 and itr % 100 == 0) \ or (itr <= 10000 and itr % 1000 == 0) or itr % 10000 == 0: lp = compute_log_likelihood(feature_matrix, sentiment, coefficients) print 'iteration %*d: log likelihood of observed labels = %.8f' % \ (int(np.ceil(np.log10(max_iter))), itr, lp) return coefficients
- 线性分类器——parameter learning
- Distributed Machine Learning —— Parameter Server
- Andrew NG 《machine learning》week 2,class6 —Parameter learning
- 线性分类器——Overfitting & regularization
- Deep Learning 1:简单线性分类
- cs231n笔记(2)——线性分类器
- 模式识别(Pattern Recognition)学习笔记(七)——线性分类器及线性判别函数
- 模式识别(Pattern Recognition)学习笔记(七)——线性分类器及线性判别函数
- PRML读书笔记——线性分类模型
- 线性分类器
- 线性分类器
- 模式识别: 线性分类器
- 线性分类器
- 线性分类器设计
- MATLAB线性分类器
- 线性分类器
- 线性分类器
- cs231n-线性分类器
- Android Gatt连接流程源码分析之ClientIf注册
- Android开发-拍照录像-AndroidStudio(二)VCamera拍摄小视频初体验
- 在js(jquery)中获得文本框焦点和失去焦点的方法
- PAT (Top Level) Practise 1013 Image Segmentation (35)
- Eclipse中Web开发需要用的一些插件的安装方法
- 线性分类器——parameter learning
- 后台检查codis集群节点工具
- 退出App你应该做些什么
- 不要一辈子靠技术生存
- 使用 Chrome Timeline 来优化页面性能
- 【ViewPager的学习】实现自动循环切换
- Java中多线程互斥访问的实现
- IDEA(jetbrain通用)优雅级使用教程
- 程序员该如何规划自己的人生