Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

numpy

I want to add one element to a vector which is geting:

import time
from numpy import *
from scipy.sparse.linalg import bicgstab,splu
from scipy.sparse import lil_matrix,identity
from scipy.linalg import lu_factor,lu_solve,cho_factor,cho_solve
from matplotlib.pyplot import *

 #N,alfa and beta are introduced

    M = lil_matrix((2*N,2*N), dtype='float64')
    b=zeros(2*N)
    M.setdiag(alfa*ones(2*N),0)
    M.setdiag(beta*ones(2*N-1),1)
    M.setdiag(beta*ones(2*N-1),-1)
    M.setdiag(ones(2*N-2),2)
    M.setdiag(ones(2*N-2),-2)
    M=M.tocsc()

    for i in range(0,2*N):
        b[i]=2*dx*feval(fuente,x[i])/6.0

    A=1.0/(3.0*dx)*M
    u=bicgstab(A,b)
    usol=u[0]

And now I want usol.insert(0,1) usol=[1,usol[0],usol[1],..] but I have an error 'numpy.ndarray' object has no attribute 'insert'

like image 337
Kotheslayer Avatar asked Aug 04 '15 09:08

Kotheslayer


Video Answer


2 Answers

In numpy, insert is a function, not a method. So you will have to use the following:

import numpy as np
#rest of your code
usol=np.insert(usol,0,1)

This will create a copy of usol with values inserted. Note that insert does not occur in-place. You can see the docs here

like image 72
user3636636 Avatar answered Oct 03 '22 04:10

user3636636


insert is not an attribute of the array. You can use usol=insert(usol,0,1) to get the result you want.

like image 45
Christoph Avatar answered Oct 03 '22 06:10

Christoph