python-机器学习代码总结
1、实现⼆维正态分布(⾼斯分布)
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
2. ⽣成符合⼆维正态分布的随机数
import numpy as np
mean1 =[0,0]
cov1 =[[1,1],[1,6]]
data = np.random.multivariate_normal(mean1, cov1,100)
3. 协⽅差矩阵
#得到协⽅差矩阵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))# 求矩阵的逆矩阵
anspose())# 求矩阵的转置矩阵
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'python生成1到100之间随机数
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、数据集来源
1. UCI数据集:archive.ics.uci.edu/ml/datasets.php
2. ⼈脸数据集:www.bioid/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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。