import numpy as np
import random
def select_threshold_test(target):
p_val = np.array([i / 100 for i in range(30)])
y_val = np.array([1] * 5 + [0] * 25)
best_epsilon, best_F1 = target(y_val, p_val)
assert np.isclose(best_epsilon, 0.04, atol=0.3 / 1000), f"Wrong best_epsilon. Expected: {0.04} got: {best_epsilon}"
assert best_F1 == 1, f"Wrong best_F1. Expected: 1 got: {best_F1}"
y_val = np.array([1] * 5 + [0] * 25)
y_val[2] = 0 # Introduce noise
best_epsilon, best_F1 = target(y_val, p_val)
assert np.isclose(best_epsilon, 0.04, atol=0.3 / 1000), f"Wrong best_epsilon. Expected: {0.04} got: {best_epsilon}"
assert np.isclose(best_F1, 0.8888888), f"Wrong best_F1. Expected: 0.8888888 got: {best_F1}"
p_val = np.array([i / 1000 for i in range(50)])
y_val = np.array([1] * 8 + [0] * 42)
y_val[5] = 0
index = [*range(50)]
random.shuffle(index)
p_val = p_val[index]
y_val = y_val[index]
best_epsilon, best_F1 = target(y_val, p_val)
assert np.isclose(best_epsilon, 0.007, atol=0.05 / 1000), f"Wrong best_epsilon. Expected: {0.0070070} got: {best_epsilon}"
assert np.isclose(best_F1, 0.933333333), f"Wrong best_F1. Expected: 0.933333333 got: {best_F1}"
print("\033[92mAll tests passed!")
def estimate_gaussian_test(target):
np.random.seed(273)
X = np.array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]]).T
mu, var = target(X)
assert type(mu) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(mu)}"
assert type(var) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(var)}"
assert mu.shape == (X.shape[1],), f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {mu.shape}"
assert type(var) == np.ndarray, f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {var.shape}"
assert np.allclose(mu, [1., 2., 3.]), f"Wrong value for mu. Expected: {[1, 2, 3]} got: {mu}"
assert np.allclose(var, [0., 0., 0.]), f"Wrong value for mu. Expected: {[0, 0, 0]} got: {var}"
X = np.array([[1, 2, 3],
[2, 4, 6],
[3, 6, 9]]).T
mu, var = target(X)
assert type(mu) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(mu)}"
assert type(var) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(var)}"
assert mu.shape == (X.shape[1],), f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {mu.shape}"
assert type(var) == np.ndarray, f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {var.shape}"
assert np.allclose(mu, [2., 4., 6.]), f"Wrong value for mu. Expected: {[2., 4., 6.]} got: {mu}"
assert np.allclose(var, [2. / 3, 8. / 3., 18. / 3.]), f"Wrong value for mu. Expected: {[2. / 3, 8. / 3., 18. / 3.]} got: {var}"
m = 500
X = np.array([np.random.normal(0, 1, m),
np.random.normal(1, 2, m),
np.random.normal(3, 1.5, m)]).T
mu, var = target(X)
assert type(mu) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(mu)}"
assert type(var) == np.ndarray, f"Wrong type for mu. Expected: {np.ndarray} got: {type(var)}"
assert mu.shape == (X.shape[1],), f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {mu.shape}"
assert type(var) == np.ndarray, f"Wrong shape for mu. Expected: {(X.shape[1],)} got: {var.shape}"
assert np.allclose(mu, [0., 1., 3.], atol=0.2), f"Wrong value for mu. Expected: {[0, 1, 3]} got: {mu}"
assert np.allclose(var, np.square([1., 2., 1.5]), atol=0.2), f"Wrong value for mu. Expected: {np.square([1., 2., 1.5])} got: {var}"
print("\033[92mAll tests passed!")
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
week1.zip (36个子文件)
week1
2 Practice Lab1
images
figure 1.png 252KB
figure 2.png 1.06MB
figure 3.png 796KB
bird_small.png 32KB
utils.py 886B
data
ex7_X.npy 5KB
.ipynb_checkpoints
C3_W1_KMeans_Assignment-checkpoint.ipynb 29KB
public_tests.py 3KB
bird_small.png 32KB
C3_W1_KMeans_Assignment.ipynb 29KB
3 Practice Quiz Anomaly detection
5.png 104KB
1.png 18KB
2.png 20KB
4.png 30KB
quiz.md 54B
3.png 36KB
4 Practice Lab2
images
figure1.png 141KB
figure3.png 278KB
figure2.png 312KB
utils.py 2KB
data
y_val_part2.npy 228B
X_val_part1.npy 5KB
y_val_part1.npy 435B
X_part1.npy 5KB
X_part2.npy 86KB
X_val_part2.npy 9KB
C3_W1_Anomaly_Detection.ipynb 28KB
public_tests.py 4KB
1 Practice Quiz Clustering
5.png 24KB
1.png 28KB
2.png 42KB
4.png 28KB
quiz.md 54B
3.png 26KB
C3_W1_Anomaly_Detection.ipynb 144KB
C3_W1_KMeans_Assignment.ipynb 354KB
共 36 条
- 1
资源评论
编程搬砖我最强
- 粉丝: 9
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 开关电源工程师-应具备的理论知识,实践技能和工程素质
- 技术资料分享CC2530中文数据手册完全版非常好的技术资料.zip
- 技术资料分享CC2530非常好的技术资料.zip
- 技术资料分享AU9254A21非常好的技术资料.zip
- 技术资料分享AT070TN92非常好的技术资料.zip
- 技术资料分享ADV7123非常好的技术资料.zip
- TestBank.java
- js-leetcode题解之146-lru-cache.js
- js-leetcode题解之145-binary-tree-postorder-traversal.js
- js-leetcode题解之144-binary-tree-preorder-traversal.js
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功