2 views

I want to convert the R code to Python so that I can plot a curve. I've tried using the map(), but it is not executing in Matplotlib as I am getting the error that it does not support generators as input. I have also executed the list(map()) and get the error that 'int' object is not iterable.

Kindly refer to the following code in R:

r <- y - (Beta0+Beta1*tt+Beta2*tt^2)

sum(r^2)

}

Beta2s <- seq(-10,0,len=100)

Here's my attempt in Python:

def rss(Beta0, Beta1, Beta2):

r = y - (Beta0 + Beta1*t + Beta2*t**2)

return np.sum(r**2)

Beta2s = np.linspace(-10, 0, num = 100)

Beta0 = 55

Beta1 = 0

plt.show()

by (108k points)

Let' say, you are having the following code in R programming:

tt <- seq(1,10,length.out=100)

y <- seq(1,10,length.out=100)

Beta0 = 55

Beta1 = 0

Beta2s <- seq(-10,0,len=100)

[1] 19223571 18806870 18394761 17987243 17584318 17185985

And the following in python:

Beta2s = np.linspace(-10, 0, num = 100)

Beta0 = 55

Beta1 = 0

y = np.linspace(1,10,100)

t = np.linspace(1,10,100)

If you want to plot then just use a list comprehension like the following:

plt.plot(Beta2s,[rss(Beta0,Beta1,i) for i in Beta2s])

Or you can also vectorize the function:

array([19223570.88655147, 18806869.74602632, 18394760.55678168,

17987243.31881757, 17584318.03213398])