`ValueError: too many values to unpack (expected 4)` with `scipy.stats.linregress`

2024/9/21 18:21:21

I know that this error message (ValueError: too many values to unpack (expected 4)) appears when more variables are set to values than a function returns.

scipy.stats.linregress returns 5 values according to the scipy documentation (http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html).

Here is a short, reproducible example of a working call, and then a failed call, to linregress:

What could account for difference and why is the second one poorly called?

from scipy import stats
import numpy as npif __name__ == '__main__':x = np.random.random(10)y = np.random.random(10)print(x,y)slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)'''
Code above works
Code below fails
'''X = np.asarray([[-15.93675813],[-29.15297922],[ 36.18954863],[ 37.49218733],[-48.05882945],[ -8.94145794],[ 15.30779289],[-34.70626581],[  1.38915437],[-44.38375985],[  7.01350208],[ 22.76274892]])Y = np.asarray( [[  2.13431051],[  1.17325668],[ 34.35910918],[ 36.83795516],[  2.80896507],[  2.12107248],[ 14.71026831],[  2.61418439],[  3.74017167],[  3.73169131],[  7.62765885],[ 22.7524283 ]])print(X,Y) # The array initialization succeeds, if both arrays are print outfor i in range(1,len(X)):slope, intercept, r_value, p_value, std_err = (stats.linregress(X[0:i,:], y = Y[0:i,:]))
Answer

Your problem originates from slicing the X and Y arrays. Also you do not need the for loop. Use the following instead and it should work.

slope, intercept, r_value, p_value, std_err = stats.linregress(X[:,0], Y[:,0])
https://en.xdnf.cn/q/71923.html

Related Q&A

Django prefetch_related from foreignkey with manytomanyfield not working

For example, in Django 1.8:class A(models.Model):x = models.BooleanField(default=True)class B(models.Model):y = models.ManyToManyField(A)class C(models.Model):z = models.ForeignKey(A)In this scenario, …

Running SimpleXMLRPCServer in separate thread and shutting down

I have a class that I wish to test via SimpleXMLRPCServer in python. The way I have my unit test set up is that I create a new thread, and start SimpleXMLRPCServer in that. Then I run all the test, and…

Clean Python multiprocess termination dependant on an exit flag

I am attempting to create a program using multiple processes and I would like to cleanly terminate all the spawned processes if errors occur. below Ive wrote out some pseudo type code for what I think …

Any limitations on platform constraints for wheels on PyPI?

Are there any limitations declared anywhere (PEPs or elsewhere) about how broad a scope the Linux wheels uploaded to PyPI should have? Specifically: is it considered acceptable practice to upload li…

Match set of dictionaries. Most elegant solution. Python

Given two lists of dictionaries, new one and old one. Dictionaries represent the same objects in both lists. I need to find differences and produce new list of dictionaries where will be objects from n…

Cookies using Python and Google App Engine

Im developing an app on the Google App Engine and have run into a problem. I want to add a cookie to each user session so that I will be able to differentiate amongst the current users. I want them all…

Matplotlib animations - how to export them to a format to use in a presentation?

So, I learned how to make cute little animations in matplotlib. For example, this:import numpy as np import matplotlib import matplotlib.pyplot as pltplt.ion()fig = plt.figure() ax = fig.add_subplot(…

Where is python interpreter located in virtualenv?

Where is python intrepreter located in virtual environment ? I am making a GUI project and I stuck while finding the python interpreter in my virtual environment.

Tkinter check which Entry last had focus

I am working on a program that has a virtual keyboard I created using Tkinter. The pages that have the keyboard enabled have entry widgets where the users need to input data. I am using pyautogui as …

Python Popen grep

Id like Popen to execute:grep -i --line-buffered "grave" data/*.txtWhen run from the shell, this gives me the wanted result. If I start, in the very same directory where I test grep, a python…