How can I use Python to convert an adjacency matrix to a transition matrix? - python-3.x

I am trying to convert a matrix like
1 1 0
0 1 1
0 1 1
to become
1 ⅓ 0
0 ⅓ ½
0 ⅓ ½
I was thinking about summing the rows and then dividing by them, but I was wondering if there was a better way to accomplish this using numpy or any other way in Python.

You can do it using numpy like below
import numpy as np
arr = np.array([[1, 1, 0],
[0, 1, 1],
[0, 1, 1]])
print(arr/arr.sum(axis=0))
[[1.0.33333333 0.]
[0.0.33333333 0.5]
[0.0.33333333 0.5]]

Related

Create a matrix from another matrix in Python 3.11

I need to create two new numpy.array matrix by using only the odd elements from another matrix for one, and the even elements for the other, and insert zeroes in the positions that aren't even or odd in the respective matrixes. How can I do that?
I tried accessing the indexes of the elements directly but this method doesn't seem to work with arrays.
Example input:
1 2 3
4 5 6
7 8 9
should yield two matrixes like:
0 2 0 1 0 3
4 0 6 and 0 5 0
0 8 0 7 0 9
You can use:
is_odd = a%2
odd = np.where(is_odd, a, 0)
even = np.where(1-is_odd, a, 0)
output:
# odd
array([[1, 0, 3],
[0, 5, 0],
[7, 0, 9]])
# even
array([[0, 2, 0],
[4, 0, 6],
[0, 8, 0]])

How to convert a 2d numpy array into a 1d numpy array by summing the values and not using for loop?

Is there a numpy function which can combine a 2d numpy array into a 1d numpy array. I want to do it without using a for loop.
Example:
[[1 0 0 0 0], [0 1 0 0 0]] => [1 1 0 0 0]
Just use the ndarray method sum along row axis:
arr2d = np.array([[1, 3, 8, 2, 0], [0, 1, 0, 5, 1]])
arr1d = arr2d.sum(axis=0)
>>> array([1, 4, 8, 7, 1])

Check if any row has the same values as a numpy array

I am working with a pandas.Dataframe that looks as follows:
A B C D
index
1 0 0 0 1
2 1 0 0 1
3 ...
4 ...
...
And I am creating a numpy.arrays that have the same shape as a row within this dataframe. I want to check if the array I am creating 'is present' within the dataframe.
In this case, for example, my array would look like this, if it is in the dataframe:
a= [0,0,0,1]
It is not if it looks like this:
b = [1,1,1,1]
Any help, even if it is a link to the right answer, is much appreciated as I have looked through stackoverflow and fortunately I did not miss anything.
df = pd.DataFrame({'A':[0, 1, 0, 0],
'B':[0, 0, 1, 1],
'C':[0, 0, 0, 0],
'D':[1, 1, 0, 1]})
# A B C D
# 0 0 0 0 1
# 1 1 0 0 1
# 2 0 1 0 0
# 3 0 1 0 1
>>> a = [0, 0, 0, 1]
>>> (df == a).all(axis=1).any()
True
>>> b = [1, 1, 1, 1]
>>> (df == b).all(axis=1).any()
False

Is there a way to extract code that constructs a data frame from the data frame?

I am looking for a way to extract code that constructs a data frame, from the loaded data frame.
Consider the following process.
# Code to construct a df:
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
'num_wings': [2, 0, 0, 0],
'num_specimen_seen': [10, 2, 1, 8]},
index=['falcon', 'dog', 'spider', 'fish'])
# Obtain the df output:
df
num_legs num_wings num_specimen_seen
falcon 2 2 10
dog 4 0 2
spider 8 0 1
fish 0 0 8
I am looking for an automatized reverse process. Suppose, I start with the df, which I load from a csv file (example below, same df as above).
df =
pd.read_csv('/path_to_data/df.csv', sep='\t')
df
num_legs num_wings num_specimen_seen
falcon 2 2 10
dog 4 0 2
spider 8 0 1
fish 0 0 8
At this point, is there a way to extract the code (listed below), that would construct the df, assuming that I did not have the code to begin with.
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
'num_wings': [2, 0, 0, 0],
'num_specimen_seen': [10, 2, 1, 8]},
index=['falcon', 'dog', 'spider', 'fish'])
This is not always useful, but I am curious if this can be done, for certain portability purposes. For instance, this would allow sharing one jupyter notebook document, without referencing anything external. And allow for a fully self-sustained replicability of data analysis.
You can get this information using df.to_dict('list') and df.index respectively:
In [9]: df
Out[9]:
num_legs num_wings num_specimen_seen
falcon 2 2 10
dog 4 0 2
spider 8 0 1
fish 0 0 8
In [10]: df.to_dict('list')
Out[10]:
{'num_legs': [2, 4, 8, 0],
'num_wings': [2, 0, 0, 0],
'num_specimen_seen': [10, 2, 1, 8]}
In [11]: df.index
Out[11]: Index(['falcon', 'dog', 'spider', 'fish'], dtype='object')
In [12]: new_df = pd.DataFrame(df.to_dict('list'), index=df.index)
In [13]: new_df
Out[13]:
num_legs num_wings num_specimen_seen
falcon 2 2 10
dog 4 0 2
spider 8 0 1
fish 0 0 8

pandas df from a dictionary of list

I have a dictionary of a two-elements list and I would like to transform it into a 3 columns pandas df.
This dict
{
'Abg': [2, 0],
'Aidi': [1, 2],
'Geng': [0, 0],
}
into this df
0 1 2
Arg 2 0
Aidi 1 2
Geng 0 0
How do I do that?
Solution found:
pd.DataFrame.from_items(name_dict.items(),
orient='index',
columns=['A','B'])

Resources