Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions python修改张铁海/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

p13/__pycache__/__init__.cpython-35.pyc
p13/__init__.py
p13/__pycache__/p13.cpython-35.pyc
p13/__pycache__/compVariance.cpython-35.pyc
p13/__pycache__/compMean.cpython-35.pyc
text.py
泡泡.py
5 changes: 5 additions & 0 deletions python修改张铁海/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
n=10
p=[]
for i in range(n):
p.append(1/n)
print(p)
84 changes: 84 additions & 0 deletions python修改张铁海/p10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def getPositon(a):
raw, column = a.shape # get the matrix of a raw and column
positon = np.argmax(a) # get the index of max in the a
# print(positon)
r, c = divmod(positon , column)
return r,c

def sence(p,z,world,pSenseCorrect):
# nRow=len(p[0])
# nCol=len(p[:0])
nRow,nCol=np.shape(p)
q_size=(nRow,nCol)
q=np.zeros(q_size)
for r in range(0,nRow):
for c in range(0,nCol):
if z== world[r][c]:
hit=1
else:
hit=0
q[r,c]=p[r,c]*(hit * pSenseCorrect + (1-hit) * (1-pSenseCorrect))
S=sum(q)
for i in range(len(q)):
q[i]=q[i]/sum(S)
return q

world = (('red', 'green', 'green', 'red', 'red'),
('red', 'red', 'green', 'red', 'red'),
('red', 'red', 'green', 'green', 'red'),
('red', 'red', 'red', 'red', 'red'))
nRow = len(world)
nCol = len(world[1])
print(world[1][1])
pStart = 0.7
ones_size=(nRow,nCol)
ones=np.ones(ones_size)
p = (1 - pStart) / (nRow * nCol - 1) * ones
pSenseCorrect=0.7
p[2,1]=pStart
measurements=['green']
# print(measurements[0])
q=sence(p,measurements[0],world,pSenseCorrect)
print("The Prior:\n",p)
print("The probability after sensing:\n",q)
r,c=getPositon(q)
print('The largest probability %s occurs at cell(%s,%s)\n'%(q[r,c],r,c))

plt.ion()
h=plt.figure(1)
ax=Axes3D(h)
ly= len(p[0]) #5 # Work out matrix dimensions
lx= len(p[:,0]) #4
xpos = np.arange(0,lx,1) # Set up a mesh of positions
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)
xpos = xpos.flatten() # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx*ly)

dx = np.ones_like(zpos)
dy = dx.copy()
dz = p.T.flatten()
ax.bar3d(xpos,ypos,zpos, dx, dy, dz)

h1=plt.figure(2)
ax=Axes3D(h1)
ly= len(q[0]) #5 # Work out matrix dimensions
lx= len(q[:,0]) #4
xpos = np.arange(0,lx,1) # Set up a mesh of positions
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)
xpos = xpos.flatten() # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx*ly)

dx = np.ones_like(zpos)
dy = dx.copy()
dz = q.T.flatten()
ax.bar3d(xpos,ypos,zpos, dx, dy, dz)
plt.ioff()
plt.show()
136 changes: 136 additions & 0 deletions python修改张铁海/p11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def getPositon(a):
raw, column = a.shape # get the matrix of a raw and column
positon = np.argmax(a) # get the index of max in the a
# print(positon)
r, c = divmod(positon , column)
return r,c

def move(p,u,pMoveCorrect):
nRow=int(len(p))
nCol=int(len(p[0]))
q=np.zeros(ones_size)
for r in range(1,nRow+1):
for c in range(1,nCol+1):
q[r-1,c-1]=pMoveCorrect * p[np.mod(r-1-u[0],nRow),np.mod(c-1-u[1],nCol)]+ (1-pMoveCorrect)* p[r-1,c-1]
# return q
return q

def sence(p,z,world,pSenseCorrect):
# nRow=len(p[0])
# nCol=len(p[:0])
nRow,nCol=np.shape(p)
q_size=(nRow,nCol)
q=np.zeros(q_size)
for r in range(0,nRow):
for c in range(0,nCol):
if z== world[r][c]:
hit=1
else:
hit=0
q[r,c]=p[r,c]*(hit * pSenseCorrect + (1-hit) * (1-pSenseCorrect))
S=sum(q)
for i in range(len(q)):
q[i]=q[i]/sum(S)
return q

def compEntropy(p):
compEntropy = -sum(sum(p * np.log2(p)))
return compEntropy


world = (('red', 'green', 'green', 'red', 'red'),
('red', 'red', 'green', 'red', 'red'),
('red', 'red', 'green', 'green', 'red'),
('red', 'red', 'red', 'red', 'red'))
nRow = len(world)
nCol = len(world[1])
print(nRow,nCol)
step={
'stop':[0,0],
'right':[0,1],
'left':[0,-1],
'down':[1,0],
'up':[-1,0]
}
pMoveCorrect = 0.8
pSenseCorrect=0.7
ones_size=(nRow,nCol)
ones=np.ones(ones_size)
p = 1/ (nRow * nCol) * ones
# compEntropy=-sum(sum(p * np.log2(p)))

#First configuration
motions=[step['stop'],step['right'],step['down'],step['down'],step['right']]
measurements=['green','green','green','green','green']
#Check the size
if len(motions)!= len(measurements):
print("The variable 'motions' should be of the same size as 'measurements' ! ")
entropy_size=(2,len(motions))
entropy=np.zeros(entropy_size)
#The main loop
plt.ion()
for i in range(0,len(motions)):
# plt.ion()
p=move(p,motions[i],pMoveCorrect)
p0=p
p=sence(p,measurements[i],world,pSenseCorrect)
#Compute entropy
entropy[:,i]=[compEntropy(p0),compEntropy(p)]
# Make figures
h=plt.figure(1)
ax1=h.add_subplot(2,1,1,projection='3d')
# ax = Axes3D(h)
plt.cla()
plt.title('Step %s\n The probability before sensing'%i)
ly = len(p0[0]) # 5 # Work out matrix dimensions
lx = len(p0[:, 0]) # 4
xpos = np.arange(0, lx, 1) # Set up a mesh of positions
ypos=(1,2,3,4,5)
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten() # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx * ly)

dx = np.ones_like(zpos)
dy = dx.copy()
dz = p0.T.flatten()
# ax1.set(color='r,b,g')
ax1.bar3d(xpos, ypos, zpos, dx, dy, dz,color='b',alpha=0.6)

ax2=h.add_subplot(2,1,2,projection='3d')
ly = len(p[0]) # 5 # Work out matrix dimensions
lx = len(p[:, 0]) # 4
xpos = np.arange(0, lx, 1) # Set up a mesh of positions
ypos = np.arange(0, ly, 1)
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten() # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx * ly)
dx = np.ones_like(zpos)
dy = dx.copy()
dz = p.T.flatten()
# plt.cla()
col=np.arange(30)
# plt.scatter(xpos,ypos,zpos,c=col)
ax2.bar3d(xpos, ypos, zpos, dx, dy, dz,color='g',alpha=0.6)
plt.pause(0.5)

print('The Posterior:\n',p)
r,c=getPositon(p)
print('The largest probability %.4f occurs at cell (%d, %d)\n '%(p[r,c],r,c))

h2=plt.figure(2)
motions_size=len(motions)
plt.plot(range(1,motions_size+1),entropy[0,:],'bh',linewidth=3,markersize=10)
plt.plot(range(1,motions_size+1),entropy[1,:],'rx',linewidth=3,markersize=10)

plt.xlim(0,(len(motions)+1))
plt.xlabel('Step')
plt.ylabel('Entropy')
plt.legend(('After sensing', 'Before sensing'),loc='upper right')
plt.ioff()
plt.show()
16 changes: 16 additions & 0 deletions python修改张铁海/p12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

pCan=0.001
pNon=0.999
z='positive'
# z='negative'
pPosCan=0.8
pPosNon=0.1
if z=='positive':
p=[pPosCan*pCan, pPosNon*pNon]
else:
p=[(1-pPosCan)*pCan,(1-pPosNon)*pNon]
S=sum(p)
for i in range(len(p)):
p[i]=p[i]/sum(p)
print(p[0])
print(p[1])
31 changes: 31 additions & 0 deletions python修改张铁海/p13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from compMean import compMean
from compVariance import compVariance
import numpy as np

def mean(x): #计算均值的
S = np.sum(x) #矩阵中每个元素求和必须用np.sum
shape = x.shape[:]
mean=S/(shape[0]*shape[1])
return mean

def compVariance(x): #计算方差的
mu=mean(x)
mu=np.mat(mu)
x2=np.square(x-mu)
sigma2=mean(x2)
return sigma2
def compMean(x): #计算均值的
S=np.sum(x)
shape=x.shape[:]
mu=S/(shape[0]*shape[1])
return mu

x=[7,38,4,23,18]
# x=[17,19,18,17,19]
# x=[18,18,18,18,18]
x=np.mat(x)
mu=compMean(x)
sigma2=compVariance(x)
print('The Expectation / Mean:\t %s \n'%mu)
print('The Variance is:\t\t %s \n'%sigma2)
print('The Standard Deviation is:\t %s \n'%np.sqrt(sigma2))
6 changes: 6 additions & 0 deletions python修改张铁海/p13/compMean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import numpy as np
def compMean(x): #计算均值的
S=np.sum(x)
shape=x.shape[:]
mu=S/(shape[0]*shape[1])
return mu
13 changes: 13 additions & 0 deletions python修改张铁海/p13/compVariance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import numpy as np
def mean(x): #计算均值的
S = np.sum(x) #矩阵中每个元素求和必须用np.sum
shape = x.shape[:]
mean=S/(shape[0]*shape[1])
return mean

def compVariance(x): #计算方差的
mu=mean(x)
mu=np.mat(mu)
x2=np.square(x-mu)
sigma2=mean(x2)
return sigma2
13 changes: 13 additions & 0 deletions python修改张铁海/p13/p13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from compMean import compMean
from compVariance import compVariance
import numpy as np

x=[7,38,4,23,18]
# x=[17,19,18,17,19]
# x=[18,18,18,18,18]
x=np.mat(x)
mu=compMean(x)
sigma2=compVariance(x)
print('The Expectation / Mean:\t %.2f \n'%mu)
print('The Variance is:\t\t %.2f \n'%sigma2)
print('The Standard Deviation is:\t %.2f \n'%np.sqrt(sigma2))
13 changes: 13 additions & 0 deletions python修改张铁海/p14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import numpy as np

from p13 import compMean #这种方式引用在使用函数时,前面必须加文件名 compMean.compMean(x)
# import compMean 这种方式引用,使用函数时,前面不用加文件名
a=2
b=4
x=[7,38,4,23,18]
x=np.mat(x)
y=a*x + b
xmu=compMean.compMean(x)
ymu=compMean.compMean(y)
print('a * Xmu + b \t= %s \n'%(a * xmu + b))
print('Ymu \t\t= %.2f \n'%ymu)
13 changes: 13 additions & 0 deletions python修改张铁海/p15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import numpy as np
from p13 import compMean
from p13 import compVariance

x= [7, 38, 4, 23, 18]
x=np.mat(x)
x2= np.square(x)
# print(x2)
x2mu= compMean.compMean(x2)
xmu = compMean.compMean(x)
xvar= compVariance.compVariance(x)
print('The Variance of X \t= %.2f \n'%xvar)
print('E[X^2]-E[X]^2 \t\t= %.2f \n'%(x2mu - np.square(xmu)))
Loading