小编jho*_*ick的帖子

使用另一个数组更改数组的形状

我想使用其他数组重塑一个数组。

假设我有array_1,其shape(5, 1),例如:

>>> array_1
array([[ 0.33333333],
       [ 0.36666667],
       [ 0.16666667],
       [ 0.06666667],
       [ 0.06666667]]
Run Code Online (Sandbox Code Playgroud)

并且array_2,其形状为(1, 5)。我想重塑array_1形状,使其具有的形状array_2array_2每当我运行代码时,的形状都会改变。

python numpy

2
推荐指数
1
解决办法
2068
查看次数

马尔可夫链平稳分布的Python代码解释

我有这个代码:

import numpy as np
from scipy.linalg import eig 
transition_mat = np.matrix([
    [.95, .05, 0., 0.],\
    [0., 0.9, 0.09, 0.01],\
    [0., 0.05, 0.9, 0.05],\
    [0.8, 0., 0.05, 0.15]])

S, U = eig(transition_mat.T)
stationary = np.array(U[:, np.where(np.abs(S - 1.) < 1e-8)[0][0]].flat)
stationary = stationary / np.sum(stationary)

>>> print stationary
[ 0.34782609  0.32608696  0.30434783  0.02173913]
Run Code Online (Sandbox Code Playgroud)

但我无法理解这一行:

stationary = np.array(U[:, np.where(np.abs(S - 1.) < 1e-8)[0][0]].flat)
Run Code Online (Sandbox Code Playgroud)

谁能解释一下这部分:U[:, np.where(np.abs(S - 1.) < 1e-8)[0][0]].flat

我知道该例程返回S:特征值,U:特征向量。我需要找到与特征值1对应的特征向量。我写了下面的代码:

for i in range(len(S)):
    if …
Run Code Online (Sandbox Code Playgroud)

python numpy python-3.x

1
推荐指数
2
解决办法
7642
查看次数

标签 统计

numpy ×2

python ×2

python-3.x ×1