附加向量问题Python

时间:2018-11-07 21:50:05

标签: python pandas numpy machine-learning scikit-learn

我试图在第一列之前附加一个1的向量。但是,无论何时我尝试插入向量,它总是发生在数组的末尾。

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)

1 个答案:

答案 0 :(得分:1)

如@hpaulj所建议,您可以使用np.concatenate,第一个使用vectorOnes,第二个使用result1。这将按照您提供它们的顺序将两个数组连接在一起。复制代码的相关部分:

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]])

或者,您可以使用np.append,请记住,提供数组的顺序就是数组在新数组中出现的顺序(因此,只需翻转{ {1}}和result1)。但是,我不建议这样做,因为np.append basically calls np.concatenate,并且您最好使用vectorOnes

concatenate