2017-04-09 2 views
1

これは、プロッタにレンジ機能によって生成されたいくつかの浮動小数点値を渡す私のコードです:のpython 3.4はTypeError:単項+に悪いオペランドのタイプ:「ジェネレータ」

import numpy as np 
import matplotlib.pyplot as plt 

gK_inf = 20.70 
gK_0 = 0.01 
tauN = 0.915 + 0.037 

def graph(formula, t_range): 
    t = np.array(t_range) 
    gK = formula(t) # <- note now we're calling the function 'formula' with x 
    plt.plot(t, gK) 
    plt.xlabel(r"$g_K$") 
    plt.ylabel(r"$\frac{P_{i}}{P_{o}}$") 
    plt.legend([r"$\frac{P_{i}}{P_{o}}$"]) 
    annotation_string = r"$E=0$" 
    plt.text(0.97,0.8, annotation_string, bbox=dict(facecolor='red', alpha=0.5), 
     transform=plt.gca().transAxes, va = "top", ha="right") 
    plt.show() 

def my_formula(t): 
    return np.power((np.power(gK_inf,0.25))*((np.power(gK_inf,0.25)-np.power(gK_0,0.25))*np.exp(-t/tauN)),4) 

def frange(x, y, jump): 
    while x < y: 
     yield x 
     x += jump 

graph(my_formula, frange(0,11e-3,1e-3)) 

そして、これがスローエラーです:

> gK = formula(t) # <- note now we're calling the function 'formula' 
> with x 
>  File "F:\Eclipse\my_test\gK", line 26, in my_formula 
>   return np.power((np.power(gK_inf,0.25))*((np.power(gK_inf,0.25)-np.power(gK_0,0.25))*np.exp(-t/tauN)),4) 
>  TypeError: bad operand type for unary -: 'generator' 

お願いします。

+0

浮動小数点範囲が必要な場合は、numpy.arangeまたは多分['numpy.linspace'](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html)を使用します。 #numpy.linspace) –

答えて

3

t_rangeはジェネレータです。あなたはt = np.array(t_range)とnumpyの配列に変換しようとします。しかし、それは動作しません。ジェネレータでnp.arrayを呼び出すと、ジェネレータを唯一の要素として1要素の配列が返されます。ジェネレータをアンロールしません。

代わりにnp.fromiter(t_range, dtype=np.float)を試すことができます。または、まずt_rangeをリストに変換してください。あなたがすべてでfrangeを書いた理由、それは基本的に組み込みrangeはすでにそのstep引数に何を行うよう

ちなみに、それは、不明です。

+0

あなたが言ったように: 'np.fromiter(t_range)'を使って、このエラーが発生しました: 'TypeError:必須引数 'dtype'(pos 2)が見つかりません' – Roboticist

関連する問題