Is there a way to find the probability of different values in matrices












1















I thought there would be a library that would help me to do this task instead of writing many lines of codes. I tried finding some solutions from books related to my problem, but I could not find any.



One of the recent books I did read related to probability:




Python for Probability, Statistics, and Machine Learning for José
Unpingco




The task is that I have a matrix like this one below



0    1
213 vha
342 gha
523 xha
121 gha
812 gha
612 vha
123 gha


and I want the program to calculate the steps of moving from, say, vha to gha in the second row. and from gha to xha in the third row. If any step is repeated, it will be added to the previous step. For example vha to gha in the first and second rows is repeated at the end of the matrix.



The desired output is will be the similar steps added together/ total number of rows-1. In the first case it is vha to gha prob = 2/7-1



Desired output



vha to gha prob = 0.3
gha to xha prob = 0.16
xha to gha prob = 0.16
gha to gha prob = 0.16
gha to vha prob = 0.16

Total probs = 1









share|improve this question


















  • 1





    What datatype is this "matrix"?

    – timgeb
    Nov 11 '18 at 20:59











  • it is <class 'list'>

    – Abdulaziz Al Jumaia
    Nov 11 '18 at 21:04


















1















I thought there would be a library that would help me to do this task instead of writing many lines of codes. I tried finding some solutions from books related to my problem, but I could not find any.



One of the recent books I did read related to probability:




Python for Probability, Statistics, and Machine Learning for José
Unpingco




The task is that I have a matrix like this one below



0    1
213 vha
342 gha
523 xha
121 gha
812 gha
612 vha
123 gha


and I want the program to calculate the steps of moving from, say, vha to gha in the second row. and from gha to xha in the third row. If any step is repeated, it will be added to the previous step. For example vha to gha in the first and second rows is repeated at the end of the matrix.



The desired output is will be the similar steps added together/ total number of rows-1. In the first case it is vha to gha prob = 2/7-1



Desired output



vha to gha prob = 0.3
gha to xha prob = 0.16
xha to gha prob = 0.16
gha to gha prob = 0.16
gha to vha prob = 0.16

Total probs = 1









share|improve this question


















  • 1





    What datatype is this "matrix"?

    – timgeb
    Nov 11 '18 at 20:59











  • it is <class 'list'>

    – Abdulaziz Al Jumaia
    Nov 11 '18 at 21:04
















1












1








1








I thought there would be a library that would help me to do this task instead of writing many lines of codes. I tried finding some solutions from books related to my problem, but I could not find any.



One of the recent books I did read related to probability:




Python for Probability, Statistics, and Machine Learning for José
Unpingco




The task is that I have a matrix like this one below



0    1
213 vha
342 gha
523 xha
121 gha
812 gha
612 vha
123 gha


and I want the program to calculate the steps of moving from, say, vha to gha in the second row. and from gha to xha in the third row. If any step is repeated, it will be added to the previous step. For example vha to gha in the first and second rows is repeated at the end of the matrix.



The desired output is will be the similar steps added together/ total number of rows-1. In the first case it is vha to gha prob = 2/7-1



Desired output



vha to gha prob = 0.3
gha to xha prob = 0.16
xha to gha prob = 0.16
gha to gha prob = 0.16
gha to vha prob = 0.16

Total probs = 1









share|improve this question














I thought there would be a library that would help me to do this task instead of writing many lines of codes. I tried finding some solutions from books related to my problem, but I could not find any.



One of the recent books I did read related to probability:




Python for Probability, Statistics, and Machine Learning for José
Unpingco




The task is that I have a matrix like this one below



0    1
213 vha
342 gha
523 xha
121 gha
812 gha
612 vha
123 gha


and I want the program to calculate the steps of moving from, say, vha to gha in the second row. and from gha to xha in the third row. If any step is repeated, it will be added to the previous step. For example vha to gha in the first and second rows is repeated at the end of the matrix.



The desired output is will be the similar steps added together/ total number of rows-1. In the first case it is vha to gha prob = 2/7-1



Desired output



vha to gha prob = 0.3
gha to xha prob = 0.16
xha to gha prob = 0.16
gha to gha prob = 0.16
gha to vha prob = 0.16

Total probs = 1






python python-3.x probability






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 '18 at 20:58









Abdulaziz Al JumaiaAbdulaziz Al Jumaia

118113




118113








  • 1





    What datatype is this "matrix"?

    – timgeb
    Nov 11 '18 at 20:59











  • it is <class 'list'>

    – Abdulaziz Al Jumaia
    Nov 11 '18 at 21:04
















  • 1





    What datatype is this "matrix"?

    – timgeb
    Nov 11 '18 at 20:59











  • it is <class 'list'>

    – Abdulaziz Al Jumaia
    Nov 11 '18 at 21:04










1




1





What datatype is this "matrix"?

– timgeb
Nov 11 '18 at 20:59





What datatype is this "matrix"?

– timgeb
Nov 11 '18 at 20:59













it is <class 'list'>

– Abdulaziz Al Jumaia
Nov 11 '18 at 21:04







it is <class 'list'>

– Abdulaziz Al Jumaia
Nov 11 '18 at 21:04














1 Answer
1






active

oldest

votes


















2














You can use a Counter to count how many times a transition occurs and then calculate probabilities for each transition.



You can use zip to combine two slices of the list m - one with the last element removed and another with the first element removed - to get tuples for adjacent elements. zip(m[:-1], m[1:]) does that. Then can you can count similar tuples - which represent transitions - with a Counter:



from collections import Counter

m = [[213, 'vha'],
[342, 'gha'],
[523, 'xha'],
[121, 'gha'],
[812, 'gha'],
[612, 'vha'],
[123, 'gha']]

c = Counter([(x[1], y[1]) for x, y in zip(m[:-1], m[1:])])
probs = [(e, v / (len(m) - 1)) for e, v in c.items()]

for p in probs:
print(p)


Output



(('vha', 'gha'), 0.3333333333333333)                                                                                                                
(('gha', 'xha'), 0.16666666666666666)
(('xha', 'gha'), 0.16666666666666666)
(('gha', 'gha'), 0.16666666666666666)
(('gha', 'vha'), 0.16666666666666666)





share|improve this answer


























  • Amazing! the code is working. I have accepted your answer. I am just wondering if you can explain how the program works or refer me to a link with an explanation. Regards

    – Abdulaziz Al Jumaia
    Nov 12 '18 at 22:44











  • @AbdulazizAlJumaia I've added links to documentation an a more detailed explanation. Hope that helps!

    – slider
    Nov 12 '18 at 23:06











  • It does :). Thanks

    – Abdulaziz Al Jumaia
    Nov 13 '18 at 8:31











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253174%2fis-there-a-way-to-find-the-probability-of-different-values-in-matrices%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use a Counter to count how many times a transition occurs and then calculate probabilities for each transition.



You can use zip to combine two slices of the list m - one with the last element removed and another with the first element removed - to get tuples for adjacent elements. zip(m[:-1], m[1:]) does that. Then can you can count similar tuples - which represent transitions - with a Counter:



from collections import Counter

m = [[213, 'vha'],
[342, 'gha'],
[523, 'xha'],
[121, 'gha'],
[812, 'gha'],
[612, 'vha'],
[123, 'gha']]

c = Counter([(x[1], y[1]) for x, y in zip(m[:-1], m[1:])])
probs = [(e, v / (len(m) - 1)) for e, v in c.items()]

for p in probs:
print(p)


Output



(('vha', 'gha'), 0.3333333333333333)                                                                                                                
(('gha', 'xha'), 0.16666666666666666)
(('xha', 'gha'), 0.16666666666666666)
(('gha', 'gha'), 0.16666666666666666)
(('gha', 'vha'), 0.16666666666666666)





share|improve this answer


























  • Amazing! the code is working. I have accepted your answer. I am just wondering if you can explain how the program works or refer me to a link with an explanation. Regards

    – Abdulaziz Al Jumaia
    Nov 12 '18 at 22:44











  • @AbdulazizAlJumaia I've added links to documentation an a more detailed explanation. Hope that helps!

    – slider
    Nov 12 '18 at 23:06











  • It does :). Thanks

    – Abdulaziz Al Jumaia
    Nov 13 '18 at 8:31
















2














You can use a Counter to count how many times a transition occurs and then calculate probabilities for each transition.



You can use zip to combine two slices of the list m - one with the last element removed and another with the first element removed - to get tuples for adjacent elements. zip(m[:-1], m[1:]) does that. Then can you can count similar tuples - which represent transitions - with a Counter:



from collections import Counter

m = [[213, 'vha'],
[342, 'gha'],
[523, 'xha'],
[121, 'gha'],
[812, 'gha'],
[612, 'vha'],
[123, 'gha']]

c = Counter([(x[1], y[1]) for x, y in zip(m[:-1], m[1:])])
probs = [(e, v / (len(m) - 1)) for e, v in c.items()]

for p in probs:
print(p)


Output



(('vha', 'gha'), 0.3333333333333333)                                                                                                                
(('gha', 'xha'), 0.16666666666666666)
(('xha', 'gha'), 0.16666666666666666)
(('gha', 'gha'), 0.16666666666666666)
(('gha', 'vha'), 0.16666666666666666)





share|improve this answer


























  • Amazing! the code is working. I have accepted your answer. I am just wondering if you can explain how the program works or refer me to a link with an explanation. Regards

    – Abdulaziz Al Jumaia
    Nov 12 '18 at 22:44











  • @AbdulazizAlJumaia I've added links to documentation an a more detailed explanation. Hope that helps!

    – slider
    Nov 12 '18 at 23:06











  • It does :). Thanks

    – Abdulaziz Al Jumaia
    Nov 13 '18 at 8:31














2












2








2







You can use a Counter to count how many times a transition occurs and then calculate probabilities for each transition.



You can use zip to combine two slices of the list m - one with the last element removed and another with the first element removed - to get tuples for adjacent elements. zip(m[:-1], m[1:]) does that. Then can you can count similar tuples - which represent transitions - with a Counter:



from collections import Counter

m = [[213, 'vha'],
[342, 'gha'],
[523, 'xha'],
[121, 'gha'],
[812, 'gha'],
[612, 'vha'],
[123, 'gha']]

c = Counter([(x[1], y[1]) for x, y in zip(m[:-1], m[1:])])
probs = [(e, v / (len(m) - 1)) for e, v in c.items()]

for p in probs:
print(p)


Output



(('vha', 'gha'), 0.3333333333333333)                                                                                                                
(('gha', 'xha'), 0.16666666666666666)
(('xha', 'gha'), 0.16666666666666666)
(('gha', 'gha'), 0.16666666666666666)
(('gha', 'vha'), 0.16666666666666666)





share|improve this answer















You can use a Counter to count how many times a transition occurs and then calculate probabilities for each transition.



You can use zip to combine two slices of the list m - one with the last element removed and another with the first element removed - to get tuples for adjacent elements. zip(m[:-1], m[1:]) does that. Then can you can count similar tuples - which represent transitions - with a Counter:



from collections import Counter

m = [[213, 'vha'],
[342, 'gha'],
[523, 'xha'],
[121, 'gha'],
[812, 'gha'],
[612, 'vha'],
[123, 'gha']]

c = Counter([(x[1], y[1]) for x, y in zip(m[:-1], m[1:])])
probs = [(e, v / (len(m) - 1)) for e, v in c.items()]

for p in probs:
print(p)


Output



(('vha', 'gha'), 0.3333333333333333)                                                                                                                
(('gha', 'xha'), 0.16666666666666666)
(('xha', 'gha'), 0.16666666666666666)
(('gha', 'gha'), 0.16666666666666666)
(('gha', 'vha'), 0.16666666666666666)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 23:05

























answered Nov 11 '18 at 21:12









sliderslider

8,21811129




8,21811129













  • Amazing! the code is working. I have accepted your answer. I am just wondering if you can explain how the program works or refer me to a link with an explanation. Regards

    – Abdulaziz Al Jumaia
    Nov 12 '18 at 22:44











  • @AbdulazizAlJumaia I've added links to documentation an a more detailed explanation. Hope that helps!

    – slider
    Nov 12 '18 at 23:06











  • It does :). Thanks

    – Abdulaziz Al Jumaia
    Nov 13 '18 at 8:31



















  • Amazing! the code is working. I have accepted your answer. I am just wondering if you can explain how the program works or refer me to a link with an explanation. Regards

    – Abdulaziz Al Jumaia
    Nov 12 '18 at 22:44











  • @AbdulazizAlJumaia I've added links to documentation an a more detailed explanation. Hope that helps!

    – slider
    Nov 12 '18 at 23:06











  • It does :). Thanks

    – Abdulaziz Al Jumaia
    Nov 13 '18 at 8:31

















Amazing! the code is working. I have accepted your answer. I am just wondering if you can explain how the program works or refer me to a link with an explanation. Regards

– Abdulaziz Al Jumaia
Nov 12 '18 at 22:44





Amazing! the code is working. I have accepted your answer. I am just wondering if you can explain how the program works or refer me to a link with an explanation. Regards

– Abdulaziz Al Jumaia
Nov 12 '18 at 22:44













@AbdulazizAlJumaia I've added links to documentation an a more detailed explanation. Hope that helps!

– slider
Nov 12 '18 at 23:06





@AbdulazizAlJumaia I've added links to documentation an a more detailed explanation. Hope that helps!

– slider
Nov 12 '18 at 23:06













It does :). Thanks

– Abdulaziz Al Jumaia
Nov 13 '18 at 8:31





It does :). Thanks

– Abdulaziz Al Jumaia
Nov 13 '18 at 8:31


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253174%2fis-there-a-way-to-find-the-probability-of-different-values-in-matrices%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ