我试图在第一列之前添加一个全为1的向量。然而,每当我尝试插入这些向量时,它们总是被添加到数组的末尾。
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.3y_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)
回答:
正如@hpaulj所建议,你可以使用np.concatenate
,以vectorOnes
作为第一个参数,result1
作为第二个参数。这样会按照你提供的顺序连接这两个数组。重现你代码的相关部分:
result1 = np.random.random((12,4))vectorOnes= np.ones((result1.shape[0],1))>>> result1array([[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]])>>> vectorOnesarray([[1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.]])new_results = np.concatenate((vectorOnes, result1),axis=1)>>> new_resultsarray([[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
,但要记住你提供数组的顺序就是新数组中数组出现的顺序(所以只需交换result1
和vectorOnes
的顺序)。然而,我不推荐这种方法,因为np.append
基本上是调用np.concatenate
,你还不如直接使用concatenate
。
>>> np.append(vectorOnes, result1, axis=1)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]])