AttributeError: 'numpy.ndarray' object has no attribute 'append'

+1 vote

this is my part of a code , whey it shows :AttributeError: 'numpy.ndarray' object has no attribute 'append'

prets = []
pvols = []
for p in range (2500):
    weights = np.random.random(noa)
    weights /= np.sum(weights)
    prets.append(np.sum(rets.mean() * weights) * 252)
    pvols.append(np.sqrt(np.dot(weights.T,
                                np.dot(rets.cov() * 252, weights))))
    prets = np.array(prets)
    pvols = np.array(pvols)

May 17 in Python by anonymous
• 220 points
288 views

1 answer to this question.

0 votes

Hi,

You created two empty list. And in the for loop you tried to append values in your list. But in the last part you tried to convert your list into numpy array using the same variable name. So I suggest you to change your variable name as given below.

prets = []
pvols = []
for p in range (2500):
    weights = np.random.random(noa)
    weights /= np.sum(weights)
    prets.append(np.sum(rets.mean() * weights) * 252)
    pvols.append(np.sqrt(np.dot(weights.T,
                                np.dot(rets.cov() * 252, weights))))
    pret = np.array(prets)
    pvol = np.array(pvols)

Make this changes in your code and try once again.

answered May 17 by MD
• 24,500 points

Related Questions In Python

+1 vote
1 answer

'numpy.ndarray' object has no attribute 'append'

Hi@Noman, You created two empty list. And in ...READ MORE

answered May 17 in Python by MD
• 24,500 points
329 views
0 votes
1 answer

Getting AttributeError: 'module' object (scipy) has no attribute 'misc' in Python. How to solve this?

>>> import scipy >>> scipy.misc Traceback (most recent call ...READ MORE

answered Dec 24, 2018 in Python by Nymeria
• 3,540 points
3,070 views
0 votes
1 answer
0 votes
1 answer

Python Pandas error: AttributeError: 'DataFrame' object has no attribute 'rows'

There is no attribute called “rows”. The ...READ MORE

answered Mar 28, 2019 in Python by Shri
28,792 views
+1 vote
3 answers

What are the ways of detecting outliners in Python

code from http://eurekastatistics.com/using-the-median-absolute-deviation-to-find-outliers  This uses the L1 distance ...READ MORE

answered Aug 24, 2018 in Python by eatcodesleeprepeat
• 4,750 points

reshown Aug 24, 2018 by Priyaj 189 views
0 votes
1 answer
0 votes
1 answer

How to create Pandas series from numpy array?

Hi. Refer to the below command: import pandas ...READ MORE

answered Apr 1, 2019 in Python by Pavan
650 views
0 votes
1 answer
0 votes
1 answer

AttributeError: type object 'numpy.ndarray' has no attribute '__array_function__'

Hi@akhtar, I think numpy version is not compatible ...READ MORE

answered Apr 13 in Python by MD
• 24,500 points
196 views