我如何迭代下面的方程来确定根

How do I Iterate the below equation to determine the roots

上一个问题

如何使用代码确定为以下方程的 sigma 循环迭代的最佳方法。

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.

异常发生是因为concatenate is a NumPy function not a numpy.ndarray方法。所以你必须称它为:

np.concatenate(arrays)

但是它在您的代码中并没有真正意义,因为您已经在内部循环中重新分配了 k。您可能想要 appendk-列表,因此您需要不同的变量名称:

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.

不确定这是否仍然满足您的需要。