资源简介
机器学习算法之线性回归 最小二乘法和岭回归算法的实现,对应博文为:
http://blog.csdn.net/suipingsp/article/details/42101139
代码片段和文件信息
‘‘‘Created on Jun06 2014
Linear Regression algorithm support ordinary least squares ridge Regression
andetc.
@author: Aidan
‘‘‘
from numpy import *
from object_json import *
from copy import *
import pdb
import operator
class linearRegress(object):
def __init__(selfLRDict = None **args):
‘‘‘currently support OLS ridge LWLR
‘‘‘
obj_list = inspect.stack()[1][-2]
self.__name__ = obj_list[0].split(‘=‘)[0].strip()
if not LRDict:
self.LRDict = {}
else:
self.LRDict = LRDict
#to Numpy matraix
if ‘OLS‘ in self.LRDict:
self.LRDict[‘OLS‘] = mat(self.LRDict[‘OLS‘])
if ‘ridge‘ in self.LRDict:
self.LRDict[‘ridge‘][0] = mat(self.LRDict[‘ridge‘][0])
self.LRDict[‘ridge‘][2] = mat(self.LRDict[‘ridge‘][2])
self.LRDict[‘ridge‘][3] = mat(self.LRDict[‘ridge‘][3])
self.LRDict[‘ridge‘][4] = mat(self.LRDict[‘ridge‘][4])
def jsonDumpsTransfer(self):
‘‘‘essential transformation to Python basic type in order to
store as json. dumps as objectname.json if filename missed ‘‘‘
#pdb.set_trace()
if ‘OLS‘ in self.LRDict:
self.LRDict[‘OLS‘] = self.LRDict[‘OLS‘].tolist()
if ‘ridge‘ in self.LRDict:
self.LRDict[‘ridge‘][0] = self.LRDict[‘ridge‘][0].tolist()
self.LRDict[‘ridge‘][2] = self.LRDict[‘ridge‘][2].tolist()
self.LRDict[‘ridge‘][3] = self.LRDict[‘ridge‘][3].tolist()
self.LRDict[‘ridge‘][4] = self.LRDict[‘ridge‘][4].tolist()
def jsonDumps(self filename=None):
‘‘‘dumps to json file‘‘‘
self.jsonDumpsTransfer()
if not filename:
jsonfile = self.__name__+‘.json‘
else: jsonfile = filename
objectDumps2File(self jsonfile)
def jsonloadTransfer(self):
‘‘‘essential transformation to object required type such as numpy matrix
call this function after newobject = objectLoadFromFile(jsonfile)‘‘‘
#pdb.set_trace()
if ‘OLS‘ in self.LRDict:
self.LRDict[‘OLS‘] = mat(self.LRDict[‘OLS‘])
if ‘ridge‘ in self.LRDict:
self.LRDict[‘ridge‘][0] = mat(self.LRDict[‘ridge‘][0])
self.LRDict[‘ridge‘][2] = mat(self.LRDict[‘ridge‘][2])
self.LRDict[‘ridge‘][3] = mat(self.LRDict[‘ridge‘][3])
self.LRDict[‘ridge‘][4] = mat(self.LRDict[‘ridge‘][4])
def solver_OLS(self xMat yMat):
x0Mat = mat(ones((xMat.shape[0]1)))
xMat = hstack((x0Mat xMat))#extend x0=1 for each sample
xTx = xMat.T*xMat
if linalg.det(xTx) == 0.0:
print “This matrix is singular cannot do inverse“
#raise
else:
self.LRDict[‘OLS‘] = xTx.I * (xMat.T*yMat)
def solver_ridg
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 197357 2011-01-19 17:17 linear_regression\abalone.txt
文件 5600 2011-01-08 10:02 linear_regression\ex0.txt
文件 5600 2011-01-18 17:18 linear_regression\ex1.txt
文件 5850 2014-12-22 09:38 linear_regression\linearregress.py
文件 5928 2014-12-22 09:26 linear_regression\linearregress.pyc
文件 2789 2014-12-01 12:11 linear_regression\ob
文件 4100 2014-12-19 10:04 linear_regression\ob
文件 4833 2014-12-22 09:26 linear_regression\testlinearregress.py
目录 0 2014-12-22 09:46 linear_regression
----------- --------- ---------- ----- ----
232057 9
- 上一篇:ocra-1.3.1
- 下一篇:基于FPGA的幅度可调信号发生器
相关资源
- 动漫知识图谱代码及关系文件
- k-means训练
- 易班自动答题脚本全版
- 栅格变化趋势,一元线性回归
- 疝气病数据集逻辑回归
- 17 机器学习案例——基于朴素贝叶斯
- opencv人脸识别源码
- 人脸识别--课堂点名
- spark机器学习简单文档
- 机器学习特征的的代码lpq
- 图解机器学习代码
- 南京大学周志华老师的一个讲普适机
- 最新版coursera吴恩达机器学习全套视频
- 吴恩达机器学习视频中英文字幕字幕
- 机器学习Kmeans实验报告
- 贝叶斯分类器实现手写数字识别
- 吴恩达机器学习视频百度网盘视频+
- 机器学习 吴恩达 全套视频和课后题
- 中文停止词库
- django web通讯
- Web安全三部曲随书源码
- 机器学习数据预处理葡萄酒数据集w
- 炼数数据挖掘视频教程
- 读取json文件切割图集
- 机器学习:猫和狗的图片训练图片2
- kaggle入门-Titanic
- 信用风险评分卡研究_基于SAS的开发与
- PRML学习笔记之Neural Nerwork神经网络
- 豆瓣电影爬虫、豆瓣电影推荐系统
- 数据挖掘与机器学习.pdf
评论
共有 条评论