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

+1 vote

This is part of my code, why it shows error. thanks in advance

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)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-220-3a3ff7a7f33e> in <module>
      4     weights = np.random.random(noa)
      5     weights /= np.sum(weights)
----> 6     prets.append(np.sum(rets.mean() * weights) * 252)
      7     pvols.append(np.sqrt(np.dot(weights.T,
      8                                 np.dot(rets.cov() * 252, weights))))

AttributeError: 'numpy.ndarray' object has no attribute 'append'
May 17 in Python by Noman
• 220 points

edited May 18 by Gitika 2,541 views
Can you share your complete code? It is difficult to understand the flow of your code.

1 answer to this question.

0 votes

Hi@Noman,

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,480 points
u didnt gave
Can you explain your query?

Related Questions In Python

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,480 points
140 views
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,480 points
366 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,480 points
1,374 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
67 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,520 points
4,056 views
0 votes
0 answers

'module' object has no attribute 'PortScanner'

I am trying to run nmap in ...READ MORE

Jan 30, 2019 in Python by Karan
591 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
42,809 views
0 votes
1 answer
+1 vote
1 answer