Search code examples
pythonscipycurve-fittingnewtons-method

How do I Iterate the below equation to determine the roots


Previous Question

How to determine the best way to iterate through a loop for a sigma for the below equation using the code.

import statistics as stats
import warnings
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy
import scipy.stats
import sympy
from scipy import stats as st
from itertools import islice, count
import itertools

def func_shape_factor(vi, k):
k_list = []  # other name
for vi in np.nditer(vi, flags=['buffered'], op_dtypes=['float64']):
    # k initial guess of 1.5, changable
    k = 1.5
    k = (np.nan_to_num((np.sum((vi) ** (k) * np.log(vi)) / np.sum((vi) ** \
        (k)) - ((np.sum(np.log(vi)) / len(vi))))))
    k_list.append(k)  # append k to the list of ks
return np.array(k)  # cast it to an array after the loop.

Solution

  • The exception happens because concatenate is a NumPy function not a numpy.ndarray method. So you have to call it as:

    np.concatenate(arrays)
    

    However it doesn't really make sense in your code because you already re-assigned k in the inner-loop. You probably want to append to the k-list so you need different variable names:

    def func_shape_factor(vi, k):
        k_list = []  # other name
        for vi in np.nditer(vi, flags=['buffered'], op_dtypes=['float64']):
            # k initial guess of 1.5, changable
            k = 1.5
            k = (np.nan_to_num((np.sum((vi) ** (k) * np.log(vi)) / np.sum((vi) ** (k)) - ((np.sum(np.log(vi)) / len(vi))))))
            k_list.append(k)  # append k to the list of ks
        return np.array(k)  # cast it to an array after the loop.
    

    Not sure if that still does what you need though.