numpy > numpy array > TypeError: only length-1 arrays can be converted to Python scalars > math.exp() 대신 numpy.exp() 사용

운영 환경
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
GNU bash, version 4.3.8(1)-release (x86_64-pc-linux-gnu)

ぃ tp // m / 0204 / 0204 / ms / 28f35d60f18954 아베
참고로
ぃ tp // m / 7, f9 / ms / 54, c092, 91880df9dc64
결과 y1을 matplotlib에서 3D 표시하려고했습니다.

실패 코드



test_3Dplot_170401a.ipynb
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import math

def func_y1(x1, x2):
    rho = x1 ** 2 + x2 ** 2
    y1 = x1 * math.exp(-rho ** 2)
    return y1

x = np.arange(-2, 2, 0.03)
y = np.arange(-2, 2, 0.03)

X, Y = np.meshgrid(x,y)

Z = func_y1(X,Y)

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X,Y,Z,cmap=cm.coolwarm)
plt.show()

결과
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-c3184147fbdd> in <module>()
     16 X, Y = np.meshgrid(x,y)
     17 
---> 18 Z = func_y1(X,Y)
     19 
     20 fig = plt.figure()

<ipython-input-33-c3184147fbdd> in func_y1(x1, x2)
      7 def func_y1(x1, x2):
      8     rho = x1 ** 2 + x2 ** 2
----> 9     y1 = x1 * math.exp(-rho ** 2)
     10     #y1 = x1 * np.exp(-rho ** 2)
     11     return y1

TypeError: only length-1 arrays can be converted to Python scalars

다음을 발견했다.
htps : // 기주 b. 이 m/㎇s 굳이 ts/에서 s/bぉb/까지 r/2013/의 v/13. py 푹신한 mpy 신. md

math.exp() 대신 numpy.exp()를 사용하는 것 같습니다.
정보 감사입니다.

수정 후



test_3Dplot_170401a.ipynb
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import math

def func_y1(x1, x2):
    rho = x1 ** 2 + x2 ** 2
    #y1 = x1 * math.exp(-rho ** 2)
    y1 = x1 * np.exp(-rho ** 2)
    return y1

x = np.arange(-2, 2, 0.03)
y = np.arange(-2, 2, 0.03)

X, Y = np.meshgrid(x,y)

Z = func_y1(X,Y)

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X,Y,Z,cmap=cm.coolwarm)
plt.show()

좋은 웹페이지 즐겨찾기