gpt4 book ai didi

python - 用向量 [n,] 填充逻辑矩阵 [r,n]

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:39 26 4
gpt4 key购买 nike

我有一个数值向量( Pandas 数据框df中的一系列)。

idx     values

0 NaN
1 1
2 2
3 NaN
4 NaN
5 33
6 34
7 90
8 NaN
9 5
10 NaN
11 22
12 70
13 NaN
14 672
15 10
16 73
17 9
18 NaN
19 15

然后我构造了一个逻辑矩阵的形式

array([[1, 1, 1, ..., 0, 0, 0],
[0, 1, 1, ..., 0, 0, 0],
[0, 0, 1, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 1, 0, 0],
[0, 0, 0, ..., 1, 1, 0],
[0, 0, 0, ..., 1, 1, 1]])

使用从 SO 上的一些答案中获取的以下代码,遗憾的是找不到了。

n=len(df)
k=5
r= n-k+1
mat=np.tile([1]*k+[0]*r, r)[:-r].reshape(r,n)

mat 的形状为 (r,n)df['values'] 的形状为 (n,)

df['values'] 中的值填充 mat 的正确方法是什么?

鉴于前面的例子,我的预期输出是:

array([[NaN, 1, 2, NaN,       ..., 0, 0, 0],
[ 0, 1, 2,NaN,NaN, ..., 0, 0, 0],
[ 0, 0, 2,NaN,NaN,33, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 672, 10, 73, 9, 0, 0],
[0, 0, 0, ..., 10,73, 9, NaN, 0],
[0, 0, 0, ..., 73, 9, NaN, 15]])

关于如何实现这一点有什么建议吗?我尝试了一个点积(希望它会像在 matlab 中一样运行并复制我的向量 r 次但没有成功。

最佳答案

您可以使用 numpy.apply_along_axisnumpy.where :

#!/usr/bin/env python3

import numpy as np
import pandas as pd

nan = np.nan

df = pd.DataFrame([
nan, 1, 2, nan, nan, 33, 34, 90,
nan, 5, nan, 22, 70, nan, 672,
10, 73, 9, nan, 15],
columns=['values'])

n = len(df)
k = 5
r = n - k + 1

mat = np.tile([1] * k + [0] * r, r)[:-r].reshape(r, n)

mat = np.apply_along_axis(lambda row: np.where(row, df['values'], row), 1, mat)

print(mat)

输出:

[[ nan   1.   2.  nan  nan   0.   0.   0.   0.   0.   0.   0.   0.   0. 0.     0.   0.   0.   0.   0.]
[ 0. 1. 2. nan nan 33. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 2. nan nan 33. 34. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. nan nan 33. 34. 90. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. nan 33. 34. 90. nan 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 33. 34. 90. nan 5. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 34. 90. nan 5. nan 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 90. nan 5. nan 22. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. nan 5. nan 22. 70. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 5. nan 22. 70. nan 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. nan 22. 70. nan 672. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 22. 70. nan 672. 10. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 70. nan 672. 10. 73. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. nan 672. 10. 73. 9. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 672. 10. 73. 9. nan 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 10. 73. 9. nan 15.]]

关于python - 用向量 [n,] 填充逻辑矩阵 [r,n],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55851684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com