小编Mon*_*ica的帖子

高斯函数python

我正在尝试使用 matplotlib 绘制高斯函数。这是我的代码:

    #!/usr/bin/env python
    from matplotlib import pyplot as plt
    import numpy as np
    import math

    def gaussian(x, alpha, r):
          return 1./(math.sqrt(alpha**math.pi))*np.exp(-alpha*np.power((x - r), 2.))

    x = np.linspace(-3, 3, 100)
    plt.plot(gaussian(x, 1, 0))

    plt.show()
Run Code Online (Sandbox Code Playgroud)

为什么范围是从 0 到 100 而不是介于 -3 和 3 之间?在此处输入图片说明

python matplotlib

3
推荐指数
1
解决办法
7408
查看次数

如何拼合嵌套的python字典?

我正在尝试拼合嵌套的字典:

dict1 = {
    'Bob': {
        'shepherd': [4, 6, 3],
        'collie': [23, 3, 45],
        'poodle': [2, 0, 6],
    },
    'Sarah': {
        'shepherd': [1, 2, 3],
        'collie': [3, 31, 4],
        'poodle': [21, 5, 6],
    },
    'Ann': {
        'shepherd': [4, 6, 3],
        'collie': [23, 3, 45],
        'poodle': [2, 10, 8],
    }
}
Run Code Online (Sandbox Code Playgroud)

我想显示列表中的所有值:[4、6、3、23、3、45、2、0、6、1、2、3,...,2、10、8]

我的第一个想法就是这样:

dict_flatted = [ i for name in names.values() for dog in dogs.values() for i in dog]
Run Code Online (Sandbox Code Playgroud)

虽然我得到了错误。我很乐意提供处理技巧。

python dictionary

2
推荐指数
1
解决办法
5943
查看次数

循环索引取决于前一个索引

我有一个清单: fruits = ['apple', 'orange', 'blueberry', strawberry']

如何创建循环,使一个索引依赖于另一个索引:

for i in range(len(fruits)):
   for j range(len(fruits[i+1:])):
       print i,j
Run Code Online (Sandbox Code Playgroud)

我想打印成对:

'apple', 'orange'
'orange', 'blueberry'
'blueberry', strawberry'
'orange', 'blueberry'
etc...
Run Code Online (Sandbox Code Playgroud)

我想获得对应于c ++语言的循环:

 for(i=0;i<5;i++) 
     for (j=i+1; j<5; j++)
         print i, j
Run Code Online (Sandbox Code Playgroud)

python loops

2
推荐指数
1
解决办法
55
查看次数

从数组中删除重复的元素

我有两个numpy数组:

[  2.09588161   2.34243927   2.45505059   3.61549894   6.42506932
   8.52095092   5.76933731   6.03952746   4.30033044   3.77862927
   3.73546847   5.40022069   8.52095092  10.61683253   7.75964201
   8.01668568   6.17414768   4.40489563   4.72554455   5.76933731
   7.75964201   6.02187958   4.53771075   2.59319536   1.94766573
   6.03952746   8.01668568   4.53771075   6.6124742    5.38450762
   4.30033044   6.17414768   2.59319536   5.38450762   4.67416659
   2.09588161   2.34243927   2.45505059   3.61549894   3.77862927
   3.73546847   5.40022069   4.40489563   4.72554455   1.94766573]

[ 2.09588161  2.34243927  2.45505059  3.61549894  3.77862927  3.73546847
  5.40022069  4.40489563  4.72554455  1.94766573]
Run Code Online (Sandbox Code Playgroud)

如何从第一个数组中删除与第二个数组中相同的元素.

我已经看过uniques方法,但它只删除了特定数组中的重复元素.

python arrays numpy

2
推荐指数
1
解决办法
60
查看次数

在 DataFrame 中重复列

在 DataFrame 中重复列的正确方法是什么?

我正在研究 df:

  England    Germany    US
0 -3.3199    -3.31      496.68
1 1004.0     4.01       4.01
2 4.9794     4.97       1504.97
3 3.1766     2003.17    3.17
Run Code Online (Sandbox Code Playgroud)

我想获得:

  England  England   Germany  Germany   US        US    
0 -3.3199  -3.3199   -3.31    -3.31     496.68    496.68    
1 1004.0   1004.0    4.01     4.01      4.01      4.01 
2 4.9794   4.9794    4.97     4.97      1504.97   1504.97
3 3.1766   3.1766    2003.17  2003.17   3.17      3.17
Run Code Online (Sandbox Code Playgroud)

我很难从原始 DataFrame 中获取标头并将其加倍:

headers_double = [x for x in headers for i in range(2)]
Run Code Online (Sandbox Code Playgroud)

随后我尝试使用新标头创建 df:

df.columns = [x for x …
Run Code Online (Sandbox Code Playgroud)

python pandas

1
推荐指数
1
解决办法
5317
查看次数

使用某些参数绘制误差函数

我正在尝试以以下形式绘制错误函数:

#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
import math
from scipy.special import erf
from scipy.integrate import quad
import scipy.integrate as integrate

def integrand(t, alpha, r ):
        return np.exp(-alpha*(t-r)**2)

def damp(alpha, rho, r):
        return quad(integrand, 0, rho, args=(alpha, r))[0]

def norm_constant(alpha, r):
        return 2.0*math.sqrt(2)/math.sqrt(math.pi) * (1./(1.- erf(-math.sqrt(np.abs(alpha))*r)))

A = 1.5
r = 0.3

g = [ norm_constant(A,r) *damp(A,x,r) for x in np.arange(-2,2,0.2)]
x = np.arange(-2,2,0.2)

A2 = 1.8
r2 = 0.3
g2 = [ norm_constant(A2,r2) *damp(A2,x,r2) for …
Run Code Online (Sandbox Code Playgroud)

python math matplotlib

0
推荐指数
1
解决办法
669
查看次数

标签 统计

python ×6

matplotlib ×2

arrays ×1

dictionary ×1

loops ×1

math ×1

numpy ×1

pandas ×1