一、感知机模型

二、线性回归(Linear Regression)

  1. from numpy import *
  2.  
  3. def loadData(filename):
  4. x = []
  5. y = []
  6. f = open(filename)
  7. for line in f.readlines():
  8. lineData = line.strip().split(',')
  9. x.append([1.0,float(lineData[0])])
  10. y.append(float(lineData[1]))
  11. return x,y
  12.  
  13. #预测函数,theta,x都是一维数组,dot运算得到实数,对于二维数组,dot运算就是矩阵运算
  14. def h(theta,x):
  15. return theta.dot(x)
  16.  
  17. #批量梯度下降
  18. def batch_gradient_descent(alpha,theta,x,y):
  19. m,n = x.shape
  20. newtheta = array([0] * n,dtype = float)
  21. for j in range(n):
  22. count = 0.0
  23. for i in range(m):
  24. count += (h(theta,x[i,:]) - y[i])*x[i,j]
  25. newtheta[j] = newtheta[j] - count * alpha / m
  26. return newtheta
  27.  
  28. #正则方程
  29. def normal_equation(x,y):
  30. return linalg.inv(transpose(x).dot(x)).dot(transpose(x)).dot(y)
  31.  
  32. #损失函数
  33. def cost_function(theta,x,y):
  34. m = x.shape[0]
  35. return (x.dot(theta) - y).dot(x.dot(theta) - y) / (2 * m)
  36.  
  37. def run():
  38. x,y = loadData('ex1data1.txt')
  39. x = array(x)
  40. y = array(y) #列向量
  41. m,n = x.shape
  42. theta = array([0] * n,dtype = float)
  43. costs = []
  44. for iters in range(1000):
  45. costs.append(cost_function(theta,x,y))
  46. theta = batch_gradient_descent(0.01,theta,x,y)
  47. print "batch gradient descent:\n"
  48. print "theta:",theta
  49. print 'cost:\n',costs
  50.  
  51. print "normal equation:\n"
  52. theta = normal_equation(x,y)
  53. print "theta:",theta
  54.  
  55. if __name__ == "__main__":
  56. run()

三、Logistic Regression

  1. def sigmoid(x):
  2. return 1.0/(1 + exp(-x))
  3.  
  4. def trainLogRegres(x,y,opts):
  5. m,n = x.shape
  6. alpha = opts["alpha"]
  7. maxIter = opts['maxIter']
  8. weight = ones((n,1))
  9.  
  10. for k in range(maxIter):
  11. if opts['optimizeType'] == 'batchGraDescent':
  12. weight = weight - alpha * x.T * (sigmoid(x*weight) - y)
  13. elif opts['optimizeType'] == 'stocGraDescent':
  14. for i in range(m):
  15. weight = weight - alpha * x[i,:].T * (sigmoid(x[i,:] * weight) - y[i,0])
  16. else:
  17. raise NameError('Not support optimize method type!')
  18.  
  19. return weight
  20.  
  21. def testLogRegres(weight,x,y):
  22. m,n = x.shape
  23. trueNum = 0
  24. for i in range(m):
  25. predict = sigmoid(x[i,:] * weight)[0,0] > 0.5
  26. if predict == bool(y[i,0]):
  27. trueNum += 1
  28. accuracy = float(trueNum) / m
  29. return accuracy
  30.  
  31. #x每行对应一个样本,y是列向量
  32. def loadData():
  33. x = []
  34. y = []
  35. f = open("testSet.txt")
  36. for line in f.readlines():
  37. lineArr = line.strip().split()
  38. x.append([1.0, float(lineArr[0]), float(lineArr[1])])
  39. y.append(float(lineArr[2]))
  40. return mat(x),mat(y).T
  41.  
  42. if __name__ == '__main__':
  43. x,y = loadData()
  44. opts = {'alpha': 0.01, 'maxIter': 50, 'optimizeType': 'stocGraDescent'}
  45. weight = trainLogRegres(x,y,opts)
  46. accuracy = testLogRegres(weight,x,y)
  47. print "accuracy:",accuracy

四、SVM

五、kmeans

https://en.wikipedia.org/wiki/Latent_semantic_analysis

常见machine learning模型实现的更多相关文章

  1. 机器学习---最小二乘线性回归模型的5个基本假设(Machine Learning Least Squares Linear Regression Assumptions)

    在之前的文章<机器学习---线性回归(Machine Learning Linear Regression)>中说到,使用最小二乘回归模型需要满足一些假设条件.但是这些假设条件却往往是人们 ...

  2. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  3. 【机器学习Machine Learning】资料大全

    昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...

  4. Machine Learning Algorithms Study Notes(4)—无监督学习(unsupervised learning)

    1    Unsupervised Learning 1.1    k-means clustering algorithm 1.1.1    算法思想 1.1.2    k-means的不足之处 1 ...

  5. Machine Learning Algorithms Study Notes(2)--Supervised Learning

    Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 22 ...

  6. 机器学习(Machine Learning)&深度学习(Deep Learning)资料

    <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...

  7. FAQ: Machine Learning: What and How

    What: 就是将统计学算法作为理论,计算机作为工具,解决问题.statistic Algorithm. How: 如何成为菜鸟一枚? http://www.quora.com/How-can-a-b ...

  8. 机器学习(Machine Learning)&深入学习(Deep Learning)资料

    <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost 到随机森林. ...

  9. Machine Learning - 第6周(Advice for Applying Machine Learning、Machine Learning System Design)

    In Week 6, you will be learning about systematically improving your learning algorithm. The videos f ...

随机推荐

  1. oracle的Hint

    与优化器模式相关的Hint 1 ALl_ROWS   让优化器启用CBO /*+ all_rows */ 2  first_rows(n)     让优化器启用CBO 模式,而且得到目标sql的执行计 ...

  2. Wikidata和SparQL简介

    知识库 数据库(Database)和SQL,相信我们大部分人都非常非常熟悉.但是“知识库”可能知道的人就要相对少一些. 知识库是一个相对比较新的概念,它其实是一堆“三元组”(类似于主-谓-宾)的组合, ...

  3. python爬虫---从零开始(三)Requests库

    1,什么是Requests库 Requests是用python语言编写,基于urllib,采用Apache2 Licensed 开源协议的HTTP库. 它比urllib更加方便,可以节约我们大量的工作 ...

  4. VS打开文件,解决方案资源管理器自动定位到文件位置

    打开 工具-->选项-->项目和解决方案-->常规,勾选“在解决方案资源管理器中跟踪活动项”

  5. mac 监控文件变化并重启php

    自己撸一个框架,需要监控代码变化 安装fswatch brew install fswatch shell重启PHP脚本reload.sh #!/bin/sh do ps -ef | grep php ...

  6. solr之windws下搭建solr服务

    安装Solr 首先保证已经正确安装了Java 下载Solr,当前最新版6.1.0 Solr各个版本下载地址 Solr从6.0之后需要Java1.8所以如果使用Solr6.0及其以上版本,请确保Java ...

  7. 激活windows10(已更新工具)

    激活windows10 1.用cmd命令: 自己动手,KMS激活win10 2016 长期服务版.步骤如下:命令提示符(管理员),依次输入以下3条命令 slmgr /ipk DCPHK-NFMTC-H ...

  8. ArrayList练习之存储字符串并遍历

    在myArrayList项目下 新建一个包 在这个包中新建一个类:ArrayListDemo4.java ArrayListDemo4.java import java.util.ArrayList; ...

  9. Druid连接池简单配置

    Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池.插件框架和SQL解析器组成.该项目主要是为了扩展JDBC的一些限制,可以让程序员实现一些特殊的需求,比如向密钥服务请求凭证.统计SQL ...

  10. Apache手册

    一.apache的安装 如果不指定安装位置,默认为/usr/local/apache2/