arrays - ValueError: operands could not be broadcast together with shapes (20,) (6,) - Stack Overflow

I am working through an assignment and keep getting the following error:ValueError: operands could not

I am working through an assignment and keep getting the following error:

ValueError: operands could not be broadcast together with shapes (20,) (6,)

The code is below

import numpy as np
import scipy as sp

def fun2(x):

    serval=7
            # This is just the size of the taylor series
            # eventually I will make the user enter this
    
    z=np.arange(1,serval,1)
            
            # Here I am creating a range for the taylor series
    
    v= (x**z)/(sp.special.factorial(z))
            # If I remove the x^z part of this equation
            # It seems to work. For some reason it gets stuck here
    
    u=np.sum(v)
    y=(3*x-10)*(1+u)
            # These two lines dont pose any problems
    return y

I have done some troubleshooting and it looks like the statement x**z is tripping it up. Can I get some guidance please. For the record I am running a taylor series so the formula I am going for is (3x-10)(1+x/1!+x^2/2!....)

The 20 as I can tell is the size of the x array I am feeding the function. the 6 is the array inside the function, ie z.

Thanks in advance for any advice.

I am working through an assignment and keep getting the following error:

ValueError: operands could not be broadcast together with shapes (20,) (6,)

The code is below

import numpy as np
import scipy as sp

def fun2(x):

    serval=7
            # This is just the size of the taylor series
            # eventually I will make the user enter this
    
    z=np.arange(1,serval,1)
            
            # Here I am creating a range for the taylor series
    
    v= (x**z)/(sp.special.factorial(z))
            # If I remove the x^z part of this equation
            # It seems to work. For some reason it gets stuck here
    
    u=np.sum(v)
    y=(3*x-10)*(1+u)
            # These two lines dont pose any problems
    return y

I have done some troubleshooting and it looks like the statement x**z is tripping it up. Can I get some guidance please. For the record I am running a taylor series so the formula I am going for is (3x-10)(1+x/1!+x^2/2!....)

The 20 as I can tell is the size of the x array I am feeding the function. the 6 is the array inside the function, ie z.

Thanks in advance for any advice.

Share Improve this question edited 2 days ago simon 5,4311 gold badge16 silver badges29 bronze badges asked Feb 28 at 7:23 Kourosh KeshavarzKourosh Keshavarz 1 New contributor Kourosh Keshavarz is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3
  • 1 If you want to compute v for every combination of x and z you can replace that line with v= (x[:, None]**z)/(sp.special.factorial(z)), which will create an array of shape (20, 6) – Nin17 Commented Feb 28 at 8:33
  • Yes, but also the sum would need to be be over axis=1. – Matt Haberland Commented 2 days ago
  • If your two arrays are of different sizes, then... where is the confusion? 20 does not equal 6. Did you mean to perform an outer operation on these arguments that produces a 20x6 matrix? – Reinderien Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

In context you probably meant to use an outer (an equivalent is possible with newaxis); but you can't simply mash two arrays together with different sizes.

import numpy as np
import scipy as sp


def fun2(x: np.ndarray, serval: int = 7):
    # Range for the taylor series
    z = np.arange(1, serval)

    v = np.pow.outer(x, z)/sp.special.factorial(z)

    u = np.sum(v, axis=-1)
    y = (3*x - 10)*(1 + u)
    return y


def demo() -> None:
    rand = np.random.default_rng(seed=0)
    print(fun2(x=rand.random(20)))


if __name__ == '__main__':
    demo()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1740998535a4288977.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信