forked from microsoft/ai-edu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update * up * duao * update
- Loading branch information
Showing
16 changed files
with
644 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import numpy as np | ||
import os | ||
import json | ||
|
||
# 作为目标的转移概率矩阵 | ||
P = np.array([ | ||
[0.1, 0.3, 0.0, 0.6], | ||
[0.8, 0.0, 0.2, 0.0], | ||
[0.0, 0.9, 0.1, 0.0], | ||
[0.0, 0.3, 0.3, 0.4] | ||
]) | ||
|
||
# 采样 | ||
def sample(n_samples, n_states, start_state): | ||
states = [i for i in range(n_states)] | ||
# 状态转移序列 | ||
X = [] | ||
# 开始采样 | ||
X.append(start_state) | ||
current = start_state | ||
for i in range(n_samples): | ||
next = np.random.choice(states, p=P[current]) | ||
X.append(next) | ||
current = next | ||
#endfor | ||
return X | ||
|
||
def save_file(X, file_name): | ||
# 把0123变成ABCD | ||
Y = [chr(x+65) for x in X] | ||
#print(Y) | ||
# 保存Y到文件 | ||
json_list = json.dumps(Y) | ||
file = open(file_name, "w") | ||
file.write(json_list) | ||
file.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
# 采样数量 | ||
n_samples = 10000 | ||
# 状态空间 | ||
n_states = 4 | ||
# 起始状态(从0开始) | ||
start_state = 1 | ||
X = sample(n_samples, n_states, start_state) | ||
#print(X) | ||
# 保存文件 | ||
root = os.path.split(os.path.realpath(__file__))[0] | ||
file_name = os.path.join(root, "CarData.txt") | ||
save_file(X, file_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import numpy as np | ||
import json | ||
import os | ||
|
||
# 计算从a_i转移到a_j的概率 | ||
def calculate_P(X, a_i, a_j): | ||
n_i = 0 | ||
n_j = 0 | ||
for x in range(len(X)-1): | ||
if a_i == X[x]: | ||
n_i += 1 | ||
if a_j == X[x+1]: | ||
n_j += 1 | ||
print(n_i, n_j, n_j/n_i) | ||
|
||
def open_file(file_name): | ||
file = open(file_name, "r") | ||
lines = file.read() | ||
file.close() | ||
data_list = json.loads(lines) | ||
return data_list | ||
|
||
if __name__ == "__main__": | ||
# 状态空间 | ||
n_states = 4 | ||
# 读取文件 | ||
root = os.path.split(os.path.realpath(__file__))[0] | ||
file_name = os.path.join(root, "CarData.txt") | ||
data_list = open_file(file_name) | ||
# 把 ABCD 变成 0123 | ||
X = [ord(x)-65 for x in data_list] | ||
# 计算转移矩阵 | ||
calculate_P(X, 0, 1) # 1代表B店,0代表A店 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import numpy as np | ||
import json | ||
import os | ||
|
||
def calculate_Matrix(n_states, X): | ||
P_counter = np.zeros((n_states, n_states)) | ||
for i in range(len(X)-1): | ||
a_i = X[i] | ||
a_j = X[i+1] | ||
P_counter[a_i, a_j] += 1 | ||
#endfor | ||
# 计算各列之和 | ||
sum = np.sum(P_counter, axis=1, keepdims=True) | ||
print("各个状态出现的次数:\n",sum) | ||
P = P_counter / sum | ||
return P | ||
|
||
def open_file(file_name): | ||
file = open(file_name, "r") | ||
lines = file.read() | ||
file.close() | ||
data_list = json.loads(lines) | ||
return data_list | ||
|
||
if __name__ == "__main__": | ||
# 状态空间 | ||
n_states = 4 | ||
# 读取文件 | ||
root = os.path.split(os.path.realpath(__file__))[0] | ||
file_name = os.path.join(root, "CarData.txt") | ||
data_list = open_file(file_name) | ||
# 把 ABCD 变成 0123 | ||
X = [ord(x)-65 for x in data_list] | ||
# 计算转移矩阵 | ||
P = calculate_Matrix(n_states, X) | ||
print("概率转移矩阵:") | ||
print(np.around(P, 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import numpy as np | ||
|
||
P = np.array([ | ||
[0.1, 0.3, 0.0, 0.6], | ||
[0.8, 0.0, 0.2, 0.0], | ||
[0.0, 0.9, 0.1, 0.0], | ||
[0.0, 0.3, 0.3, 0.4] | ||
]) | ||
|
||
def calculate_day(X, P, day): | ||
X_curr = X.copy() | ||
for i in range(day): | ||
print(str.format("day {0}: {1} ", i, X_curr)) | ||
X_next = np.dot(X_curr, P) | ||
X_curr = X_next.copy() | ||
|
||
if __name__=="__main__": | ||
X = np.array([0,1,0,0]) | ||
calculate_day(X, P, 6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import numpy as np | ||
|
||
P = np.array([ | ||
[0.1, 0.3, 0.0, 0.6], | ||
[0.8, 0.0, 0.2, 0.0], | ||
[0.0, 0.9, 0.1, 0.0], | ||
[0.0, 0.3, 0.3, 0.4] | ||
]) | ||
|
||
# 计算K步转移概率矩阵 | ||
def K_step_matrix(P, K): | ||
Pk=P.copy() | ||
for i in range(K-1): | ||
Pk=np.dot(P,Pk) | ||
#print(Pk) | ||
return Pk | ||
|
||
if __name__=="__main__": | ||
X = np.array([0,1,0,0]) | ||
P5 = K_step_matrix(P, 5) | ||
print("5步转移矩阵:\n", P5) | ||
X5 = np.dot(X, P5) | ||
print("第 5 天的情况:", X5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import numpy as np | ||
|
||
P = np.array([ | ||
[0.1, 0.3, 0.0, 0.6], | ||
[0.8, 0.0, 0.2, 0.0], | ||
[0.0, 0.9, 0.1, 0.0], | ||
[0.0, 0.3, 0.3, 0.4] | ||
]) | ||
|
||
def Check_Convergence(P): | ||
P_curr = P.copy() | ||
for i in range(100000): | ||
P_next=np.dot(P,P_curr) | ||
print("迭代次数 =",i+1) | ||
print(P_next) | ||
if np.allclose(P_curr, P_next): | ||
break | ||
P_curr = P_next | ||
return P_next | ||
|
||
if __name__=="__main__": | ||
Pn = Check_Convergence(P) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.