当前位置: 首页>>代码示例>>Python>>正文


Python Matrix.getMatrix方法代码示例

本文整理汇总了Python中Matrix.Matrix.getMatrix方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.getMatrix方法的具体用法?Python Matrix.getMatrix怎么用?Python Matrix.getMatrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Matrix.Matrix的用法示例。


在下文中一共展示了Matrix.getMatrix方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: range

# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import getMatrix [as 别名]
enhancer = ImageEnhance.Contrast(im)

im = enhancer.enhance(2)
#
#im = im.convert('P')
#
#im.show()
#print im.format, im.mode, im.size 

#Lim = im
Lim = im.convert('L')  
#Lim.save('E:/Downloads/captchaxx.jpg')  
threshold = 80 
table = []  

for i in range(256):  
    if i < threshold: 
        table.append(0)  
    else:  
        table.append(1)  

bim = Lim.point(table, '1')  

m=Matrix(bim)
h=MatrixHandler(m.getMatrix())
h.doHandler()

#bim.show()
##
#Lim.save('captchaxxx.jpg')
# 
开发者ID:wangshiyang,项目名称:ReptiliaTest,代码行数:33,代码来源:ImageTest.py

示例2: LinearRegression

# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import getMatrix [as 别名]
class LinearRegression(object):
    def __init__(self, data=None, degree=1):
        """ initialize object
			@param	data: path to data file
			@param	degree: degree of polynomial to solve (e.g., quadratic)
		"""

        self.independent_variables = Matrix()
        self.dependent_variables = Matrix()
        self.coefficients = list()

        # make sure degree is at least 1
        if degree < 1:
            degree = 1

            # if data is specified, load it and set independent and dependent variables accordingly
        if data is not None:
            document = Document().open(filePath=data, splitLines=True, splitTabs=True)

            append_to_independent_variables = self.independent_variables.append
            append_to_dependent_variables = self.dependent_variables.append

            # loop through the rows in the document to get data
            for row in document:
                new_row = [float(value) for value in row]

                dependent_variable_row = [new_row[-1]]
                independent_variable_row = [new_row[0] ** i for i in xrange(degree + 1)]
                # print independent_variable_row, new_row, dependent_variable_row

                # append_to_independent_variables(new_row[:-1])
                append_to_independent_variables(independent_variable_row)
                # append_to_dependent_variables(new_row[-1:])
                append_to_dependent_variables(dependent_variable_row)

                # print self.independent_variables.matrix
            self.coefficients = self.getCoefficients([self.independent_variables, self.dependent_variables])

    def getCoefficients(self, data=None):
        """	solve for the coefficient weights from the data
			@param	data:	list of independent and dependent variables
			@return list of coefficients
		"""

        # if data is specified, set X and Y matrices to it
        if data is not None:
            X = data[0].getMatrix(True)
            Y = data[1].getMatrix(True)

            Xt = data[0].getMatrix(True)
            Xt.transpose()

            # set X and Y matrices to object's data if none specified
        else:
            X = self.independent_variables.getMatrix(True)
            Y = self.dependent_variables.getMatrix(True)

            Xt = self.independent_variables.getMatrix(True)
            Xt.transpose()

        XtX = (Xt * X).getInverse()
        XtY = Xt * Y

        coefficients = XtX * XtY
        coefficients.transpose()

        return coefficients[0]

    def getResiduals(self):
        residuals = list()
        append = residuals.append

        for i in xrange(len(self.independent_variables)):

            y = self.dependent_variables[i][0]
            y_estimated = 0

            for j in xrange(len(self.coefficients)):
                y_estimated += self.coefficients[j] * self.independent_variables[i][j]

            append(y - y_estimated)

        return residuals

    def getRSquared(self):
        mean = self.getMean(self.dependent_variables)

        residuals = self.getResiduals()

        residual_variation = [r ** 2 for r in residuals]
        total_variation = [(y[0] - mean) ** 2 for y in self.dependent_variables]

        return 1 - (sum(residual_variation) / sum(total_variation))

    def getCorrelationCoefficient(self):
        return math.sqrt(self.getRSquared())

    def getMean(self, data=None, predictor=0):
        if data is None:
            data = self.independent_variables
#.........这里部分代码省略.........
开发者ID:gabastil,项目名称:Python_MacBookAir_NLP_Cebuano_Scripts,代码行数:103,代码来源:LinearRegression.py


注:本文中的Matrix.Matrix.getMatrix方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。