本文整理汇总了Python中Matrix.Matrix.scaler方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.scaler方法的具体用法?Python Matrix.scaler怎么用?Python Matrix.scaler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Matrix.Matrix
的用法示例。
在下文中一共展示了Matrix.scaler方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: power
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import scaler [as 别名]
def power(self, iterations, mat, estimate):
r = 1
k = 0
m = iterations
y = estimate
x = mat.multiply(y)
while r > .001 and k < m:
max_x = x.find_max()
x.scaler(1 / max_x)
y = Matrix(x.get_data())
x = mat.multiply(y)
temp = Matrix(y.get_data())
temp.transpose()
a = temp.multiply(x).get_data()[0][0]
b = temp.multiply(y).get_data()[0][0]
mu = a / b
r = Matrix(y.get_data())
r.scaler(mu)
r = r.subtract(x)
r = r.find_max()
k += 1
y.scaler(1 / mu)
return mu, mat.trace() - mu, y
示例2: __init__
# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import scaler [as 别名]
class Operations:
def __init__(self):
self.data = []
self.matrices = []
self.mean = Matrix([[0, 0]])
self.covariance = Matrix([[0, 0], [0, 0]])
self.setup()
self.set_mean()
self.set_covariance()
print(len(self.matrices))
def get_mean(self):
return self.mean
def get_covariance(self):
return self.covariance
# Read in and prepare the eigendata from a file
def setup(self):
temp = []
filename = open('eigendata.txt', 'r')
for line in filename:
row = line.strip()
temp.append(row)
for i in range(0, len(temp)):
self.data.append(temp[i].split())
for i in self.data:
matrix = Matrix([[float(i[0]), float(i[1])]])
self.matrices.append(matrix)
# Calculate the mean and assign it to the Class variable self.mean
def set_mean(self):
for i in self.matrices:
self.mean = self.mean.add(i)
self.mean.scaler(1 / len(self.matrices))
# Calculate the covariance and assign it to the Class variable self.covariance
def set_covariance(self):
for i in self.matrices:
a = i.subtract(self.mean)
a_transpose = Matrix(a.get_data())
a_transpose.transpose()
b = a_transpose.multiply(a)
self.covariance = self.covariance.add(b)
self.covariance.scaler(1 / len(self.matrices))
# Implementation of the power method for finding the dominant eigenvalue for a square matrix
# Returns both eigenvalues for a 2x2 matrix as well as a unit length vector.
# If the matrix is not a 2x2 matrix mu will be the dominant eigenvalue.
# iterations: max number of iterations allowed
# mat: the covariance matrix of the matrix being tested
# estimate: a matrix to begin with as the estimate
def power(self, iterations, mat, estimate):
r = 1
k = 0
m = iterations
y = estimate
x = mat.multiply(y)
while r > .001 and k < m:
max_x = x.find_max()
x.scaler(1 / max_x)
y = Matrix(x.get_data())
x = mat.multiply(y)
temp = Matrix(y.get_data())
temp.transpose()
a = temp.multiply(x).get_data()[0][0]
b = temp.multiply(y).get_data()[0][0]
mu = a / b
r = Matrix(y.get_data())
r.scaler(mu)
r = r.subtract(x)
r = r.find_max()
k += 1
y.scaler(1 / mu)
return mu, mat.trace() - mu, y
# Implementation of the leverrier method for finding the characteristic equation for a polynomial
def leverrier(self, companion):
res = []
b = companion
a = -(b.trace())
res.append(a)
for i in range(companion.get_rows() - 1, 0, -1):
ai = companion.identity()
ai.scaler(a)
b = companion.multiply(b.add(ai))
a = -(b.trace()) / (5 - i + 1)
res.append(a)
return res
# This method is used to find the roots of a polynomial equation by providing the companion matrix
# for the polynomial and an estimate matrix for the power method.
# Based on Deflation from this site: http://www.maths.qmul.ac.uk/~wj/MTH5110/notes/notes08.pdf
def find_roots(self, comp, est):
count = comp.get_rows()
for i in range(count):
#.........这里部分代码省略.........