[Python] AttributeError: 'Float' object has no attribute 'sqrt'

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    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)

    Continue reading...

Compartilhe esta Página