这是我的np数组中的一些数据:
[2002 675 456 111]
[2003 682 472 101]
[2004 731 497 129]]
我想创建一个矩阵,其中第一列是列表年龄=范围(15,50),第一行是年份(2002,2003,...)
答案 0 :(得分:1)
您可以使用csv模块或pandas库来执行该任务。但是如果你想进行快速测试,你可能会发现这段代码很有用:
D = rbind(c(2,0,0),c(0,0,0),c(0,0,0))
# [,1] [,2] [,3]
#[1,] 2 0 0
#[2,] 0 0 0
#[3,] 0 0 0
d <- rep(0,3)
#[1] 0 0 0
expandQPObj(D,d)
# "1/2*((x1*2+x2*0+x3*0)*x1+(x1*0+x2*0+x3*0)*x2+(x1*0+x2*0+x3*0)*x3) - 0*x1+0*x2+0*x3"
这就是你得到的:
data = np.asarray([[2002, 675, 456, 111],
[2003, 682, 472, 101],
[2004, 731, 497, 129]])
ages = np.arange(15, 50, dtype=data.dtype)
arr = np.zeros((1 + ages.size, 1 + data.shape[0]), dtype=data.dtype)
arr[0:data.shape[1], 1:] = data.T
arr[1:, 0] = ages
希望这有帮助