简体   繁体   English

Python错误:“ numpy.float64”对象没有属性“ append”

[英]Python Error: 'numpy.float64' object has no attribute 'append'

I am trying to run a simulation in which I do the following: 我正在尝试执行以下模拟操作:

  1. Take 2000 random samples from a uniform distribution between 0 and 1 从0到1之间的均匀分布中提取2000个随机样本
  2. Calculate the difference, du, between any sample and the one chosen before it 计算任何样本与其之前选择的样本之间的差du
  3. Use that difference to calculate r=EXP(-a*du) 使用该差值计算r = EXP(-a * du)
  4. Compare another random sample z to the calculated value of r 将另一个随机样本z与r的计算值进行比较
  5. Create a list of the random samples for which r>z, and discard all others 创建r> z的随机样本的列表,并丢弃所有其他样本
  6. Repeat this process until 2000 samples have been "accepted" 重复此过程,直到“接受”了2000个样本为止

Here is what I have thus far. 到目前为止,这就是我所拥有的。 When I run this code, I receive the error message "'numpy.float64' object has no attribute 'append'". 运行此代码时,收到错误消息“'numpy.float64'对象没有属性'append'”。 Any ideas on how to fix this issue? 有关如何解决此问题的任何想法?

import numpy as np                                                          
import matplotlib.pyplot as plt                                             
import math                                                                 

NP=np.random.uniform(0,1,size=(2000,))                                      
a=np.linspace(0.1,2,num=20)                                                 

for i in range(len(a)):                                                     
    dr = []                                                                 
    du = []                                                                 
    for j in range(1999):                                                   
        du=N[j+1]-N[j]                                                      
        r=math.exp(-a[i]*du)                                                
        z=np.random.uniform(0,1)                                            
        if r>z:                                                             
            du.append(N[j+1])                                               
            dr.append(r)

You are using the same variable name, du , for two distinct concepts (the container for all the valid du values, and each individual du value per iteration). 对于两个不同的概念(所有有效du值的容器以及每次迭代的每个du值的容器),您将使用相同的变量名du

Change your code to du_values = [] and du_values.append(N[j + 1]) and it should work. 将代码更改为du_values = []du_values.append(N[j + 1]) ,它应该可以工作。

As an aside, there appears to be a typo in your code - you define the original array as NP , but later refer to it as N . 顺便说一句,您的代码中似乎有一个错别字-您将原始数组定义为NP ,但后来将其称为N

Note that since you did not provide any code, the code I am going to show you is not FOR you but code I have used and SOUNDS like you could easily adopt to to your needs. 请注意,由于您未提供任何代码,因此我将向您展示的代码不是FOR您,而是我已使用的代码,而SOUNDS就像您可以轻松地满足您的需求。

 # this will simulate 1000 different combinations of my portfolio
    for x in range(1000):
        weights = np.random.random(len(tickers))
        weights /= np.sum(weights)
        portfolio_returns.append(np.sum(weights * log_returns.mean()) * 250)
        portfolio_volatilities.append(np.sqrt(np.dot(weights.T, np.dot(log_returns.cov() * 250, weights))))

What this code is doing is short is 1000x it is creating random numbers and weights for my data points, and then appending the result.The code won't be uniform as it is random, but if you really wanted uniform you could simply step through the increments. 该代码的作用是缩短1000倍,它是为我的数据点创建随机数和权重,然后附加结果。代码不是统一的,因为它是随机的,但是如果您真的想要统一,则可以简单地逐步完成增量。 However I don't think you want uniform, but rather a large enough sample size to not have it be thrown off by outliers. 但是,我不希望您想要统一的方法,而是要有足够大的样本量,以免被异常值抛弃。

A way to compare the results would be something like this. 比较结果的方法将是这样的。

simple_return = (mydata / mydata.shift(1)) - 1

Comparing random samples should be pretty straightforard, so if you do need help follow up. 比较随机样本应该是很简单的,因此,如果需要帮助,请跟进。 And with numpy arrays you can also filter/remove items based on criteria. 使用numpy数组,您还可以根据条件过滤/删除项目。

Sorry if these do not answer exactly what you are wanting, but it should get you headed in the right direction. 很抱歉,如果这些回答不能完全满足您的需求,那么您应该朝正确的方向前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM