1、实现二维正态分布(高斯分布)
- 给定参数生成二维正态分布,并求给定值下的概率密度
def getGauss(X,mean,cov):
covv = np.array(cov)
c1 = math.pow(np.linalg.det(covv),0.5)#求行列式
c2 = np.linalg.inv(covv)#求逆
XX = np.array(X)
meann = np.array(mean)
de = np.array(XX-meann)
de1 = de.T
p1 = np.dot(de1,c2)
p2 = (-1)* np.dot(p1,de)/2
p = math.pow(math.e,p2)/(2*math.pi*c1)
return p
- 生成符合二维正态分布的随机数
import numpy as np
mean1 = [0, 0]
cov1 = [[1, 1], [1, 6]]
data = np.random.multivariate_normal(mean1, cov1, 100)
- 协方差矩阵
#得到协方差矩阵S
def getS(mean,data):
sum = 0
for i in range(len(data)):
xx = np.array(data[i]) - np.array(mean)
xxT = reshapelist(np.array(xx))
sum = sum + np.dot(xxT,np.array([xx]))
return (sum/len(data))
2、矩阵简单运算
import numpy as np
lis = np.mat([[1,2,3],[3,4,5],[4,5,6]])
print(np.linalg.inv(lis)) # 求矩阵的逆矩阵
print(lis.transpose()) # 求矩阵的转置矩阵
print(np.linalg.det(lis)) # 求矩阵的行列式
print(np.linalg.eig(lis)) # 求矩阵的特征值与特征向量,求得的元组中第一个为特征值元组,第二个为相对应的特征向量
eigenvalues, feature_vectors = np.linalg.eig(cov) # 特征值分解.得到特征值和特征向量
shape:
>>> c = array([[1,1],[1,2],[1,3],[1,4]])
>>> c.shape
(4, 2)
>>> c.shape[0]
4
>>> c.shape[1]
2
3、随机数
import random
# 产生 1 到 10 的一个整数型随机数
print( random.randint(1,10) )
# 产生 0 到 1 之间的随机浮点数
print( random.random() )
# 产生 1.1 到 5.4 之间的随机浮点数,区间可以不是整数
print( random.uniform(1.1,5.4) )
# 从序列中随机选取一个元素
print( random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) )
# 生成从1到100的间隔为2的随机整数
print( random.randrange(1,100,2) )
# 将序列a中的元素顺序打乱
a=[1,3,5,6,7]
random.shuffle([1,3,5,6,7])
print(a)
3、排序
x = np.array([1,4,3,-1,6,9])
x.argsort()
# array([3, 0, 1, 2, 4, 5], dtype=int64)
#取数组最小值
x[x.argsort()[0]]
#最大值
x[x.argsort()[-1]]
4、 画图
import matplotlib.pyplot as plt
colors1 = '#00CED1' # 点的颜色
colors2 = '#DC143C'
colors3 = '#808080'
colors4 = '#000080'
for i in range(len(x)):
if(abs(C[i]-0)<1e-10):
plt.scatter(x[i], y[i],c=colors1)
elif(abs(C[i]-1)<1e-10):
plt.scatter(x[i], y[i], c=colors2)
elif (abs(C[i]-2)<1e-10):
plt.scatter(x[i], y[i], c=colors3)
elif(abs(C[i]-3)<1e-10):
plt.scatter(x[i], y[i], c=colors1)
for i in range(len(mean)):
plt.scatter(mean[i][0],mean[i][1],c=colors4)
#plt.plot(xf, yf,label="M="+str(m-1)) #线图
plt.figure(figsize=(6, 4))
plt.legend() # 显示图例
plt.show() # 显示画图
5、数据集来源
- UCI数据集:http://archive.ics.uci.edu/ml/datasets.php
- 人脸数据集:https://www.bioid.com/facedb/ (BioD人脸数据库)
BioD人脸数据库pgm文件读取,显示操作:
# 载入脸部数据.得到m*n的数组。m*n为pgm文件的像素
def load_face_data(filepath):
for dir_path, dir_names, file_names in os.walk(filepath):
# walk() 函数内存放的是数据的绝对路径,同时注意斜杠的方向。
for fn in file_names:
if fn[-3:] == 'pgm':
image_filename = os.path.join(dir_path, fn)
x = mh.imread(image_filename, as_grey=True)
print(x.shape[0])
#画出图像
plt.imshow(x)
plt.axis('off') # 不显示坐标轴
plt.show()
return x
用记事本打开
P5为pgm文件版本。
384 为生成数组的列数。286 为生成数组的行数。384*286为像素
255为最大灰度
6、二维数组的一些运算
#得到转置后的数组
def reserve(data):
n = data.shape[0] #行数
m = data.shape[1] #列数
dataT = np.zeros((m,n))
for i in range(n):
for j in range(m):
dataT[j][i] = data[i][j]
return dataT
#行向量转化为列向量
def reshapelist(list):
#print(list)
rl = np.zeros((len(list),1))
for i in range(len(list)):
rl[i][0] = list[i]
return rl