Related
I have a pseudoinverse problem as follows:
Y = W.T # X
Where
Y.shape = (2, 800)
W.shape = (9, 2)
X.shape = (9, 800)
I have Y and X and I am looking for W. I used numpy.linalg.pinv.
W = Y # numpy.linalg.pinv(X)
But the results did not match: I found this
W.T # X != Y
What did I miss here?
Here is my code:
X = np.random.random(size=(9, 800))
Y = np.random.randint(low=0, high=2, size=(2, 800))
Xinv = np.linalg.pinv(X)
W = Y # Xinv
W # X # != Y ???
Suppose a grid is defined by a set of grid parameters: its origin (x0, y0), an angel between one side and x axis, and increments and - please see the figure below.
There are scattered points with known coordinates on the grid but they don’t exactly fall on grid intersections. Is there an algorithm to find a set of grid parameters to define the grid so that the points are best fit to grid intersections?
Suppose the known coordinates are:
(2 , 5.464), (3.732, 6.464), (5.464, 7.464)
(3 , 3.732), (4.732, 4.732), (6.464, 5.732)
(4 , 2 ), (5.732, 3 ), (7.464, 4 ).
I expect the algorithm to find the origin (4, 2), the angle 30 degree, and the increments both 2.
You can solve the problem by finding a matrix that transforms points from positions (0, 0), (0, 1), ... (2, 2) onto the given points.
Although the grid has only 5 degrees of freedom (position of the origin + angle + scale), it is easier to define the transformation using 2x3 matrix A, because the problem can be made linear in this case.
Let a point with index (x0, y0) to be transformed into point (x0', y0') on the grid, for example (0, 0) -> (2, 5.464) and let a_ij be coefficients of matrix A. Then this pair of points results in 2 equations:
a_00 * x0 + a_01 * y0 + a_02 = x0'
a_10 * x0 + a_11 * y0 + a_12 = y0'
The unknowns are a_ij, so these equations can be written in form
a_00 * x0 + a_01 * y0 + a_02 + a_10 * 0 + a_11 * 0 + a_12 * 0 = x0'
a_00 * 0 + a_01 * 0 + a_02 * 0 + a_10 * x0 + a_11 * y0 + a_12 = y0'
or in matrix form
K0 * (a_00, a_01, a_02, a_10, a_11, a_12)^T = (x0', y0')^T
where
K0 = (
x0, y0, 1, 0, 0, 0
0, 0, 0, x0, y0, 1
)
These equations for each pair of points can be combined in a single equation
K * (a_00, a_01, a_02, a_10, a_11, a_12)^T = (x0', y0', x1', y1', ..., xn', yn')^T
or K * a = b
where
K = (
x0, y0, 1, 0, 0, 0
0, 0, 0, x0, y0, 1
x1, y1, 1, 0, 0, 0
0, 0, 0, x1, y1, 1
...
xn, yn, 1, 0, 0, 0
0, 0, 0, xn, yn, 1
)
and (xi, yi), (xi', yi') are pairs of corresponding points
This can be solved as a non-homogeneous system of linear equations. In this case the solution will minimize sum of squares of distances from each point to nearest grid intersection. This transform can be also considered to maximize overall likelihood given the assumption that points are shifted from grid intersections with normally distributed noise.
a = (K^T * K)^-1 * K^T * b
This algorithm can be easily implemented if there is a linear algebra library is available. Below is an example in Python:
import numpy as np
n_points = 9
aligned_points = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
grid_points = [(2, 5.464), (3.732, 6.464), (5.464, 7.464), (3, 3.732), (4.732, 4.732), (6.464, 5.732), (4, 2), (5.732, 3), (7.464, 4)]
K = np.zeros((n_points * 2, 6))
b = np.zeros(n_points * 2)
for i in range(n_points):
K[i * 2, 0] = aligned_points[i, 0]
K[i * 2, 1] = aligned_points[i, 1]
K[i * 2, 2] = 1
K[i * 2 + 1, 3] = aligned_points[i, 0]
K[i * 2 + 1, 4] = aligned_points[i, 1]
K[i * 2 + 1, 5] = 1
b[i * 2] = grid_points[i, 0]
b[i * 2 + 1] = grid_points[i, 1]
# operator '#' is matrix multiplication
a = np.linalg.inv(np.transpose(K) # K) # np.transpose(K) # b
A = a.reshape(2, 3)
print(A)
[[ 1. 1.732 2. ]
[-1.732 1. 5.464]]
Then the parameters can be extracted from this matrix:
theta = math.degrees(math.atan2(A[1, 0], A[0, 0]))
scale_x = math.sqrt(A[1, 0] ** 2 + A[0, 0] ** 2)
scale_y = math.sqrt(A[1, 1] ** 2 + A[0, 1] ** 2)
origin_x = A[0, 2]
origin_y = A[1, 2]
theta = -59.99927221917264
scale_x = 1.99995599951599
scale_y = 1.9999559995159895
origin_x = 1.9999999999999993
origin_y = 5.464
However there remains a minor issue: matrix A corresponds to an affine transform. This means that grid axes are not guaranteed to be perpendicular. If this is a problem, then the first two columns of the matrix can be modified in a such way that the transform preserves angles.
Update: I fixed the mistakes and resolved sign ambiguities, so now this algorithm produces the expected result. However it should be tested to see if all cases are handled correctly.
Here is another attempt to solve this problem. The idea is to decompose transformation into non-uniform scaling matrix and rotation matrix A = R * S and then solve for coefficients sx, sy, r1, r2 of these matrices given restriction that r1^2 + r2^2 = 1. The minimization problem is described here: How to find a transformation (non-uniform scaling and similarity) that maps one set of points to another?
def shift_points(points):
n_points = len(points)
shift = tuple(sum(coords) / n_points for coords in zip(*points))
shifted_points = [(point[0] - shift[0], point[1] - shift[1]) for point in points]
return shifted_points, shift
n_points = 9
aligned_points = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
grid_points = [(2, 5.464), (3.732, 6.464), (5.464, 7.464), (3, 3.732), (4.732, 4.732), (6.464, 5.732), (4, 2), (5.732, 3), (7.464, 4)]
aligned_points, aligned_shift = shift_points(aligned_points)
grid_points, grid_shift = shift_points(grid_points)
c1, c2 = 0, 0
b11, b12, b21, b22 = 0, 0, 0, 0
for i in range(n_points):
c1 += aligned_points[i][0] ** 2
c2 += aligned_points[i][0] ** 2
b11 -= 2 * aligned_points[i][0] * grid_points[i][0]
b12 -= 2 * aligned_points[i][1] * grid_points[i][0]
b21 -= 2 * aligned_points[i][0] * grid_points[i][1]
b22 -= 2 * aligned_points[i][1] * grid_points[i][1]
k = (b11 ** 2 * c2 + b22 ** 2 * c1 - b21 ** 2 * c2 - b12 ** 2 * c1) / \
(b21 * b11 * c2 - b12 * b22 * c1)
# r1_sqr and r2_sqr might need to be swapped
r1_sqr = 2 / (k ** 2 + 4 + k * math.sqrt(k ** 2 + 4))
r2_sqr = 2 / (k ** 2 + 4 - k * math.sqrt(k ** 2 + 4))
for sign1, sign2 in [(1, 1), (-1, 1), (1, -1), (-1, -1)]:
r1 = sign1 * math.sqrt(r1_sqr)
r2 = sign2 * math.sqrt(r2_sqr)
scale_x = -b11 / (2 * c1) * r1 - b21 / (2 * c1) * r2
scale_y = b12 / (2 * c2) * r2 - b22 / (2 * c2) * r1
if scale_x >= 0 and scale_y >= 0:
break
theta = math.degrees(math.atan2(r2, r1))
There might be ambiguities in choosing r1_sqr and r2_sqr. Origin point can be estimated from aligned_shift and grid_shift, but I didn't implement it yet.
theta = -59.99927221917264
scale_x = 1.9999559995159895
scale_y = 1.9999559995159895
SELECT TCID, START_TIME, RESULT,
cast(START_TIME as date) as m_date,
max(cast(START_TIME as time)) as max_time
FROM jenkins_result.JENKINS_RESULT
WHERE TCID = 'A330506'
GROUP BY TCID, m_date;
This is my data:
ID TCID START_DATE RESULT
1545240 A435727 2020-11-08 03:11:43 PASS
1545334 A435727 2020-11-08 03:19:53 PASS
1547439 A435727 2020-11-09 03:11:52 PASS
1547621 A435727 2020-11-09 03:20:05 PASS
1548388 A435727 2020-11-09 07:51:29 PASS
1558801 A435727 2020-11-12 00:11:10 PASS
1561899 A435727 2020-11-12 08:48:59 PASS
I want to get result of each TCID follow date like this
ID TCID START_DATE RESULT
1545334 A435727 2020-11-08 03:19:53 PASS
1548388 A435727 2020-11-09 07:51:29 PASS
1561899 A435727 2020-11-12 08:48:59 PASS
But the result current like that:
1545240 A435727 2020-11-08 03:11:43 PASS 2020-11-08 03:19:53
1547439 A435727 2020-11-09 03:11:52 PASS 2020-11-09 07:51:29
1558801 A435727 2020-11-12 00:11:10 PASS 2020-11-12 08:48:59
def connect_cli_server(self):
connect_success = 0
if self.ssh_client is None:
self.ssh_client = paramiko.SSHClient()
self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
for cnt in range(self.retry_cnt):
try:
self.ssh_client.connect(self.ip, 22, self.id, self.pw, timeout=self.time_out,
banner_timeout=self.banner)
connect_success = 1
break
except:
if cnt < 10:
time.sleep(random.uniform(0.1, 0.3))
if 10 <= cnt < 20:
time.sleep(random.uniform(0.1, 1))
else:
time.sleep(random.uniform(0.5, 1.5))
continue
if not connect_success:
try:
self.connect_cli_server_thru_remote_server()
except Exception as error:
print(error)
return False
return True
def send_command(self, ssh_client, command):
chan = ssh_client.get_transport().open_session()
chan.get_pty()
fileobject = chan.makefile()
chan.exec_command(command)
byteoutput = fileobject.read()
convetedstring = byteoutput.decode("UTF-8")
return convetedstring
I created some sample input by sql command
create table tbl_mock (
id int,
tc int,
startdate datetime,
result varchar(20)
);
insert into tbl_mock(id, tc, startdate, result) values (1, 1, '2020/11/12 09:00:00', 'pass');
insert into tbl_mock(id, tc, startdate, result) values (2, 1, '2020/11/12 10:00:00', 'fail');
insert into tbl_mock(id, tc, startdate, result) values (3, 1, '2020/11/12 11:00:00', 'pass');
insert into tbl_mock(id, tc, startdate, result) values (4, 1, '2020/11/13 09:00:00', 'pass');
insert into tbl_mock(id, tc, startdate, result) values (5, 1, '2020/11/13 10:00:00', 'fail');
insert into tbl_mock(id, tc, startdate, result) values (6, 1, '2020/11/13 11:00:00', 'fail');
You can try the below sql command to get your result
select tbl_a.*
from tbl_mock as tbl_a,
(select tc,
cast(startdate as date) as m_date,
max(cast(startdate as time)) as m_time
from tbl_mock
group by tc, m_date) as tbl_b
where tbl_a.tc = tbl_b.tc
and timestamp(tbl_b.m_date, tbl_b.m_time) = tbl_a.startdate
You can try this:
SELECT TCID
,START_TIME
,RESULT
,cast(START_TIME as date) as m_date
,max(cast(START_TIME as time)) as max_time
FROM jenkins_result.JENKINS_RESULT
WHERE TCID='A330506'
GROUP BY TCID
,START_TIME
,RESULT
,cast(START_TIME as date)
ORDER BY TCID
,m_date;
which should be the same as this:
SELECT DISTINCT TCID
,START_TIME
,RESULT
,cast(START_TIME as date) as m_date
,max(cast(START_TIME as time)) OVER() as max_time
FROM jenkins_result.JENKINS_RESULT
WHERE TCID='A330506'
ORDER BY TCID
,m_date;
or if you need to get the MAX value per TCID:
SELECT DISTINCT TCID
,START_TIME
,RESULT
,cast(START_TIME as date) as m_date
,max(cast(START_TIME as time)) OVER(PARTITION BY TCID) as max_time
FROM jenkins_result.JENKINS_RESULT
WHERE TCID='A330506'
ORDER BY TCID
,m_date;
import pygame
import os
import numpy as np
import queue
pygame.init()
q = 8
w = 70
def normalize_image(img):
return pygame.transform.scale(img, (w - 10, w - 10))
agent = pygame.image.load(os.path.join('EmptyAgent.png'))
wumpus = pygame.image.load(os.path.join('Wumpus.png'))
gold = pygame.image.load(os.path.join('Gold.png'))
pit = pygame.image.load(os.path.join('Pit.png'))
agent = normalize_image(agent)
wumpus = normalize_image(wumpus)
gold = normalize_image(gold)
pit = normalize_image(pit)
gr_empty = 0
gr_agent = 4
gr_wumpus = 1
gr_gold = 3
gr_pit = 2
clock = pygame.time.Clock()
screen = pygame.display.set_mode((q * w, q * w))
xh, yh = q - 1, 0
grid = [[gr_empty] * q for _ in range(q)]
grid[xh][yh] = gr_agent
bg_color = (255, 255, 255)
border_color = (0, 0, 0)
dx = [0,1,0,-1]
dy = [1,0,-1,0]
def bfs():
vs = np.zeros((q,q,8))
tracex = np.zeros((q,q,8))
tracey = np.zeros((q,q,8))
traced = np.zeros((q,q,8))
tracex[0][0][0] = 2
que = queue.Queue()
que.put((q-1,0,0,0))
vs[q-1][0][0]=1
gx, gy, gd, gt = 0, 0, 0, 1000
while not que.empty():
(x,y,dir,t) = que.get()
d=0
if grid[x][y]==3 and dir<4:
if gt>t:
gx, gy, gd, gt = x,y,dir,t
if vs[x][y][dir+4]==0:
que.put((x,y,dir+4,t+1))
vs[x][y][dir+4]=1
tracex[x][y][dir+4],tracey[x][y][dir+4],traced[x][y][dir+4] = x,y,dir
continue
if x==q-1 and y==0 and dir>3:
return 1000-(t+1)*10, gx, gy, gd, tracex, tracey, traced
if x<0 or y<0 or x>=q or y>=q:
continue
if grid[x][y]==gr_wumpus or grid[x][y]==gr_pit:
continue
if dir>3:
d+=4
dir-=4
if vs[x][y][(dir + 1 + 4) % 4 + d] == 0:
que.put((x, y, (dir + 1 + 4) % 4 + d, t + 1))
vs[x][y][(dir + 1 + 4) % 4 + d] = 1
tracex[x][y][(dir + 1 + 4) % 4 + d], tracey[x][y][(dir + 1 + 4) % 4 + d], traced[x][y][(dir + 1 + 4) % 4 + d] = x, y, dir
if vs[x][y][(dir - 1 + 4) % 4 + d] == 0:
que.put((x, y, (dir - 1 + 4) % 4 + d, t + 1))
vs[x][y][(dir - 1 + 4) % 4 + d] = 1
tracex[x][y][(dir - 1 + 4) % 4 + d], tracey[x][y][(dir - 1 + 4) % 4 + d], traced[x][y][(dir - 1 + 4) % 4 + d] = x, y, dir
if x+dx[dir]<0 or x+dx[dir]>=q or y+dy[dir]<0 or y+dy[dir]>=q:
continue
if vs[x + dx[dir]][y + dy[dir]][dir + d] == 0:
que.put((x + dx[dir], y + dy[dir], dir + d, t + 1))
vs[x + dx[dir]][y + dy[dir]][dir + d] = 1
tracex[x + dx[dir]][y + dy[dir]][dir + d], tracey[x + dx[dir]][y + dy[dir]][dir + d], traced[x + dx[dir]][y + dy[dir]][dir + d] = x, y, dir
return -1, gx, gy, gd, tracex, tracey, traced
def test():
grid[q-2][1] = gr_wumpus
grid[q-3][2] = gr_gold
grid[q-3][0] = gr_pit
ans, gx, gy, gd, tracex, tracey, traced = bfs()
if ans<0:
ans=-1
print(ans)
tx, ty, td = gx, gy, gd
if ans>=0:
while 1:
print(tx, ty, td)
if tx==q-1 and ty==0:
break
xn, yn, dn = int(tracex[tx][ty][td]), int(tracey[tx][ty][td]), int(traced[tx][ty][td])
tx, ty, td = xn,yn,dn
# print(tracex[0][0][0])
def draw_img(img, x, y):
screen.blit(img, (x + 5, y + 5))
def draw():
x, y = 0, 0
screen.fill(bg_color)
for i in range(q):
for j in range(q):
if grid[i][j] == gr_agent:
draw_img(agent, x, y)
elif grid[i][j] == gr_wumpus:
draw_img(wumpus, x, y)
elif grid[i][j] == gr_gold:
draw_img(gold, x, y)
elif grid[i][j] == gr_pit:
draw_img(pit, x, y)
for k in range(4):
pygame.draw.rect(screen, border_color, (x - k, y - k, w, w), 1)
x = x + w
x = 0
y = y + w
def main():
test()
while True:
events = pygame.event.get()
keys = pygame.key.get_pressed()
for event in events:
if event.type == pygame.QUIT:
return
elif event.type == pygame.MOUSEBUTTONDOWN:
mx, my = event.pos
gy = mx // w
gx = my // w
if not grid[gx][gy] == gr_empty:
continue
elif event.button == 1:
# Left click
grid[gx][gy] = gr_wumpus
elif event.button == 3:
# Scroll slick
grid[gx][gy] = gr_gold
elif event.button == 2:
# Middle click
grid[gx][gy] = gr_pit
draw()
pygame.display.flip()
clock.tick(600)
main()
import pygame
import os
import numpy as np
import queue
pygame.init()
q = 3
w = 70
def normalize_image(img):
return pygame.transform.scale(img, (w - 10, w - 10))
agent = pygame.image.load(os.path.join('EmptyAgent.png'))
wumpus = pygame.image.load(os.path.join('Wumpus.png'))
gold = pygame.image.load(os.path.join('Gold.png'))
pit = pygame.image.load(os.path.join('Pit.png'))
agent = normalize_image(agent)
wumpus = normalize_image(wumpus)
gold = normalize_image(gold)
pit = normalize_image(pit)
gr_empty = 0
gr_agent = 4
gr_wumpus = 1
gr_gold = 3
gr_pit = 2
clock = pygame.time.Clock()
screen = pygame.display.set_mode((q * w, q * w))
xh, yh = q - 1, 0
grid = [[gr_empty] * q for _ in range(q)]
grid[xh][yh] = gr_agent
bg_color = (255, 255, 255)
border_color = (0, 0, 0)
dx = [0,1,0,-1]
dy = [1,0,-1,0]
def bfs():
vs = np.zeros((q,q,8))
tracex = np.zeros((q,q,8))
tracey = np.zeros((q,q,8))
traced = np.zeros((q,q,8))
tracex[0][0][0] = 2
que = queue.Queue()
que.put((q-1,0,0,0))
vs[q-1][0][0]=1
gx, gy, gd, gt = 0, 0, 0, 1000
while not que.empty():
(x,y,dir,t) = que.get()
d=0
if grid[x][y]==3 and dir<4:
if gt>t:
gx, gy, gd, gt = x,y,dir,t
if vs[x][y][dir+4]==0:
que.put((x,y,dir+4,t+1))
vs[x][y][dir+4]=1
tracex[x][y][dir+4],tracey[x][y][dir+4],traced[x][y][dir+4] = x,y,dir
continue
if x==q-1 and y==0 and dir>3:
gx, gy, gd, gt = x, y, dir, t
return 1000-(t+1)*10, gx, gy, gd, tracex, tracey, traced
if x<0 or y<0 or x>=q or y>=q:
continue
if grid[x][y]==gr_wumpus or grid[x][y]==gr_pit:
continue
if dir>3:
d+=4
dir-=4
if vs[x][y][(dir + 1 + 4) % 4 + d] == 0:
que.put((x, y, (dir + 1 + 4) % 4 + d, t + 1))
vs[x][y][(dir + 1 + 4) % 4 + d] = 1
tracex[x][y][(dir + 1 + 4) % 4 + d], tracey[x][y][(dir + 1 + 4) % 4 + d], traced[x][y][(dir + 1 + 4) % 4 + d] = x, y, dir + d
if vs[x][y][(dir - 1 + 4) % 4 + d] == 0:
que.put((x, y, (dir - 1 + 4) % 4 + d, t + 1))
vs[x][y][(dir - 1 + 4) % 4 + d] = 1
tracex[x][y][(dir - 1 + 4) % 4 + d], tracey[x][y][(dir - 1 + 4) % 4 + d], traced[x][y][(dir - 1 + 4) % 4 + d] = x, y, dir + d
if x+dx[dir]<0 or x+dx[dir]>=q or y+dy[dir]<0 or y+dy[dir]>=q:
continue
if vs[x + dx[dir]][y + dy[dir]][dir + d] == 0:
que.put((x + dx[dir], y + dy[dir], dir + d, t + 1))
vs[x + dx[dir]][y + dy[dir]][dir + d] = 1
tracex[x + dx[dir]][y + dy[dir]][dir + d], tracey[x + dx[dir]][y + dy[dir]][dir + d], traced[x + dx[dir]][y + dy[dir]][dir + d] = x, y, dir + d
return -1, gx, gy, gd, tracex, tracey, traced
def test():
grid[q-2][1] = gr_wumpus
grid[q-3][2] = gr_gold
grid[q-3][0] = gr_pit
ans, gx, gy, gd, tracex, tracey, traced = bfs()
if ans<0:
ans=-1
print(ans)
tx, ty, td = gx, gy, gd
if ans>=0:
while 1:
print(tx, ty, td)
if tx==q-1 and ty==0 and td==0:
break
xn, yn, dn = int(tracex[tx][ty][td]), int(tracey[tx][ty][td]), int(traced[tx][ty][td])
tx, ty, td = xn,yn,dn
# print(tracex[0][0][0])
def draw_img(img, x, y):
screen.blit(img, (x + 5, y + 5))
def draw():
x, y = 0, 0
screen.fill(bg_color)
for i in range(q):
for j in range(q):
if grid[i][j] == gr_agent:
draw_img(agent, x, y)
elif grid[i][j] == gr_wumpus:
draw_img(wumpus, x, y)
elif grid[i][j] == gr_gold:
draw_img(gold, x, y)
elif grid[i][j] == gr_pit:
draw_img(pit, x, y)
for k in range(4):
pygame.draw.rect(screen, border_color, (x - k, y - k, w, w), 1)
x = x + w
x = 0
y = y + w
def main():
test()
while True:
events = pygame.event.get()
keys = pygame.key.get_pressed()
for event in events:
if event.type == pygame.QUIT:
return
elif event.type == pygame.MOUSEBUTTONDOWN:
mx, my = event.pos
gy = mx // w
gx = my // w
if not grid[gx][gy] == gr_empty:
continue
elif event.button == 1:
# Left click
grid[gx][gy] = gr_wumpus
elif event.button == 3:
# Scroll slick
grid[gx][gy] = gr_gold
elif event.button == 2:
# Middle click
grid[gx][gy] = gr_pit
draw()
pygame.display.flip()
clock.tick(600)
main()
I need to implement the following pseudocode to JuMP/Julia:
forall{i in M, j in Ni[i]}: x[i] <= y[j];
I imagine something like:
for i in M and j in Ni[i]
#constraint(model, x[i] <= y[j])
end
How do I properly implement the 2 iterators in the for loop?
I don't know if you want one iteration with both values, or the Cartesian product of the iterators, but here are example for both:
julia> M = 1:3; N = 4:6;
julia> for (m, n) in zip(M, N) # single iterator over both M and N
#show m, n
end
(m, n) = (1, 4)
(m, n) = (2, 5)
(m, n) = (3, 6)
julia> for m in M, n in N # Cartesian product
#show m, n
end
(m, n) = (1, 4)
(m, n) = (1, 5)
(m, n) = (1, 6)
(m, n) = (2, 4)
(m, n) = (2, 5)
(m, n) = (2, 6)
(m, n) = (3, 4)
(m, n) = (3, 5)
(m, n) = (3, 6)
You want
#constraint(model, [i = M, j = Ni[i]], x[i] <= y[j])
Here is the relevant documentation: https://www.juliaopt.org/JuMP.jl/stable/constraints/#Constraint-containers-1
From deeplearning course on Coursera I've implemented logistic regression :
import numpy as np
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
def sigmoid(z):
s = 1 / (1 + np.exp(-z))
return s
def initialize_with_zeros(dim):
w = np.zeros(shape=(dim, 1))
b = 0
return w, b
def propagate(w, b, X, Y):
m = X.shape[1]
A = sigmoid(np.dot(w.T, X) + b) # compute activation
cost = (- 1 / m) * np.sum(Y * np.log(A) + (1 - Y) * (np.log(1 - A))) # compute cost
dw = (1 / m) * np.dot(X, (A - Y).T)
db = (1 / m) * np.sum(A - Y)
cost = np.squeeze(cost)
grads = {"dw": dw,
"db": db}
return grads, cost
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
costs = []
for i in range(num_iterations):
grads, cost = propagate(w, b, X, Y)
dw = grads["dw"]
db = grads["db"]
w = w - learning_rate * dw # need to broadcast
b = b - learning_rate * db
if i % 100 == 0:
costs.append(cost)
# Print the cost every 100 training examples
if print_cost and i % 100 == 0:
print ("Cost after iteration %i: %f" % (i, cost))
params = {"w": w,
"b": b}
grads = {"dw": dw,
"db": db}
return params, grads, costs
def predict(w, b, X):
m = X.shape[1]
Y_prediction = np.zeros((1, m))
w = w.reshape(X.shape[0], 1)
A = sigmoid(np.dot(w.T, X) + b)
for i in range(A.shape[1]):
# Convert probabilities a[0,i] to actual predictions p[0,i]
### START CODE HERE ### (≈ 4 lines of code)
print(A)
Y_prediction[0, i] = 1 if A[0, i] > 0.5 else 0
### END CODE HERE ###
assert(Y_prediction.shape == (1, m))
return Y_prediction
print ("sigmoid(0) = " + str(sigmoid(0)))
print ("sigmoid(9.2) = " + str(sigmoid(9.2)))
dim = 2
w, b = initialize_with_zeros(dim)
print ("w = " + str(w))
print ("b = " + str(b))
w, b, X, Y = np.array([[1], [2]]), 2, np.array([[-1,-2], [3,4]]), np.array([[1, 0]])
grads, cost = propagate(w, b, X, Y)
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))
params, grads, costs = optimize(w, b, X, Y, num_iterations= 10000, learning_rate = 0.01, print_cost = False)
print ("w = " + str(params["w"]))
print ("b = " + str(params["b"]))
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print("predictions = " + str(predict(w, b, X)))
def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
w, b = initialize_with_zeros(X_train.shape[0])
parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
w = parameters["w"]
b = parameters["b"]
Y_prediction_test = predict(w, b, X_test)
Y_prediction_train = predict(w, b, X_train)
print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))
d = {"costs": costs,
"Y_prediction_test": Y_prediction_test,
"Y_prediction_train" : Y_prediction_train,
"w" : w,
"b" : b,
"learning_rate" : learning_rate,
"num_iterations": num_iterations}
return d
I'm attempting to use a generic dataset which contains 5 samples where each sample contain 4 elements :
train_set_x = np.array([[1,2,3,4],[4,3,2,1],[1,2,3,4],[4,3,2,1],[1,2,3,4]])
train_set_y = np.array([1,0,1,0,1])
test_set_x = np.array([[1,2,3,4],[4,3,2,1],[1,2,3,4],[4,3,2,1],[1,2,3,4]])
test_set_y = np.array([1,0,1,0,1])
train_set_x , train_set_y , test_set_x , test_set_y
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005, print_cost = True)
But the following error is thrown :
<ipython-input-409-bd4e233a8f4e> in propagate(w, b, X, Y)
18
19 A = sigmoid(np.dot(w.T, X) + b) # compute activation
---> 20 cost = (- 1 / m) * np.sum(Y * np.log(A) + (1 - Y) * (np.log(1 - A))) # compute cost
21
22 dw = (1 / m) * np.dot(X, (A - Y).T)
ValueError: operands could not be broadcast together with shapes (5,) (1,4)
Do I need to change the weight dimensions in order to compute the cost value ?
Update :
Using modification :
A = sigmoid(np.dot(X , w) + b) # compute activation
causes error :
<ipython-input-546-7a7980550834> in propagate(w, b, X, Y)
20 m = X.shape[1]
21
---> 22 A = sigmoid(np.dot(X , w) + b) # compute activation
23 print('w.T' , w.T , 'w' , w, 'X' , X , 'Y' , Y , 'A' , A)
24 cost = (- 1 / m) * np.sum(Y * np.log(A) + (1 - Y) * (np.log(1 - A))) # compute cost
ValueError: shapes (5,4) and (5,1) not aligned: 4 (dim 1) != 5 (dim 0)