使用搜索功能并没有帮助我找到问题的解决方案,这就是我创建此帖子的原因。
首先,我是Python的新手,因此我的知识有限。
我正在分析一个基于时间使用日志的数据集。因此,列表示时间步长(tb1_1 - tb1_144)
,而行则表示填写日志的相应个人(超过60k的观察值)。此外,每个单元格中的值表示每个时间步执行的活动。 (例如1 =睡眠等。)数据集如下所示:
为了进行分析,我想通过使用时间不均一的马尔可夫链,基于日记条目提供的信息来创建活动资料,该图形将看起来像这样:
我已经看过许多有关如何在Python中计算马尔可夫链的示例。但是,这些示例都没有考虑到,对于每个时间步长t,都需要从初始数据集中导出新的转换矩阵。
如果有人可以帮助我,我将非常高兴。
谢谢, 费利克斯
-----更新---------
经过几次尝试,我终于找到了如何构建动态矩阵的方法。请在下面找到代码。如果有人知道如何进一步缩短时间,请随时发表评论:)
# generate transition probability matrices
matrix_drei_personen_haus_winter_weekday_ft_master= np.empty(shape=(144, 14, 14))
def a1(x,y):
try:
return x/y
except ZeroDivisionError:
return 0
for i in range (0,144):
for j in range (1,15):
for m in range (1,15):
a=a1(len(drei_personen_haus_winter_weekday_ft_master[(drei_personen_haus_winter_weekday_ft_master.iloc[:,i]==j)&(drei_personen_haus_winter_weekday_ft_master.iloc[:,i+1]==m)]),len(drei_personen_haus_winter_weekday_ft_master[drei_personen_haus_winter_weekday_ft_master.iloc[:,i]==j]))
matrix_drei_personen_haus_winter_weekday_ft_master[i,j-1,m-1]=a
#create discrete heterogeneous Markov-chain
def activity_forecast(steps):
# Choose the starting state
activityToday = "4"
print("Start state: " + activityToday)
# Shall store the sequence of states taken. So, this only has the starting state for now.
activityList = [activityToday]
i = 0
while i != steps:
#create sequence of activities
if activityToday == "1":
change = np.random.choice(transitionName[0],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,0,:])
if change == "11":
activityList.append("1")
pass
elif change == "12":
activityToday = "2"
activityList.append("2")
elif change == "13":
activityToday = "3"
activityList.append("3")
elif change == "14":
activityToday = "4"
activityList.append("4")
elif change == "15":
activityToday = "5"
activityList.append("5")
elif change == "16":
activityToday = "6"
activityList.append("6")
elif change == "17":
activityToday = "7"
activityList.append("7")
elif change == "18":
activityToday = "8"
activityList.append("8")
elif change == "19":
activityToday = "9"
activityList.append("9")
elif change == "0110":
activityToday = "10"
activityList.append("10")
elif change == "0111":
activityToday = "11"
activityList.append("11")
elif change == "0112":
activityToday = "12"
activityList.append("12")
elif change == "0113":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "2":
change = np.random.choice(transitionName[1],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,1,:])
if change == "22":
activityList.append("2")
pass
elif change == "21":
activityToday = "1"
activityList.append("1")
elif change == "23":
activityToday = "3"
activityList.append("3")
elif change == "24":
activityToday = "4"
activityList.append("4")
elif change == "25":
activityToday = "5"
activityList.append("5")
elif change == "26":
activityToday = "6"
activityList.append("6")
elif change == "27":
activityToday = "7"
activityList.append("7")
elif change == "28":
activityToday = "8"
activityList.append("8")
elif change == "29":
activityToday = "9"
activityList.append("9")
elif change == "210":
activityToday = "10"
activityList.append("10")
elif change == "211":
activityToday = "11"
activityList.append("11")
elif change == "112":
activityToday = "12"
activityList.append("12")
elif change == "213":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "3":
change = np.random.choice(transitionName[2],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,2,:])
if change == "33":
activityList.append("3")
pass
elif change == "31":
activityToday = "1"
activityList.append("1")
elif change == "32":
activityToday = "2"
activityList.append("2")
elif change == "34":
activityToday = "4"
activityList.append("4")
elif change == "35":
activityToday = "5"
activityList.append("5")
elif change == "36":
activityToday = "6"
activityList.append("6")
elif change == "37":
activityToday = "7"
activityList.append("7")
elif change == "38":
activityToday = "8"
activityList.append("8")
elif change == "39":
activityToday = "9"
activityList.append("9")
elif change == "310":
activityToday = "10"
activityList.append("10")
elif change == "311":
activityToday = "11"
activityList.append("11")
elif change == "312":
activityToday = "12"
activityList.append("12")
elif change == "313":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "4":
change = np.random.choice(transitionName[3],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,3,:])
if change == "44":
activityList.append("4")
pass
elif change == "42":
activityToday = "2"
activityList.append("2")
elif change == "43":
activityToday = "3"
activityList.append("3")
elif change == "41":
activityToday = "1"
activityList.append("1")
elif change == "45":
activityToday = "5"
activityList.append("5")
elif change == "46":
activityToday = "6"
activityList.append("6")
elif change == "47":
activityToday = "7"
activityList.append("7")
elif change == "48":
activityToday = "8"
activityList.append("8")
elif change == "49":
activityToday = "9"
activityList.append("9")
elif change == "310":
activityToday = "10"
activityList.append("10")
elif change == "411":
activityToday = "11"
activityList.append("11")
elif change == "412":
activityToday = "12"
activityList.append("12")
elif change == "413":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "5":
change = np.random.choice(transitionName[4],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,4,:])
if change == "55":
activityList.append("5")
pass
elif change == "52":
activityToday = "2"
activityList.append("2")
elif change == "53":
activityToday = "3"
activityList.append("3")
elif change == "54":
activityToday = "4"
activityList.append("4")
elif change == "51":
activityToday = "1"
activityList.append("1")
elif change == "56":
activityToday = "6"
activityList.append("6")
elif change == "57":
activityToday = "7"
activityList.append("7")
elif change == "58":
activityToday = "8"
activityList.append("8")
elif change == "59":
activityToday = "9"
activityList.append("9")
elif change == "510":
activityToday = "10"
activityList.append("10")
elif change == "511":
activityToday = "11"
activityList.append("11")
elif change == "512":
activityToday = "12"
activityList.append("12")
elif change == "513":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "6":
change = np.random.choice(transitionName[5],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,5,:])
if change == "66":
activityList.append("6")
pass
elif change == "62":
activityToday = "2"
activityList.append("2")
elif change == "63":
activityToday = "3"
activityList.append("3")
elif change == "64":
activityToday = "4"
activityList.append("4")
elif change == "65":
activityToday = "5"
activityList.append("5")
elif change == "61":
activityToday = "1"
activityList.append("1")
elif change == "67":
activityToday = "7"
activityList.append("7")
elif change == "68":
activityToday = "8"
activityList.append("8")
elif change == "69":
activityToday = "9"
activityList.append("9")
elif change == "610":
activityToday = "10"
activityList.append("10")
elif change == "611":
activityToday = "11"
activityList.append("11")
elif change == "612":
activityToday = "12"
activityList.append("12")
elif change == "613":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "7":
change = np.random.choice(transitionName[6],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,6,:])
if change == "77":
activityList.append("7")
pass
elif change == "72":
activityToday = "2"
activityList.append("2")
elif change == "73":
activityToday = "3"
activityList.append("3")
elif change == "74":
activityToday = "4"
activityList.append("4")
elif change == "75":
activityToday = "5"
activityList.append("5")
elif change == "76":
activityToday = "6"
activityList.append("6")
elif change == "71":
activityToday = "1"
activityList.append("1")
elif change == "78":
activityToday = "8"
activityList.append("8")
elif change == "79":
activityToday = "9"
activityList.append("9")
elif change == "710":
activityToday = "10"
activityList.append("10")
elif change == "711":
activityToday = "11"
activityList.append("11")
elif change == "712":
activityToday = "12"
activityList.append("12")
elif change == "713":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "8":
change = np.random.choice(transitionName[7],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,7,:])
if change == "88":
activityList.append("8")
pass
elif change == "82":
activityToday = "2"
activityList.append("2")
elif change == "83":
activityToday = "3"
activityList.append("3")
elif change == "84":
activityToday = "4"
activityList.append("4")
elif change == "85":
activityToday = "5"
activityList.append("5")
elif change == "86":
activityToday = "6"
activityList.append("6")
elif change == "87":
activityToday = "7"
activityList.append("7")
elif change == "81":
activityToday = "1"
activityList.append("1")
elif change == "89":
activityToday = "9"
activityList.append("9")
elif change == "810":
activityToday = "10"
activityList.append("10")
elif change == "811":
activityToday = "11"
activityList.append("11")
elif change == "812":
activityToday = "12"
activityList.append("12")
elif change == "813":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "9":
change = np.random.choice(transitionName[8],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,8,:])
if change == "99":
activityList.append("9")
pass
elif change == "92":
activityToday = "2"
activityList.append("2")
elif change == "93":
activityToday = "3"
activityList.append("3")
elif change == "94":
activityToday = "4"
activityList.append("4")
elif change == "95":
activityToday = "5"
activityList.append("5")
elif change == "96":
activityToday = "6"
activityList.append("6")
elif change == "97":
activityToday = "7"
activityList.append("7")
elif change == "98":
activityToday = "8"
activityList.append("8")
elif change == "91":
activityToday = "1"
activityList.append("1")
elif change == "910":
activityToday = "10"
activityList.append("10")
elif change == "911":
activityToday = "11"
activityList.append("11")
elif change == "912":
activityToday = "12"
activityList.append("12")
elif change == "913":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "10":
change = np.random.choice(transitionName[9],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,9,:])
if change == "1010":
activityList.append("10")
pass
elif change == "102":
activityToday = "2"
activityList.append("2")
elif change == "103":
activityToday = "3"
activityList.append("3")
elif change == "104":
activityToday = "4"
activityList.append("4")
elif change == "105":
activityToday = "5"
activityList.append("5")
elif change == "106":
activityToday = "6"
activityList.append("6")
elif change == "107":
activityToday = "7"
activityList.append("7")
elif change == "108":
activityToday = "8"
activityList.append("8")
elif change == "109":
activityToday = "9"
activityList.append("9")
elif change == "101":
activityToday = "1"
activityList.append("1")
elif change == "1011":
activityToday = "11"
activityList.append("11")
elif change == "1012":
activityToday = "12"
activityList.append("12")
elif change == "1013":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "11":
change = np.random.choice(transitionName[10],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,10,:])
if change == "1111":
activityList.append("11")
pass
elif change == "112":
activityToday = "2"
activityList.append("2")
elif change == "113":
activityToday = "3"
activityList.append("3")
elif change == "114":
activityToday = "4"
activityList.append("4")
elif change == "115":
activityToday = "5"
activityList.append("5")
elif change == "116":
activityToday = "6"
activityList.append("6")
elif change == "117":
activityToday = "7"
activityList.append("7")
elif change == "118":
activityToday = "8"
activityList.append("8")
elif change == "119":
activityToday = "9"
activityList.append("9")
elif change == "1110":
activityToday = "10"
activityList.append("10")
elif change == "111":
activityToday = "1"
activityList.append("1")
elif change == "1112":
activityToday = "12"
activityList.append("12")
elif change == "1113":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "12":
change = np.random.choice(transitionName[11],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,11,:])
if change == "1212":
activityList.append("12")
pass
elif change == "122":
activityToday = "2"
activityList.append("2")
elif change == "123":
activityToday = "3"
activityList.append("3")
elif change == "124":
activityToday = "4"
activityList.append("4")
elif change == "125":
activityToday = "5"
activityList.append("5")
elif change == "126":
activityToday = "6"
activityList.append("6")
elif change == "127":
activityToday = "7"
activityList.append("7")
elif change == "128":
activityToday = "8"
activityList.append("8")
elif change == "129":
activityToday = "9"
activityList.append("9")
elif change == "1210":
activityToday = "10"
activityList.append("10")
elif change == "1211":
activityToday = "11"
activityList.append("11")
elif change == "121":
activityToday = "1"
activityList.append("1")
elif change == "1213":
activityToday = "13"
activityList.append("13")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "13":
change = np.random.choice(transitionName[12],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,12,:])
if change == "1313":
activityList.append("13")
pass
elif change == "132":
activityToday = "2"
activityList.append("2")
elif change == "133":
activityToday = "3"
activityList.append("3")
elif change == "134":
activityToday = "4"
activityList.append("4")
elif change == "135":
activityToday = "5"
activityList.append("5")
elif change == "136":
activityToday = "6"
activityList.append("6")
elif change == "137":
activityToday = "7"
activityList.append("7")
elif change == "138":
activityToday = "8"
activityList.append("8")
elif change == "139":
activityToday = "9"
activityList.append("9")
elif change == "1310":
activityToday = "10"
activityList.append("10")
elif change == "1311":
activityToday = "11"
activityList.append("11")
elif change == "1312":
activityToday = "12"
activityList.append("12")
elif change == "131":
activityToday = "1"
activityList.append("1")
else:
activityToday = "14"
activityList.append("14")
elif activityToday == "14":
change = np.random.choice(transitionName[13],replace=True,p=matrix_drei_personen_haus_winter_weekday_ft_master[i,13,:])
if change == "1414":
activityList.append("14")
pass
elif change == "142":
activityToday = "2"
activityList.append("2")
elif change == "143":
activityToday = "3"
activityList.append("3")
elif change == "144":
activityToday = "4"
activityList.append("4")
elif change == "145":
activityToday = "5"
activityList.append("5")
elif change == "146":
activityToday = "6"
activityList.append("6")
elif change == "147":
activityToday = "7"
activityList.append("7")
elif change == "148":
activityToday = "8"
activityList.append("8")
elif change == "149":
activityToday = "9"
activityList.append("9")
elif change == "1410":
activityToday = "10"
activityList.append("10")
elif change == "1411":
activityToday = "11"
activityList.append("11")
elif change == "1412":
activityToday = "12"
activityList.append("12")
elif change == "1413":
activityToday = "13"
activityList.append("13")
else:
activityToday = "1"
activityList.append("1")
i += 1
print("Possible states: " + str(activityList))
print("End state after "+ str(steps) + " steps: " + activityToday)
# Function that forecasts the possible state for the next 144 steps
activity_forecast(143)