资源简介

机器学习算法之线性回归 最小二乘法和岭回归算法的实现,对应博文为: 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\object_json.py

     文件       4100  2014-12-19 10:04  linear_regression\object_json.pyc

     文件       4833  2014-12-22 09:26  linear_regression\testlinearregress.py

     目录          0  2014-12-22 09:46  linear_regression

----------- ---------  ---------- -----  ----

               232057                    9


评论

共有 条评论