動作環境
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)
http://qiita.com/Takumi0204/items/28f35d60bef18954aabe
を参考に
http://qiita.com/7of9/items/54ec092a91880df9dc64
の結果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
以下を見つけた。
https://github.com/lesguillemets/notes/blob/master/2013/Nov/13.python-numpy-sin.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()