'float'没有属性dtype

时间:2018-12-21 01:37:09

标签: python numpy matplotlib floating-point

我有一组数据,并且使用numpy和scipy对数据进行了正态分布。但是当我尝试打印pdf时,出现以下错误:

error description

我尝试更改Z的dtype,但这没有用。任何建议都会有所帮助。谢谢。

import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import numpy as np
import random 
from sympy.matrices import Matrix
from sympy import symbols, pprint, N
from scipy.stats import multivariate_normal
from target import true_target_trajectory, target_posiion

def plot_gaussian(X, Y, Z):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(X, Y, Z)
    plt.show()

def covariance(x, y):
    sigma1 = np.std(x, dtype=np.float64)
    sigma2 = np.std(y, dtype=np.float64)
    cov = np.matrix([[sigma1, sigma1*sigma2], [sigma1*sigma2, sigma2]])
    min_eig = np.min(np.real(np.linalg.eigvals(cov)))
    if min_eig < 0:
        cov -= 10*min_eig * np.eye(*cov.shape)
    return cov

def gaussian(x, mu, cov):
    rv = multivariate_normal(mu, cov)
    return rv.pdf(x)

#plot_gaussian()
vin = 300
qin = 9
x = []
y = []
time = np.linspace(0, 2*np.pi, 100)
for t in (time):
    cc = target_posiion(vin, qin, t)
    x.append(cc.T[0])
    y.append(cc.T[1])
mu = np.array([np.mean(x), np.mean(y)])
cov = covariance(x, y)
X, Y = np.meshgrid(x, y)
pos = np.dstack((X, Y))
Z = gaussian(pos, mu, cov)
plot_gaussian(X, Y, Z)

我试图用x = np.linspace(-1,3,100)和y = np.linspace(0,4,100)重现该问题。但这并没有给出任何错误,我得到了预期的钟形曲线。 所以我附上目标位置的代码。 target_position的代码:

import random
import numpy as np
from sympy.vector.coordsysrect import CoordSys3D
from sympy.physics.mechanics import dynamicsymbols
from sympy import symbols, sin, pprint, Derivative, Identity, N
from sympy.matrices import Matrix, BlockMatrix, block_collapse

C = CoordSys3D('C')
i, j, k = C.base_vectors()

def evaluate_matrix(m, v_in, q_in, tk):
    w, t = symbols('w t')
    v0, q = symbols('v0 q')
    params = {v0:v_in, q:q_in, t:tk}
    return Matrix([[N(m[0].subs(params)), N(m[1].subs(params))]]).T

def true_target_trajectory(v_in, q_in, tk):
    w, t = symbols('w t')
    v0, q, A = symbols('v0 q A')
    r, v, a, x, y = dynamicsymbols('r v a x y')
    A = (v0**2)/q
    w = q/(2*v0)
    x = A*sin(w*t)*i
    y = A*sin(2*w*t)*j
    r = x + y
    r_m = Matrix(r.to_matrix(C)[:2])
    v = Derivative(r, t).doit()
    v_m = Matrix(v.to_matrix(C)[:2])
    a = Derivative(v, t).doit()
    a_m = Matrix(a.to_matrix(C)[:2])
    x_k = BlockMatrix([[r_m.T, v_m.T, a_m.T]]).T
    I = Identity(2)
    H = BlockMatrix([[I, I, I]])
    z = evaluate_matrix(block_collapse(H*x_k), v_in, q_in, tk)
    return z

def target_posiion(v_in, q_in, tk):
    sigma = 50
    u_k = Matrix([[random.gauss(0,1), random.gauss(0,1)]]).T
    z = true_target_trajectory(v_in, q_in, tk)
    z_c_k = z + sigma*u_k
    return z_c_k

1 个答案:

答案 0 :(得分:0)

问题

问题在于您的xysympy.core.numbers.Float类型的列表,而不是常规的Python float。 Numpy不知道如何转换Sympy数值类型,因此meshgrid最终返回X的{​​{1}}和Y数组。沿着这条线,最终结束了对dtype=object的呼叫。

修复

只需将ax.plot_surfacex转换为y的标准Numpy数组,然后再将它们传递到np.float64

meshgrid

一旦您这样做,一切都会好起来的。输出如下:

enter image description here