質問編集履歴
2
初めての質問で失礼しました。挿入いたしました。
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -92,30 +92,6 @@ 
     | 
|
| 
       92 
92 
     | 
    
         
             
            p0 = 0.5, 0.5
         
     | 
| 
       93 
93 
     | 
    
         
             
            popt, pcov = curve_fit(func, array_x, array_y, p0)
         
     | 
| 
       94 
94 
     | 
    
         
             
            ```
         
     | 
| 
       95 
     | 
    
         
            -
            import numpy as np
         
     | 
| 
       96 
     | 
    
         
            -
            from scipy import integrate
         
     | 
| 
       97 
     | 
    
         
            -
            from scipy.optimize import curve_fit
         
     | 
| 
       98 
     | 
    
         
            -
            import math
         
     | 
| 
       99 
     | 
    
         
            -
            import pandas as pd
         
     | 
| 
       100 
     | 
    
         
            -
             
     | 
| 
       101 
     | 
    
         
            -
            Eins = 2.01 * 10**8
         
     | 
| 
       102 
     | 
    
         
            -
             
     | 
| 
       103 
     | 
    
         
            -
            Lorentzbroad = Eins / 2 / math.pi
         
     | 
| 
       104 
     | 
    
         
            -
            widthL = Lorentzbroad / 10**9
         
     | 
| 
       105 
     | 
    
         
            -
             
     | 
| 
       106 
     | 
    
         
            -
            df = pd.read_csv('book1.csv')
         
     | 
| 
       107 
     | 
    
         
            -
            array_x=np.array(df['t'])
         
     | 
| 
       108 
     | 
    
         
            -
            array_y=np.array(df['y'])
         
     | 
| 
       109 
     | 
    
         
            -
             
     | 
| 
       110 
     | 
    
         
            -
            def func(x, param1, param2):
         
     | 
| 
       111 
     | 
    
         
            -
                def voigt(z):
         
     | 
| 
       112 
     | 
    
         
            -
                    return param1 * 0.8256 / np.pi**(3/2) / param2 / widthL * np.exp((-1) * (2 * z / param2)**2 * np.log(2)) / (1 + (2 * (x - z) / widthL)**2)
         
     | 
| 
       113 
     | 
    
         
            -
                integration = integrate.quad(voigt, -np.inf, np.inf)[0]
         
     | 
| 
       114 
     | 
    
         
            -
                return integration
         
     | 
| 
       115 
     | 
    
         
            -
             
     | 
| 
       116 
     | 
    
         
            -
            p0 = 0.5, 0.5
         
     | 
| 
       117 
     | 
    
         
            -
            popt, pcov = curve_fit(func, array_x, array_y, p0)
         
     | 
| 
       118 
     | 
    
         
            -
             
     | 
| 
       119 
95 
     | 
    
         
             
            ### 試したこと
         
     | 
| 
       120 
96 
     | 
    
         | 
| 
       121 
97 
     | 
    
         
             
            ### 補足情報(FW/ツールのバージョンなど)
         
     | 
1
初めての質問で失礼しました。挿入いたしました。
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -5,14 +5,76 @@ 
     | 
|
| 
       5 
5 
     | 
    
         
             
            パラメータは2つでparam1とparam2です。
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
       7 
7 
     | 
    
         
             
            ### 発生している問題・エラーメッセージ
         
     | 
| 
      
 8 
     | 
    
         
            +
            ```
         
     | 
| 
      
 9 
     | 
    
         
            +
            ---------------------------------------------------------------------------
         
     | 
| 
      
 10 
     | 
    
         
            +
            TypeError                                 Traceback (most recent call last)
         
     | 
| 
      
 11 
     | 
    
         
            +
            <ipython-input-10-baa10670dc7d> in <module>
         
     | 
| 
      
 12 
     | 
    
         
            +
                 21 
         
     | 
| 
      
 13 
     | 
    
         
            +
                 22 p0 = 0.5, 0.5
         
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
      
 14 
     | 
    
         
            +
            ---> 23 popt, pcov = curve_fit(func, array_x, array_y, p0)
         
     | 
| 
       9 
15 
     | 
    
         | 
| 
      
 16 
     | 
    
         
            +
            ~\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
         
     | 
| 
      
 17 
     | 
    
         
            +
                750         # Remove full_output from kwargs, otherwise we're passing it in twice.
         
     | 
| 
      
 18 
     | 
    
         
            +
                751         return_full = kwargs.pop('full_output', False)
         
     | 
| 
      
 19 
     | 
    
         
            +
            --> 752         res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
         
     | 
| 
      
 20 
     | 
    
         
            +
                753         popt, pcov, infodict, errmsg, ier = res
         
     | 
| 
      
 21 
     | 
    
         
            +
                754         cost = np.sum(infodict['fvec'] ** 2)
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            ~\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
         
     | 
| 
      
 24 
     | 
    
         
            +
                381     if not isinstance(args, tuple):
         
     | 
| 
      
 25 
     | 
    
         
            +
                382         args = (args,)
         
     | 
| 
      
 26 
     | 
    
         
            +
            --> 383     shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
         
     | 
| 
      
 27 
     | 
    
         
            +
                384     m = shape[0]
         
     | 
| 
      
 28 
     | 
    
         
            +
                385 
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
            ~\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
         
     | 
| 
      
 31 
     | 
    
         
            +
                 24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
         
     | 
| 
      
 32 
     | 
    
         
            +
                 25                 output_shape=None):
         
     | 
| 
      
 33 
     | 
    
         
            +
            ---> 26     res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
         
     | 
| 
      
 34 
     | 
    
         
            +
                 27     if (output_shape is not None) and (shape(res) != output_shape):
         
     | 
| 
      
 35 
     | 
    
         
            +
                 28         if (output_shape[0] != 1):
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
            ~\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in func_wrapped(params)
         
     | 
| 
      
 38 
     | 
    
         
            +
                456     if transform is None:
         
     | 
| 
      
 39 
     | 
    
         
            +
                457         def func_wrapped(params):
         
     | 
| 
      
 40 
     | 
    
         
            +
            --> 458             return func(xdata, *params) - ydata
         
     | 
| 
      
 41 
     | 
    
         
            +
                459     elif transform.ndim == 1:
         
     | 
| 
      
 42 
     | 
    
         
            +
                460         def func_wrapped(params):
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
            <ipython-input-10-baa10670dc7d> in func(x, param1, param2)
         
     | 
| 
      
 45 
     | 
    
         
            +
                 17     def voigt(z):
         
     | 
| 
      
 46 
     | 
    
         
            +
                 18         return param1 * 0.8256 / np.pi**(3/2) / param2 / widthL * np.exp((-1) * (2 * z / param2)**2 * np.log(2)) / (1 + (2 * (x - z) / widthL)**2)
         
     | 
| 
      
 47 
     | 
    
         
            +
            ---> 19     integration = integrate.quad(voigt, -np.inf, np.inf)[0]
         
     | 
| 
      
 48 
     | 
    
         
            +
                 20     return integration
         
     | 
| 
      
 49 
     | 
    
         
            +
                 21 
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
            ~\Anaconda3\lib\site-packages\scipy\integrate\quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
         
     | 
| 
      
 52 
     | 
    
         
            +
                339     if weight is None:
         
     | 
| 
      
 53 
     | 
    
         
            +
                340         retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
         
     | 
| 
      
 54 
     | 
    
         
            +
            --> 341                        points)
         
     | 
| 
      
 55 
     | 
    
         
            +
                342     else:
         
     | 
| 
      
 56 
     | 
    
         
            +
                343         retval = _quad_weight(func, a, b, args, full_output, epsabs, epsrel,
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
            ~\Anaconda3\lib\site-packages\scipy\integrate\quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
         
     | 
| 
      
 59 
     | 
    
         
            +
                448             return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
         
     | 
| 
      
 60 
     | 
    
         
            +
                449         else:
         
     | 
| 
      
 61 
     | 
    
         
            +
            --> 450             return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
         
     | 
| 
      
 62 
     | 
    
         
            +
                451     else:
         
     | 
| 
      
 63 
     | 
    
         
            +
                452         if infbounds != 0:
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
            TypeError: only size-1 arrays can be converted to Python scalars
         
     | 
| 
      
 66 
     | 
    
         
            +
            ```
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
       10 
69 
     | 
    
         
             
            ### 該当のソースコード
         
     | 
| 
      
 70 
     | 
    
         
            +
            ```python
         
     | 
| 
       11 
71 
     | 
    
         
             
            import numpy as np
         
     | 
| 
       12 
72 
     | 
    
         
             
            from scipy import integrate
         
     | 
| 
       13 
73 
     | 
    
         
             
            from scipy.optimize import curve_fit
         
     | 
| 
      
 74 
     | 
    
         
            +
            import math
         
     | 
| 
       14 
75 
     | 
    
         
             
            import pandas as pd
         
     | 
| 
       15 
76 
     | 
    
         | 
| 
      
 77 
     | 
    
         
            +
            Eins = 2.01 * 10**8
         
     | 
| 
       16 
78 
     | 
    
         
             
            Lorentzbroad = Eins / 2 / math.pi
         
     | 
| 
       17 
79 
     | 
    
         
             
            widthL = Lorentzbroad / 10**9
         
     | 
| 
       18 
80 
     | 
    
         | 
| 
         @@ -29,7 +91,31 @@ 
     | 
|
| 
       29 
91 
     | 
    
         | 
| 
       30 
92 
     | 
    
         
             
            p0 = 0.5, 0.5
         
     | 
| 
       31 
93 
     | 
    
         
             
            popt, pcov = curve_fit(func, array_x, array_y, p0)
         
     | 
| 
      
 94 
     | 
    
         
            +
            ```
         
     | 
| 
      
 95 
     | 
    
         
            +
            import numpy as np
         
     | 
| 
      
 96 
     | 
    
         
            +
            from scipy import integrate
         
     | 
| 
      
 97 
     | 
    
         
            +
            from scipy.optimize import curve_fit
         
     | 
| 
      
 98 
     | 
    
         
            +
            import math
         
     | 
| 
      
 99 
     | 
    
         
            +
            import pandas as pd
         
     | 
| 
       32 
100 
     | 
    
         | 
| 
      
 101 
     | 
    
         
            +
            Eins = 2.01 * 10**8
         
     | 
| 
      
 102 
     | 
    
         
            +
             
     | 
| 
      
 103 
     | 
    
         
            +
            Lorentzbroad = Eins / 2 / math.pi
         
     | 
| 
      
 104 
     | 
    
         
            +
            widthL = Lorentzbroad / 10**9
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
      
 106 
     | 
    
         
            +
            df = pd.read_csv('book1.csv')
         
     | 
| 
      
 107 
     | 
    
         
            +
            array_x=np.array(df['t'])
         
     | 
| 
      
 108 
     | 
    
         
            +
            array_y=np.array(df['y'])
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
            def func(x, param1, param2):
         
     | 
| 
      
 111 
     | 
    
         
            +
                def voigt(z):
         
     | 
| 
      
 112 
     | 
    
         
            +
                    return param1 * 0.8256 / np.pi**(3/2) / param2 / widthL * np.exp((-1) * (2 * z / param2)**2 * np.log(2)) / (1 + (2 * (x - z) / widthL)**2)
         
     | 
| 
      
 113 
     | 
    
         
            +
                integration = integrate.quad(voigt, -np.inf, np.inf)[0]
         
     | 
| 
      
 114 
     | 
    
         
            +
                return integration
         
     | 
| 
      
 115 
     | 
    
         
            +
             
     | 
| 
      
 116 
     | 
    
         
            +
            p0 = 0.5, 0.5
         
     | 
| 
      
 117 
     | 
    
         
            +
            popt, pcov = curve_fit(func, array_x, array_y, p0)
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
       33 
119 
     | 
    
         
             
            ### 試したこと
         
     | 
| 
       34 
120 
     | 
    
         | 
| 
       35 
121 
     | 
    
         
             
            ### 補足情報(FW/ツールのバージョンなど)
         
     |