5

I'm trying to utilize the scipy.stats.linregress() function. I double checked that both my inputs are the same size and both are arrays. However I keep getting the following error:

Traceback (most recent call last):
  File "C:/Users/chianh/Desktop/Reliability Py Scripts/MedianRanks_Solver_Rev0.py", line 48, in <module>
    slope, intercept, r_value, p_value, std_err = stats.linregress(data, median_rank)
  File "C:\Users\chianh\AppData\Local\Programs\Python\Python35-32\lib\site-packages\scipy\stats\_stats_mstats_common.py", line 81, in linregress
    r_den = np.sqrt(ssxm * ssym)
AttributeError: 'Float' object has no attribute 'sqrt'

The code that i wrote is pasted below:

import numpy as np
import matplotlib.pyplot as plot
import scipy.stats as stats
from sympy import Symbol, binomial, expand_func, solve, solveset

def eval_median_rank( mylist ):
    P = 0.5                         # Percentage Point (would be 0.5 to be median)
    Z = Symbol('Z', real=True)      # Median Rank (Should be solved for each test unit)
    result = []                     # Place holder for the evaluated Median Ranks

    # Re-iterate and perform for each data point
    for j in range(1, mylist.size+1):
        eq = 0
        k = j

        # Generate the Median Rank equation which will be solved for 'Z' or Median Rank
        while k <= mylist.size:
            eq = eq + expand_func(binomial(mylist.size, k) * (Z**k) * (1 - Z)**(mylist.size-k))
            k += 1
        eq = eq - P

        # Solve for only Median Rank solutions that only fall between 0 and 1.
        # Then append to the returning list
        sol = [i for i in solve(eq, Z, list=True) if (i > 0) and (i < 1)]
        result.extend(sol)

    result.sort(reverse=True)
    return np.array(result)

######################################################################################################



# Note that the samples must be ordered in ascending fail-time/UOM,
# and the system must be solved in that order.

data = np.array([96, 257, 498, 763, 1051, 1744], dtype=object)
rank = np.arange(1, data.size+1)
median_rank = eval_median_rank(rank)
print(median_rank)

# Use Scipy's stats package to perform Least-Squares fit
slope, intercept, r_value, p_value, std_err = stats.linregress(data, median_rank)
7
  • Possible duplicate? stackoverflow.com/questions/18833639/… Commented Apr 13, 2016 at 20:59
  • 2
    Don't pass sympy objects to scipy functions. scipy doesn't handle sympy's Float object. Commented Apr 14, 2016 at 3:03
  • Would it be able to handle numpy arrays? Commented Apr 19, 2016 at 13:22
  • did you manage to solve your issue? Commented Aug 16, 2017 at 4:46
  • 2
    @CharlieParker: Yes, that should work. Commented Aug 16, 2017 at 4:48

1 Answer 1

-1

The error you're getting is caused because you're mixing SymPy objects with NumPy/SciPy, and SciPy's linregress() expects float or numpy.float64 types—not sympy.Float.

You're using dtype=object, and more importantly, in your eval_median_rank function, you're solving equations using SymPy, which returns results as sympy.Float, not Python float.

As a result, median_rank becomes a NumPy array of sympy.Float elements. When linregress() tries to compute things like np.sqrt(), it fails because sympy.Float doesn’t have .sqrt() (NumPy expects regular floats).

Solution:

Add this at the end of eval_median_rank():

return np.array(result, dtype=float)

This ensures the output is pure NumPy float64, fully compatible with SciPy.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.