一、感知机模型

二、线性回归(Linear Regression)

from numpy import *

def loadData(filename):
x = []
y = []
f = open(filename)
for line in f.readlines():
lineData = line.strip().split(',')
x.append([1.0,float(lineData[0])])
y.append(float(lineData[1]))
return x,y #预测函数,theta,x都是一维数组,dot运算得到实数,对于二维数组,dot运算就是矩阵运算
def h(theta,x):
return theta.dot(x) #批量梯度下降
def batch_gradient_descent(alpha,theta,x,y):
m,n = x.shape
newtheta = array([0] * n,dtype = float)
for j in range(n):
count = 0.0
for i in range(m):
count += (h(theta,x[i,:]) - y[i])*x[i,j]
newtheta[j] = newtheta[j] - count * alpha / m
return newtheta #正则方程
def normal_equation(x,y):
return linalg.inv(transpose(x).dot(x)).dot(transpose(x)).dot(y) #损失函数
def cost_function(theta,x,y):
m = x.shape[0]
return (x.dot(theta) - y).dot(x.dot(theta) - y) / (2 * m) def run():
x,y = loadData('ex1data1.txt')
x = array(x)
y = array(y) #列向量
m,n = x.shape
theta = array([0] * n,dtype = float)
costs = []
for iters in range(1000):
costs.append(cost_function(theta,x,y))
theta = batch_gradient_descent(0.01,theta,x,y)
print "batch gradient descent:\n"
print "theta:",theta
print 'cost:\n',costs print "normal equation:\n"
theta = normal_equation(x,y)
print "theta:",theta if __name__ == "__main__":
run()

三、Logistic Regression

def sigmoid(x):
return 1.0/(1 + exp(-x)) def trainLogRegres(x,y,opts):
m,n = x.shape
alpha = opts["alpha"]
maxIter = opts['maxIter']
weight = ones((n,1)) for k in range(maxIter):
if opts['optimizeType'] == 'batchGraDescent':
weight = weight - alpha * x.T * (sigmoid(x*weight) - y)
elif opts['optimizeType'] == 'stocGraDescent':
for i in range(m):
weight = weight - alpha * x[i,:].T * (sigmoid(x[i,:] * weight) - y[i,0])
else:
raise NameError('Not support optimize method type!') return weight def testLogRegres(weight,x,y):
m,n = x.shape
trueNum = 0
for i in range(m):
predict = sigmoid(x[i,:] * weight)[0,0] > 0.5
if predict == bool(y[i,0]):
trueNum += 1
accuracy = float(trueNum) / m
return accuracy #x每行对应一个样本,y是列向量
def loadData():
x = []
y = []
f = open("testSet.txt")
for line in f.readlines():
lineArr = line.strip().split()
x.append([1.0, float(lineArr[0]), float(lineArr[1])])
y.append(float(lineArr[2]))
return mat(x),mat(y).T if __name__ == '__main__':
x,y = loadData()
opts = {'alpha': 0.01, 'maxIter': 50, 'optimizeType': 'stocGraDescent'}
weight = trainLogRegres(x,y,opts)
accuracy = testLogRegres(weight,x,y)
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. c语言 预处理的使用 宏展开下的#,##

    1. #include   包含头文件 2.define 宏定义(可以理解为替换,不进行语法检查) 写法 #define 宏名 宏体  加括号 #define ABC (5+3) #define AB ...

  2. IoC简介

    控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来降低程序代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency Injecti ...

  3. python爬虫---实现项目(二) 分析Ajax请求抓取数据

    这次我们来继续深入爬虫数据,有些网页通过请求的html代码不能直接拿到数据,我们所需的数据是通过ajax渲染到页面上去的,这次我们来看看如何分析ajax 我们这次所使用的网络库还是上一节的Reques ...

  4. [LUOGU] P1962 斐波那契数列

    求斐波那契第n项. [f(n-1) f(n)] * [0,1] = [f(n) f(n+1)] [1,1] 由此原理,根据矩阵乘法的结合律,用快速幂算出中间那个矩阵的n次方即可. 快速幂本质和普通快速 ...

  5. Java Web中的编码解析

    在springmvc工程web.xml中配置中文编码 <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码--> <filter> <filter-name&g ...

  6. Django之学员管理

    Django之学员管理 实现-------在前端页面提交的数据,后端可直接写入数据库.在页面实现操作数据库的增删改查. 数据表设计:(三个角色四张表) 班级表: id title 1 花果山国小一年级 ...

  7. 【笔记】linux x86漏洞利用

    0x1任意代码执行是如何实现的? 任意代码执行使用一种叫“覆盖返回地址”的技术来实现.这种方式使得攻击者重写位于栈上的返回地址,这将导致任意代码执行.

  8. 【转】关于RabbitMQ

    1      什么是RabbitMQ? RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不 ...

  9. XTUOJ 15503 - C

    15503 - C Accepted: 6    Submissions: 27    Time Limit: 3000 ms    Memory Limit: 1048576 KB 在解决了小女孩的 ...

  10. Flask设计带认证token的RESTful API接口[翻译]

    上一篇文章, 使用python的Flask实现一个RESTful API服务器端  简单地演示了Flask实的现的api服务器,里面提到了因为无状态的原则,没有session cookies,如果访问 ...