如何摆脱Python中过多的嵌套阵列

发布于 2025-01-20 04:14:36 字数 1779 浏览 2 评论 0原文

嵌套阵列

我想将上述变成以下。当我进行线性回归时,这是偶然发生的,即输出已经在1x1数组中,让我知道您是否想查看更多我的代码。看来我的beta变量是嵌套的问题。

正常阵列

获取输出

一般来说,我只是在尝试从[[array([x x x) ]),数组([x]),数组([x]),阵列([x]),array([x])]]

to
[[x,x,x,x,x]]

def si_model():
    dj_data = pd.read_csv("/data.tsv", sep = "\t")
    dj_data = dj_data.pct_change().dropna()
    ann_dj_data = dj_data * 252
    dj_index = ann_dj_data['^DJI']
    ann_dj_data = ann_dj_data.drop('^DJI', axis='columns')
    
      # Function to Linear Regress Each Stock onto DJ
    def model_regress(stock):
        # Fit DJ to Index Data
        DJ = np.array(dj_index).reshape(len(stock), 1)

        # Regression of each stock onto DJ
        lm = LinearRegression().fit(DJ, y=stock.to_numpy())
        resids = stock.to_numpy() - lm.predict(DJ)

        return lm.coef_, lm.intercept_, resids.std()
    
    # Run model regression on each stock
    lm_all = ann_dj_data.apply(lambda stock: model_regress(stock)).T

    # Table of the Coeffeicents
    lm_all = lm_all.rename(columns={0: 'Beta       ', 1: 'Intercept', 2: 'Rsd Std'})
        
    # Varaince of the index's returns
    dj_index_var = dj_index.std() ** 2
        
    betas = lm_all['Beta       '].to_numpy()
    resid_vars = lm_all['Rsd Std'].to_numpy() ** 2
    
    # Single index approximation of covariance matrix using identity matrix (np.eye)
    Qsi = dj_index_var * betas * betas.reshape(-1, 1) + np.eye(len(betas)) * resid_vars
    return Qsi

# Printing first five rows of approximation
Qsi = si_model()
print("Covariance Matrix")
print(Qsi[:5, :5])

Nested Array

I want to turn the above into the below. This accidentally happened as I was doing a linear regression that the output was already in a 1x1 array, let me know if you would like to see more of my code. It looks like my betas variable is the issue with the nesting.

Normal Array

Generally speaking, I am just trying to get the output from

[[ array([x]), array([x]), array([x]), array([x]), array([x])]]

to
[[x, x, x, x, x ]]

def si_model():
    dj_data = pd.read_csv("/data.tsv", sep = "\t")
    dj_data = dj_data.pct_change().dropna()
    ann_dj_data = dj_data * 252
    dj_index = ann_dj_data['^DJI']
    ann_dj_data = ann_dj_data.drop('^DJI', axis='columns')
    
      # Function to Linear Regress Each Stock onto DJ
    def model_regress(stock):
        # Fit DJ to Index Data
        DJ = np.array(dj_index).reshape(len(stock), 1)

        # Regression of each stock onto DJ
        lm = LinearRegression().fit(DJ, y=stock.to_numpy())
        resids = stock.to_numpy() - lm.predict(DJ)

        return lm.coef_, lm.intercept_, resids.std()
    
    # Run model regression on each stock
    lm_all = ann_dj_data.apply(lambda stock: model_regress(stock)).T

    # Table of the Coeffeicents
    lm_all = lm_all.rename(columns={0: 'Beta       ', 1: 'Intercept', 2: 'Rsd Std'})
        
    # Varaince of the index's returns
    dj_index_var = dj_index.std() ** 2
        
    betas = lm_all['Beta       '].to_numpy()
    resid_vars = lm_all['Rsd Std'].to_numpy() ** 2
    
    # Single index approximation of covariance matrix using identity matrix (np.eye)
    Qsi = dj_index_var * betas * betas.reshape(-1, 1) + np.eye(len(betas)) * resid_vars
    return Qsi

# Printing first five rows of approximation
Qsi = si_model()
print("Covariance Matrix")
print(Qsi[:5, :5])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后eg是否自 2025-01-27 04:14:36

您可以使用 squeeze()

这是一个类似于您的小例子:

import numpy as np

a = np.array([17.1500691])
b = np.array([5.47690856])
c = np.array([5.47690856])
d = np.array([11.7700696])

e = list([[a,b],[c,d]])
print(e)

f = np.squeeze(np.array(e), axis=2)
print(f)

输出:

[[array([17.1500691]), array([5.47690856])], [array([5.47690856]), array([11.7700696])]]
[[17.1500691   5.47690856]
 [ 5.47690856 11.7700696 ]]

You can use squeeze().

Here is a small example similar to yours:

import numpy as np

a = np.array([17.1500691])
b = np.array([5.47690856])
c = np.array([5.47690856])
d = np.array([11.7700696])

e = list([[a,b],[c,d]])
print(e)

f = np.squeeze(np.array(e), axis=2)
print(f)

Output:

[[array([17.1500691]), array([5.47690856])], [array([5.47690856]), array([11.7700696])]]
[[17.1500691   5.47690856]
 [ 5.47690856 11.7700696 ]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文