本文整理汇总了Python中numpy.mat方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.mat方法的具体用法?Python numpy.mat怎么用?Python numpy.mat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.mat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testDamp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def testDamp(self):
A = np.mat('''-0.2 0.06 0 -1;
0 0 1 0;
-17 0 -3.8 1;
9.4 0 -0.4 -0.6''')
B = np.mat('''-0.01 0.06;
0 0;
-32 5.4;
2.6 -7''')
C = np.eye(4)
D = np.zeros((4,2))
sys = ss(A, B, C, D)
wn, Z, p = damp(sys, False)
# print (wn)
np.testing.assert_array_almost_equal(
wn, np.array([4.07381994, 3.28874827, 3.28874827,
1.08937685e-03]))
np.testing.assert_array_almost_equal(
Z, np.array([1.0, 0.07983139, 0.07983139, 1.0]))
示例2: testConnect2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def testConnect2(self):
sys = append(ss([[-5, -2.25], [4, 0]], [[2], [0]],
[[0, 1.125]], [[0]]),
ss([[-1.6667, 0], [1, 0]], [[2], [0]],
[[0, 3.3333]], [[0]]),
1)
Q = [ [ 1, 3], [2, 1], [3, -2]]
sysc = connect(sys, Q, [3], [3, 1, 2])
np.testing.assert_array_almost_equal(
sysc.A, np.mat([[-5, -2.25, 0, -6.6666],
[4, 0, 0, 0],
[0, 2.25, -1.6667, 0],
[0, 0, 1, 0]]))
np.testing.assert_array_almost_equal(
sysc.B, np.mat([[2], [0], [0], [0]]))
np.testing.assert_array_almost_equal(
sysc.C, np.mat([[0, 0, 0, -3.3333],
[0, 1.125, 0, 0],
[0, 0, 0, 3.3333]]))
np.testing.assert_array_almost_equal(
sysc.D, np.mat([[1], [0], [0]]))
示例3: lr_train_bgd
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def lr_train_bgd(feature: np.array, label: np.array, max_cycle: int, alpha: float) -> np.mat:
"""利用梯度下降法训练逻辑回归模型(LR)
:param feature: 特征
:param label: 标签
:param max_cycle: 最大迭代次数
:param alpha: 学习率
:return: w的权重
"""
n = np.shape(feature)[1] # 特征个数
w = np.random.rand(n).reshape((n, 1)) # 随机初始化权重
i = 0
while i <= max_cycle:
i += 1
h = sigmoid(np.dot(feature, w)) # 做点乘,计算sigmoid值
err = label - h # 误差
if i % 100 == 0:
print(f'error rate: {error_rate(h, label)}')
w += alpha * np.dot(feature.T, err) # wi+ 1= wi+ α·d 梯度下降进行权重修正
return w
示例4: fundamentalToRt
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def fundamentalToRt(F):
W = np.mat([[0,-1,0],[1,0,0],[0,0,1]],dtype=float)
U,d,Vt = np.linalg.svd(F)
if np.linalg.det(U) < 0:
U *= -1.0
if np.linalg.det(Vt) < 0:
Vt *= -1.0
R = np.dot(np.dot(U, W), Vt)
if np.sum(R.diagonal()) < 0:
R = np.dot(np.dot(U, W.T), Vt)
t = U[:, 2]
# TODO: Resolve ambiguities in better ways. This is wrong.
if t[2] < 0:
t *= -1
# TODO: UGLY!
if os.getenv("REVERSE") is not None:
t *= -1
return np.linalg.inv(poseRt(R, t))
示例5: bar2e
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def bar2e(ex,ey,ep):
"""
Compute the element stiffness matrix for two dimensional bar element.
:param list ex: element x coordinates [x1, x2]
:param list ey: element y coordinates [y1, y2]
:param list ep: [E, A]: E - Young's modulus, A - Cross section area
:return mat Ke: stiffness matrix, [4 x 4]
"""
E=ep[0]
A=ep[1]
b = np.mat([[ex[1]-ex[0]],[ey[1]-ey[0]]])
L = np.asscalar(np.sqrt(b.T*b))
Kle = np.mat([[1.,-1.],[-1.,1.]])*E*A/L
n = np.asarray(b.T/L).reshape(2,)
G = np.mat([
[n[0],n[1],0.,0.],
[0.,0.,n[0],n[1]]
])
return G.T*Kle*G
示例6: _compute_spectrogram
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def _compute_spectrogram(self, sample_rate):
""" Creates the STFT matrices for channel 0 and 1, and computes the frequency matrix.
Parameter:
sample_rate (integer): sample rate
Returns:
stft_ch0 (np.matrix): a 2D Numpy matrix containing the stft of channel 0
stft_ch1 (np.matrix): a 2D Numpy matrix containing the stft of channel 1
wmat (np.matrix): a 2D Numpy matrix containing the frequencies of analysis of the Fourier transform
"""
# Compute the stft of the two channel mixtures
self.audio_signal.stft_params = self.stft_params
self.audio_signal.stft()
stft_ch0 = self.audio_signal.get_stft_channel(0)
stft_ch1 = self.audio_signal.get_stft_channel(1)
# Compute the freq. matrix for later use in phase calculations
n_time_bins = len(self.audio_signal.time_bins_vector)
wmat = np.array(np.tile(np.mat(
self.audio_signal.freq_vector).T, (1, n_time_bins))) * (
2 * np.pi / sample_rate)
wmat += constants.EPSILON
return stft_ch0, stft_ch1, wmat
示例7: fit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def fit(self,oridata,orilabel):
if self._batchsize == 'auto':
self._batchsize = oridata.shape[1]
data = self.normalscaler.fit_transform(oridata)
label = self.onehotencoder.fit_transform(np.mat(orilabel).T)
mappingdata = self.mapping_generator.generator_nodes(data,self._maptimes,self._batchsize,self._map_function)
enhencedata = self.enhence_generator.generator_nodes(mappingdata,self._enhencetimes,self._batchsize,self._enhence_function)
inputdata = np.column_stack((mappingdata,enhencedata))
self.pesuedoinverse = self.pinv(inputdata)
self.W = self.pesuedoinverse.dot(label)
Y = self.predict(oridata)
accuracy, i = self.accuracy(Y,orilabel),0
print("inital setting, number of mapping nodes {0}, number of enhence nodes {1}, accuracy {2}".format(mappingdata.shape[1],enhencedata.shape[1],round(accuracy,5)))
while i < self._traintimes and accuracy < self._acc:
Y = self.addingenhence_predict(data, label, self._step,self._batchsize)
accuracy = self.accuracy(Y,orilabel)
i += 1
print("enhencing {3}, number of mapping nodes {0}, number of enhence nodes {1}, accuracy {2}".format(len(self.mapping_generator.Wlist)*self._batchsize,len(self.enhence_generator.Wlist)*self._batchsize,round(accuracy,5),i))
示例8: addingenhence_nodes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def addingenhence_nodes(self, data, label, step = 1, batchsize = 'auto'):
if batchsize == 'auto':
batchsize = data.shape[1]
mappingdata = self.mapping_generator.transform(data)
inputdata = self.transform(data)
localenhence_generator = node_generator()
extraenhence_nodes = localenhence_generator.generator_nodes(mappingdata,step,batchsize,self._enhence_function)
D = self.pesuedoinverse.dot(extraenhence_nodes)
C = extraenhence_nodes - inputdata.dot(D)
BT = self.pinv(C) if (C == 0).any() else np.mat((D.T.dot(D)+np.eye(D.shape[1]))).I.dot(D.T).dot(self.pesuedoinverse)
self.W = np.row_stack((self.W-D.dot(BT).dot(label),BT.dot(label)))
self.enhence_generator.update(localenhence_generator.Wlist,localenhence_generator.blist)
self.pesuedoinverse = np.row_stack((self.pesuedoinverse - D.dot(BT),BT))
示例9: adding_nodes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def adding_nodes(self, data, label, mapstep = 1, enhencestep = 1, batchsize = 'auto'):
if batchsize == 'auto':
batchsize = data.shape[1]
mappingdata = self.mapping_generator.transform(data)
inputdata = self.transform(data)
localmap_generator = node_generator()
extramap_nodes = localmap_generator.generator_nodes(data,mapstep,batchsize,self._map_function)
localenhence_generator = node_generator()
extraenh_nodes = localenhence_generator.generator_nodes(mappingdata,enhencestep,batchsize,self._map_function)
extra_nodes = np.column_stack((extramap_nodes,extraenh_nodes))
D = self.pesuedoinverse.dot(extra_nodes)
C = extra_nodes - inputdata.dot(D)
BT = self.pinv(C) if (C == 0).any() else np.mat((D.T.dot(D)+np.eye(D.shape[1]))).I.dot(D.T).dot(self.pesuedoinverse)
self.W = np.row_stack((self.W-D.dot(BT).dot(label),BT.dot(label)))
self.pesuedoinverse = np.row_stack((self.pesuedoinverse - D.dot(BT),BT))
self.local_mapgeneratorlist.append(localmap_generator)
self.local_enhgeneratorlist.append(localenhence_generator)
示例10: fit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def fit(self,oridata,orilabel):
if self._batchsize == 'auto':
self._batchsize = oridata.shape[1]
data = self.normalscaler.fit_transform(oridata)
label = self.onehotencoder.fit_transform(np.mat(orilabel).T)
mappingdata = self.mapping_generator.generator_nodes(data,self._maptimes,self._batchsize,self._map_function)
enhencedata = self.enhence_generator.generator_nodes(mappingdata,self._enhencetimes,self._batchsize,self._enhence_function)
inputdata = np.column_stack((mappingdata,enhencedata))
self.pesuedoinverse = self.pinv(inputdata)
self.W = self.pesuedoinverse.dot(label)
Y = self.predict(oridata)
accuracy, i = self.accuracy(Y,orilabel),0
print("inital setting, number of mapping nodes {0}, number of enhence nodes {1}, accuracy {2}".format(mappingdata.shape[1],enhencedata.shape[1],round(accuracy,5)))
while i < self._traintimes and accuracy < self._acc:
Y = self.addingmap_predict(data, label, self._step,self._batchsize)
accuracy = self.accuracy(Y,orilabel)
i += 1
print("mapping {3}, number of mapping nodes {0}, number of enhence nodes {1}, accuracy {2}".format((len(self.mapping_generator.Wlist)+len(self.local_mapgeneratorlist)*len(self.local_mapgeneratorlist[0].Wlist))*self._batchsize,len(self.enhence_generator.Wlist)*self._batchsize,round(accuracy,5),i))
示例11: fit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def fit(self,data,label):
if self._batchsize == 'auto':
self._batchsize = data.shape[1]
data = self.normalscaler.fit_transform(data)
label = self.onehotencoder.fit_transform(np.mat(label).T)
mappingdata = self.mapping_generator.generator_nodes(data,self._maptimes,self._batchsize,self._map_function)
enhencedata = self.enhence_generator.generator_nodes(mappingdata,self._enhencetimes,self._batchsize,self._enhence_function)
print('number of mapping nodes {0}, number of enhence nodes {1}'.format(mappingdata.shape[1],enhencedata.shape[1]))
print('mapping nodes maxvalue {0} minvalue {1} '.format(round(np.max(mappingdata),5),round(np.min(mappingdata),5)))
print('enhence nodes maxvalue {0} minvalue {1} '.format(round(np.max(enhencedata),5),round(np.min(enhencedata),5)))
inputdata = np.column_stack((mappingdata,enhencedata))
pesuedoinverse = self.pinv(inputdata,self._reg)
self.W = pesuedoinverse.dot(label)
示例12: fit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def fit(self,oridata,orilabel):
if self._batchsize == 'auto':
self._batchsize = oridata.shape[1]
data = self.normalscaler.fit_transform(oridata)
label = self.onehotencoder.fit_transform(np.mat(orilabel).T)
mappingdata = self.mapping_generator.generator_nodes(data,self._maptimes,self._batchsize,self._map_function)
enhencedata = self.enhence_generator.generator_nodes(mappingdata,self._enhencetimes,self._batchsize,self._enhence_function)
inputdata = np.column_stack((mappingdata,enhencedata))
self.pesuedoinverse = self.pinv(inputdata)
self.W = self.pesuedoinverse.dot(label)
Y = self.predict(oridata)
accuracy, i = self.accuracy(Y,orilabel),0
# print("inital setting, number of mapping nodes {0}, number of enhence nodes {1}, accuracy {2}".format(mappingdata.shape[1],enhencedata.shape[1],round(accuracy,5)))
while i < self._traintimes and accuracy < self._acc:
Y = self.adding_predict(oridata, orilabel, self._mapstep, self._enhencestep, self._batchsize)
accuracy = self.accuracy(Y,orilabel)
i += 1
# print("adding {3}, number of mapping nodes {0}, number of enhence nodes {1}, accuracy {2}".format((len(self.mapping_generator.Wlist)+len(self.local_mapgeneratorlist)*len(self.local_mapgeneratorlist[0].Wlist))*self._batchsize,(len(self.enhence_generator.Wlist)+len(self.local_enhgeneratorlist)*len(self.local_enhgeneratorlist[0].Wlist))*self._batchsize,round(accuracy,5),i))
示例13: polar_returns
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def polar_returns(ret, k):
"""
Calculate polar return
:param obs: pandas DataFrame
:return: return radius, return angles
"""
ret= np.mat(ret)
# Find the radius and the angle decomposition on price relative vectors
radius = np.linalg.norm(ret, ord=1, axis=1)
angle = np.divide(ret, np.mat(radius).T)
# Select the 'window' greater values on the observation
index = np.argpartition(radius, -(int(ret.shape[0] * k) + 1))[-(int(ret.shape[0] * k) + 1):]
index = index[np.argsort(radius[index])]
# Return the radius and the angle for extreme found values
return radius[index][::-1], angle[index][::-1]
# Pareto Extreme Risk Index
示例14: Eu_dis
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def Eu_dis(x):
"""
Calculate the distance among each raw of x
:param x: N X D
N: the object number
D: Dimension of the feature
:return: N X N distance matrix
"""
x = np.mat(x)
aa = np.sum(np.multiply(x, x), 1)
ab = x * x.T
dist_mat = aa + aa.T - 2 * ab
dist_mat[dist_mat < 0] = 0
dist_mat = np.sqrt(dist_mat)
dist_mat = np.maximum(dist_mat, dist_mat.T)
return dist_mat
示例15: get_affine_matrix
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import mat [as 别名]
def get_affine_matrix(center, angle, translate, scale, shear=0):
# Helper method to compute affine transformation
# As it is explained in PIL.Image.rotate
# We need compute affine transformation matrix: M = T * C * RSS * C^-1
# where T is translation matrix: [1, 0, tx | 0, 1, ty | 0, 0, 1]
# C is translation matrix to keep center: [1, 0, cx | 0, 1, cy | 0, 0, 1]
# RSS is rotation with scale and shear matrix
# RSS(a, scale, shear) = [ cos(a)*sx -sin(a + shear)*sy 0]
# [ sin(a)*sx cos(a + shear)*sy 0]
# [ 0 0 1]
angle = math.radians(angle)
shear = math.radians(shear)
T = np.array([[1, 0, translate[0]], [0, 1, translate[1]], [0, 0, 1]]).astype(np.float32)
C = np.array([[1, 0, center[0]], [0, 1, center[1]], [0, 0, 1]]).astype(np.float32)
RSS = np.array([[ math.cos(angle)*scale[0], -math.sin(angle + shear)*scale[1], 0],
[ math.sin(angle)*scale[0], math.cos(angle + shear)*scale[1], 0],
[ 0, 0, 1]]).astype(np.float32)
C_inv = np.linalg.inv(np.mat(C))
M = T.dot(C).dot(RSS).dot(C_inv)
return M
# ** tools **