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
1,451 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
• 56,940 points

Related Questions In Python

0 votes
1 answer

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

Hi@Nilesh, You have created two empty lists. And ...READ MORE

answered Aug 10 in Python by MD
• 56,940 points
380 views
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
• 56,940 points
1,396 views
+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
• 56,940 points
2,596 views
0 votes
0 answers

AttributeError: 'numpy.int32' object has no attribute 'map'

can any one suggest solution data['isLarge'] = data.size.map({'small' ...READ MORE

Sep 5 in Python by Mohammed
• 120 points
70 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 261 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
978 views
0 votes
1 answer

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

Hi@akhtar, If you trying to append data in ...READ MORE

answered May 5 in Python by MD
• 56,940 points
10,185 views
0 votes
1 answer

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

Hi@Nilesh, You created two empty lists. And in ...READ MORE

answered Aug 10 in Python by MD
• 56,940 points
145 views