这样
我 |
矩阵 |
向 |
从 |
的 |
想 |
一个 |
数据 |
创建 |
的 |
有 |
邻接 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
及时。
邻接矩阵应反映以下逻辑:
对于x1列:
1 应该在x2列中的3行,
22 应该转到X2列中的3行,
3 应该转到3行在X2列
X2列中:X3列相同的模式。
这对于所有列。因此,就像将给定列中的每个元素链接到以下列的所有元素一样,依此类推。
输出应为具有列和行n x n的矩阵(其中整个矩阵中唯一值的n中的n)和...嗯,邻接矩阵。
此数据框只是一个示例,我必须使用的数据框有数百列。
对于这8列,输出应类似于这样:
|
1 |
2 |
3 |
5 |
22 |
23 |
1 |
6 |
1 |
0 |
0 |
0 |
0 0 |
0 |
0 |
0 |
2 |
0 |
0 0 |
0 0 |
0 1 |
0 |
3 |
4 1 4 |
1 |
0 |
1 |
5 |
0 |
1 |
0 |
1 |
0 1 |
0 |
2 2 2 2 0 1 |
0 |
2 0 2 |
0 |
2 |
0 |
2 |
2 |
0 0 |
0 |
0 |
0 |
0 |
0 0 |
这是图表的表现。 (编辑)
我一直在努力使它起作用,但是现在真的迷失了...
Tia
P.S.我正在与R一起工作,但Python也可以工作。
I want to create a directed adjacency matrix from data like this:
x1 |
x2 |
x3 |
x4 |
x5 |
x6 |
x7 |
x8 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
2 |
22 |
22 |
22 |
3 |
3 |
3 |
2 |
3 |
3 |
3 |
3 |
5 |
5 |
2 |
3 |
23 |
Where the columns represent states in time.
The adjacency matrix should reflect the following logic:
For the column x1:
1 should go to the 3 rows in column x2,
22 should go to the 3 rows in column x2,
3 should go to the 3 rows in column x2
For the column x2: The same pattern going to column x3.
And this for all columns. So it's like linking each element in a given column to all elements of the following column, and so on.
The output should be a matrix with columns and rows N x N (where N in the number of unique values in the whole matrix) and... well, an adjacency matrix.
This dataframe is just a sample, the one I have to use has hundreds of columns.
For these 8 columns, the output should resemble something like this:
|
1 |
2 |
3 |
5 |
22 |
23 |
1 |
6 |
1 |
0 |
0 |
0 |
0 |
2 |
0 |
0 |
2 |
0 |
0 |
0 |
3 |
0 |
1 |
4 |
1 |
0 |
1 |
5 |
0 |
1 |
0 |
1 |
0 |
0 |
22 |
0 |
0 |
1 |
0 |
2 |
0 |
23 |
0 |
0 |
0 |
0 |
0 |
0 |
This is a representation of how the graph should look like. (edited)

I've been trying to make it work, but am really lost by now...
TIA
P.S. I'm working with R but Python could also work.
发布评论
评论(4)
我认为邻接矩阵不是您所追求的。我想这应该是过渡的摘要信息。您可以在下面尝试基本R代码(无
igraph
),该代码给出
data
I don't think the adjacency matrix is the thing you are after. I guess it should be the summary info of transitions. You can try the base R code below (without
igraph
)which gives
data
你可以做:
You could do:
看来您可能会误解邻接矩阵的工作原理。
该矩阵包含布尔值(是或错误),
应为节点索引1,2,3,4,...
如果从节点1到节点2有一个链接,则第2行,第1列中的单元格将是真实的。
让我们索引您的前两个列,
所以节点1链接到节点4,5,而6个
邻接矩阵看起来像这样
It seems that you may misunderstand how an adjacency matrix works.
The matrix contains Boolean values ( true or false )
The nodes should be indexed 1,2,3,4, ...
If there is a link from node 1 to node 2, then the cell in row 2, column 1 will be true.
Let's index your first two columns like this
So node 1 is linked to nodes 4,5, and 6
and the adjacency matrix looks like this
从
@thomasiscoding
的数据框开始。第一个替代方法是将所有节点组合在一起,而无需考虑时间(x1,x2,...)。
输出。
替代(ii)考虑了观察时间。
输出。
Expand.Grid()将X(i)处的所有发生与i = 1至7的x(i+1)结合在一起。
根据手头的情况选择M1或M2。
输出(i)。使用
表(MMM)
检查此输出。输出(ii)。
Starting with the dataframe of
@ThomasisCoding
.The first alternative is to combine all nodes without regard to time (x1, x2, ...).
Output.
Alternative (II) takes into account the time of observation.
Output.
Expand.grid() combines all occurrences at x(i) with x(i+1) for i = 1 through 7.
Choose m1 or m2 depending on scenario at hand.
Output (I). Check this output with
table(mmm)
.Output (II).