shape size len;reshape;np.sum的axis

import numpy as np
print(np.rint(1.5))
print(2**3)
a=[[1,2,3,4,5],
[2,3,4,5,6]]
print(type(a))
a=np.array(a)
print(type(a))
print(a)
print(a.shape)
summed=np.sum(a,axis=0)
print(summed)
summed2=np.sum(a,axis=1)
print(summed2)

b=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
b=np.array(b)
b=b.reshape(2,4,3)
print(b.shape)
print(b.size)
print(b)
print(b.shape[0])
summed=np.sum(b,axis=0)
print(summed)
print(summed.shape)
summed2=np.sum(b,axis=1)
print(summed2)
print(summed2.shape)
summed3=np.sum(b,axis=2)
print(summed3)
print(summed3.shape)
#X的shape(x,y,z),即X.shape=(x,y,z),X.shape[0]是x x是最外层 axis0是最外层相加 加完变成(y,z)


2.0
8
<class ‘list’>
<class ‘numpy.ndarray’>
[[1 2 3 4 5]
[2 3 4 5 6]]
(2, 5)
[ 3 5 7 9 11]
[15 20]
(2, 4, 3)
24
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]

[[13 14 15]
[16 17 18]
[19 20 21]
[22 23 24]]]
2
[[14 16 18]
[20 22 24]
[26 28 30]
[32 34 36]]
(4, 3)
[[22 26 30]
[70 74 78]]
(2, 3)
[[ 6 15 24 33]
[42 51 60 69]]
(2, 4)

Process finished with exit code 0

(143,9)的DataFrame与(143.7)的DataFrame在做以下操作时import numpy as np def GM11(x0): # 灰色预测模型 x1 = np.cumsum(x0) z1 = (x1[:len(x1)-1] + x1[1:])/2.0 z1 = z1.reshape((len(z1),1)) B = np.append(-z1, np.ones_like(z1), axis=1) Y = x0[1:].reshape((len(x0)-1, 1)) [[a], [b]] = np.dot(np.dot(np.linalg.inv(np.dot(B.T, B)), B.T), Y) return (a, b) def GM11_predict(x0, a, b): # 预测函数 result = [] for i in range(1, 11): result.append((x0[0]-b/a)*(1-np.exp(a))*np.exp(-a*(i-1))) result.append((x0[0]-b/a)*(1-np.exp(a))*np.exp(-a*10)) return result # 计算灰色关联度 def Grey_Relation(x, y): x = np.array(x) y = np.array(y) x0 = x[0] y0 = y[0] x_model = GM11(x) y_model = GM11(y) x_predict = GM11_predict(x, *x_model) y_predict = GM11_predict(y, *y_model) delta_x = np.abs(x-x_predict)/np.abs(x).max() delta_y = np.abs(y-y_predict)/np.abs(y).max() grey_relation = 0.5*np.exp(-0.5*((delta_x-delta_y)**2).sum()) return grey_relation # 计算灰色关联度矩阵 def Grey_Relation_Matrix(data1, data2): matrix = [] for i in range(data1.shape[1]): row = [] for j in range(data2.shape[1]): x = data1.iloc[:, i].tolist() y = data2.iloc[:, j].tolist() grey_relation = Grey_Relation(x, y) row.append(grey_relation) matrix.append(row) return np.array(matrix) # 计算人口-经济的灰色关联度矩阵 relation_matrix = Grey_Relation_Matrix(pop_data, eco_data),发生了以下错误:operands could not be broadcast together with shapes (143,) (11,) ,请写出问题所在,并给出解决代码
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值