在每个点上评估正常的cdf

时间:2018-07-21 17:19:42

标签: python numpy scipy

我想在5个点中的每个点上评估由4x3点网格定义的几个正常CDF。

import numpy as np
import scipy.stats


a = np.array([-1, 0, 1])
b = np.array([1, 2, 3, 4])
x = np.array([-.5, 0, .5, 1, 2])
grid_a, grid_b = np.meshgrid(a,b)
scipy.stats.norm(loc=grid_a, scale=grid_b).cdf(x)

引发此异常:

ValueErrorTraceback (most recent call last)                                                                                     
 <ipython-input-46-82423c7451d2> in <module>()                                                                                   
       3 x = np.array([-.5, 0, .5, 1, 2])                                                                                        
       4 grid_a, grid_b = np.meshgrid(a,b)                                                                                       
 ----> 5 scipy.stats.norm(loc=grid_a, scale=grid_b).cdf(x)                                                                       

 ~/.envs/practice/lib/python3.6/site-packages/scipy/stats/_distn_infrastructure.py in cdf(self, x)                               
     454                                                                                                                         
     455     def cdf(self, x):                                                                                                   
 --> 456         return self.dist.cdf(x, *self.args, **self.kwds)                                                                
     457                                                                                                                         
     458     def logcdf(self, x):                                                                                                

 ~/.envs/practice/lib/python3.6/site-packages/scipy/stats/_distn_infrastructure.py in cdf(self, x, *args, **kwds)                
    1733         args = tuple(map(asarray, args))                                                                                
    1734         dtyp = np.find_common_type([x.dtype, np.float64], [])                                                           
 -> 1735         x = np.asarray((x - loc)/scale, dtype=dtyp)                                                                     
    1736         cond0 = self._argcheck(*args) & (scale > 0)                                                                     
    1737         cond1 = self._open_support_mask(x) & (scale > 0)                                                                

 ValueError: operands could not be broadcast together with shapes (5,) (4,3)                                                     

1 个答案:

答案 0 :(得分:3)

您必须重塑class User < ApplicationRecord has_many :created_notes, class_name: 'Note', foreign_key: :user_id has_many :received_shares, foreign_key: :receiver_id, class_name: 'Share' has_many :received_notes, through: :received_shares, source: :note has_many :shares has_many :shared_notes, through: :shares, source: :note end class Share < ApplicationRecord # Optional belongs_to :creator, class_name: 'User', foreign_key: :user_id belongs_to :receiver, class_name: 'User', foreign_key: :receiver_id # Mandatory belongs_to :note end class Note < ApplicationRecord ; end user_a = User.first user_a.shared_notes user_a.received_notes user_a.created_notes user.shares.each {|share| share.note}user.shares.map(&:note)才能兼容广播。例如,您可以通过向a添加一个平凡的尺寸和向b添加两个平凡的尺寸来做到这一点。也就是说,使用x(形状为(3,1))和a(形状为(4,1,1))。 (而不是b,您可能更喜欢更明确的a[:, None],但它的值仅为b[:, None, None]。)然后,None的形状为(5,),而形状为{ {1}}和np.newaxis分别具有形状(3,1)和(4,1,1),通过广播计算的结果的形状为(4,3,5):

None

也可以像在问题中一样使用“冻结”分发x的{​​{1}}方法:

a