Appending vector issue Python

2086 views python
-3

I'm trying to append a vector of 1's before the first column. However, anytime I try to insert the vectors, It always happens to the end of the array.

Here is the current code I have written.

x1 = np.array([0.100, 0.200, 0.250, 0.350, 0.400, 0.450, 0.500, 0.600, 
0.750, 0.800, 0.850, 0.900])

mu1 = np.array([0.000, 0.333, 0.667, 1.000])
mu2 = np.array([0.000, 0.167, 0.333, 0.500, 0.667, 0.833, 1.000])
s= 0.3
y_train = [0.603, 0.986, 0.891, 0.834, 0.572, 0.353, -0.085, 
-0.371,-0.967, -0.989, -0.749, -0.382]

y_train=np.array(y_train)
basis_function1 = [[0 for i in range(0,4)]for j in range(0,12)]
basis_function2 = [[0 for i in range(0,7)]for j in range(0,12)]
result1=[]
result2=[]



for x in x1:
    for m in mu1:
        a= np.exp(-((x-m)**2)/2*s**2)
        result1.append(a)


for x in x1:
    for m in mu2:
        a= np.exp(-((x-m)**2)/2*s**2)
        result2.append(a)

result1= np.reshape(result1, (12,4))
result2= np.reshape(result2, (12,7))
vectorOnes= np.ones((12,1))


result1 = np.append(result1,vectorOnes, axis=1)
np.insert(result1, 0, 1, axis=1)

print(result1)
print(result2)

answered question

List append always adds at the end. np.insert has different syntax and behavior. Don't confuse the two. Also we discourage the use of np.append. Learn to use np.concatenate directly.

1 Answer

11

As suggested by @hpaulj, you can use np.concatenate. Reproducing the relevant parts of your code:

result1 = np.random.random((12,4))
vectorOnes= np.ones((result1.shape[0],1))

>>> result1
array([[0.24082843, 0.31800901, 0.01556211, 0.32774249],
       [0.41475486, 0.90611202, 0.00791056, 0.49544814],
       [0.22842928, 0.97168093, 0.1808639 , 0.32310355],
       [0.99674441, 0.97379065, 0.7482597 , 0.9397243 ],
       [0.37714731, 0.94101763, 0.73416157, 0.36625995],
       [0.16470904, 0.97471554, 0.58262108, 0.67246731],
       [0.40309562, 0.88545376, 0.40600242, 0.06040476],
       [0.65425856, 0.15789502, 0.09350497, 0.49837995],
       [0.65652148, 0.00545527, 0.68244463, 0.38962242],
       [0.4012334 , 0.67545283, 0.09977628, 0.18019942],
       [0.67110475, 0.45046098, 0.24962163, 0.71436953],
       [0.32890942, 0.6090705 , 0.71712907, 0.35790405]])
>>> vectorOnes
array([[1.],
       [1.],
       [1.],
       [1.],
       [1.],
       [1.],
       [1.],
       [1.],
       [1.],
       [1.],
       [1.],
       [1.]])

new_results = np.concatenate((vectorOnes, result1),axis=1)
>>> new_results
array([[1.        , 0.24082843, 0.31800901, 0.01556211, 0.32774249],
       [1.        , 0.41475486, 0.90611202, 0.00791056, 0.49544814],
       [1.        , 0.22842928, 0.97168093, 0.1808639 , 0.32310355],
       [1.        , 0.99674441, 0.97379065, 0.7482597 , 0.9397243 ],
       [1.        , 0.37714731, 0.94101763, 0.73416157, 0.36625995],
       [1.        , 0.16470904, 0.97471554, 0.58262108, 0.67246731],
       [1.        , 0.40309562, 0.88545376, 0.40600242, 0.06040476],
       [1.        , 0.65425856, 0.15789502, 0.09350497, 0.49837995],
       [1.        , 0.65652148, 0.00545527, 0.68244463, 0.38962242],
       [1.        , 0.4012334 , 0.67545283, 0.09977628, 0.18019942],
       [1.        , 0.67110475, 0.45046098, 0.24962163, 0.71436953],
       [1.        , 0.32890942, 0.6090705 , 0.71712907, 0.35790405]])

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories