吴恩达机器学习编程作业python3版本
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-02-08 16:05 Coursera_python3
目录 0 2018-02-08 16:04 Coursera_python3Gauss Anomaly detection
目录 0 2018-02-08 16:04 Coursera_python3Gauss Anomaly detection.idea
文件 317 2018-02-07 14:14 Coursera_python3Gauss Anomaly detection.ideaGauss Anomaly detection.iml
文件 185 2018-02-07 14:12 Coursera_python3Gauss Anomaly detection.ideamisc.xml
文件 298 2018-02-07 14:12 Coursera_python3Gauss Anomaly detection.ideamodules.xml
文件 1048 2018-02-07 14:12 Coursera_python3Gauss Anomaly detection.ideaworkspace.xml
文件 5214 2018-02-07 15:46 Coursera_python3Gauss Anomaly detectionGauss Anomaly detection.py
文件 9501 2017-12-11 15:07 Coursera_python3Gauss Anomaly detectionex8data1.mat
文件 93481 2017-12-11 15:07 Coursera_python3Gauss Anomaly detectionex8data2.mat
目录 0 2018-02-08 16:04 Coursera_python3K-means聚类
目录 0 2018-02-08 16:04 Coursera_python3K-means聚类.idea
文件 317 2018-01-23 15:45 Coursera_python3K-means聚类.ideaK-means聚类.iml
文件 185 2018-01-23 15:41 Coursera_python3K-means聚类.ideamisc.xml
文件 278 2018-01-23 15:41 Coursera_python3K-means聚类.ideamodules.xml
文件 1048 2018-01-23 15:41 Coursera_python3K-means聚类.ideaworkspace.xml
文件 4485 2018-01-29 16:07 Coursera_python3K-means聚类Kmeans(also for 3d).py
文件 45606 2017-09-27 22:01 Coursera_python3K-means聚类ird_small.mat
文件 33031 2017-09-27 22:01 Coursera_python3K-means聚类ird_small.png
文件 995 2017-12-11 15:07 Coursera_python3K-means聚类ex7data1.mat
文件 4784 2017-12-11 15:07 Coursera_python3K-means聚类ex7data2.mat
文件 11027767 2017-12-11 15:07 Coursera_python3K-means聚类ex7faces.mat
文件 4071 2018-01-24 15:16 Coursera_python3K-means聚类kmeans(not suitbale for 3d).py
目录 0 2018-02-08 16:04 Coursera_python3PCA
目录 0 2018-02-08 16:04 Coursera_python3PCA.idea
文件 317 2018-01-30 16:35 Coursera_python3PCA.ideaPCA.iml
文件 258 2018-01-30 16:27 Coursera_python3PCA.ideamodules.xml
文件 1048 2018-01-30 16:27 Coursera_python3PCA.ideaworkspace.xml
文件 2444 2018-01-30 18:32 Coursera_python3PCAPCA.py
文件 995 2017-12-11 15:07 Coursera_python3PCAex7data1.mat
文件 11027767 2017-12-11 15:07 Coursera_python3PCAex7faces.mat
............此处省略2356个文件信息
#本文是高斯分布用于异常检测
#load data set
import scipy.io as sio
import numpy as np
import matplotlib.pyplot as plt
data = sio.loadmat(‘ex8data1.mat‘)
X = data[‘X‘]
Xval = data[‘Xval‘]
yval = data[‘yval‘]
print (X.shapeXval.shapeyval.shape)
print (X[:5])
print (yval[:5])
plt.plot(X[:0]X[:1]‘bx‘) # 沿坐标轴划线函数plot x1 and x2 using blue x markers
plt.xlim(0 30)
plt.ylim(0 30)
plt.title(‘Visualize the ex8data1‘)
plt.xlabel(‘Latency (ms)‘)
plt.ylabel(‘Throughput (mb/s)‘)
plt.show()
# 高斯分布
from scipy import stats # 内置有计算数据点属于正态分布的概率的方法
# 求得均值和方差
def estimateGaussian(x):
# train set x fit the musigma2
m n = x.shape
mu = np.mean(x axis=0).reshape(1 -1) # 仅变成1行,不管多少列
# sigma2 = np.sum(np.square(x - mu)axis=0).reshape(1-1) / m
sigma2 = np.var(x axis=0).reshape(1 -1) # 求方差,也可直接求标准差sigma
return mu sigma2
# 计算高斯概率
def p(x mu sigma2):
# x is a new example:[m*n]
m n = x.shape
p_list = []
for j in range(m):
p = 1
for i in range(n):
p *= stats.norm.pdf(x[j i] mu[0 i] np.sqrt(sigma2[0 i]))
# stats.norm.pdf(xmeansigma)
p_list.append(p)
p_array = np.array(p_list).reshape(-1 1) # 仅为1列
return p_array
mu sigma2 = estimateGaussian(X)
print(‘mu.shape:‘mu.shape ‘sigma2.shape:‘ sigma2.shape)
print(‘mu:‘mu ‘sigma2:‘ sigma2)
p_train = p(X mu sigma2) # 调用p(x mu sigma2)函数,也仅为1列
print(‘p_train[:5]:‘ p_train[:5])
p_val = p(Xval mu sigma2)
print(‘p_val[:5]:‘ p_val[:5])
# cross validation for select threshold
# 交叉验证用于阈值选定这里用的是F1 score这个评估指标
def selectThreshold(y pval):
bestEpsilon = 0
bestF1 = 0
stepSize = (np.max(pval) - np.min(pval)) / 1000 # 为何如此选取?
for epsilon in np.arange(np.min(pval) np.max(pval) stepSize):
predictions = (pval < epsilon) # 判断真假
# fp= np.sum((predictions == 1) & (y == 0))
fp = np.sum((predictions == 1) & (y == 0))
fn = np.sum((predictions == 0) & (y == 1))
tp = np.sum((predictions == 1) & (y == 1))
if tp + fp == 0:
precision = 0
else:
precision = float(tp) / (tp + fp) # note!!!!float!!!
if tp + fn == 0:
recall = 0
else:
recall = float(tp) / (tp + fn)
if precision + recall == 0:
F1 = 0
else:
F1 = 2.0 * precision * recall / (precision + recall)
if F1 > bestF1:
bestF1 = F1
bestEpsilon = epsilon
return bestEpsilon bestF1
#test the cs
epsilonF1 = selectThreshold(yvalp_val)
print(“Best epsilon found using cross-validation: %e“%(epsilon))
print(“Best F1 on Cross Validation Set: %f“%(F1))
# 可视化一下检测是异常值
print(“Outliers found: %d “ % (np.sum(p_train < epsilon)))
# visualization:Draw a red circle around those outliers
outliters = np.where(p_train.ravel() < epsilon)
plt.plot(X[: 0] X[: 1] ‘bx‘)
plt.plot(X[outliters
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-02-08 16:05 Coursera_python3
目录 0 2018-02-08 16:04 Coursera_python3Gauss Anomaly detection
目录 0 2018-02-08 16:04 Coursera_python3Gauss Anomaly detection.idea
文件 317 2018-02-07 14:14 Coursera_python3Gauss Anomaly detection.ideaGauss Anomaly detection.iml
文件 185 2018-02-07 14:12 Coursera_python3Gauss Anomaly detection.ideamisc.xm
文件 298 2018-02-07 14:12 Coursera_python3Gauss Anomaly detection.ideamodules.xm
文件 1048 2018-02-07 14:12 Coursera_python3Gauss Anomaly detection.ideaworkspace.xm
文件 5214 2018-02-07 15:46 Coursera_python3Gauss Anomaly detectionGauss Anomaly detection.py
文件 9501 2017-12-11 15:07 Coursera_python3Gauss Anomaly detectionex8data1.mat
文件 93481 2017-12-11 15:07 Coursera_python3Gauss Anomaly detectionex8data2.mat
目录 0 2018-02-08 16:04 Coursera_python3K-means聚类
目录 0 2018-02-08 16:04 Coursera_python3K-means聚类.idea
文件 317 2018-01-23 15:45 Coursera_python3K-means聚类.ideaK-means聚类.iml
文件 185 2018-01-23 15:41 Coursera_python3K-means聚类.ideamisc.xm
文件 278 2018-01-23 15:41 Coursera_python3K-means聚类.ideamodules.xm
文件 1048 2018-01-23 15:41 Coursera_python3K-means聚类.ideaworkspace.xm
文件 4485 2018-01-29 16:07 Coursera_python3K-means聚类Kmeans(also for 3d).py
文件 45606 2017-09-27 22:01 Coursera_python3K-means聚类ird_small.mat
文件 33031 2017-09-27 22:01 Coursera_python3K-means聚类ird_small.png
文件 995 2017-12-11 15:07 Coursera_python3K-means聚类ex7data1.mat
文件 4784 2017-12-11 15:07 Coursera_python3K-means聚类ex7data2.mat
文件 11027767 2017-12-11 15:07 Coursera_python3K-means聚类ex7faces.mat
文件 4071 2018-01-24 15:16 Coursera_python3K-means聚类kmeans(not suitbale for 3d).py
目录 0 2018-02-08 16:04 Coursera_python3PCA
目录 0 2018-02-08 16:04 Coursera_python3PCA.idea
文件 317 2018-01-30 16:35 Coursera_python3PCA.ideaPCA.iml
文件 258 2018-01-30 16:27 Coursera_python3PCA.ideamodules.xm
文件 1048 2018-01-30 16:27 Coursera_python3PCA.ideaworkspace.xm
文件 2444 2018-01-30 18:32 Coursera_python3PCAPCA.py
文件 995 2017-12-11 15:07 Coursera_python3PCAex7data1.mat
文件 11027767 2017-12-11 15:07 Coursera_python3PCAex7faces.mat
............此处省略2356个文件信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)