• 1399/05/12

code jalase23 va 24 :

mamnuun misham code ro uplead konid ostad chon man ba error mvajeh misham harkari mikonam...

mamnoon

  • 1399/05/12
  • ساعت 20:32
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline


def draw(x1,x2):
  ln=plt.plot(x1,x2)

def sigmoid(score):
  return 1/(1+np.exp(-score))

def calculate_error(line_parameters, points , y):
  n=points.shape[0]
  p= sigmoid(points*line_parameters)
  cross_entropy=-(1/n)*(np.log(p).T*y + np.log(1-p).T*(1-y))
  return cross_entropy
  
n_pts=10
np.random.seed(0)
bias= np.ones(n_pts)
top_region=np.array([np.random.normal(10,2,n_pts), np.random.normal(12,2,n_pts), bias]).T
bottom_region= np.array([np.random.normal(5,2, n_pts), np.random.normal(6,2, n_pts), bias]).T
all_points=np.vstack((top_region, bottom_region))
w1=-0.1
w2=-0.15
b=0
line_parameters = np.matrix([w1,w2,b]).T
x1=np.array([bottom_region[:,0].min(), top_region[:,0].max()])
x2= -b/w2 + (x1*(-w1/w2))
y=np.array([np.zeros(n_pts), np.ones(n_pts)]).reshape(n_pts*2, 1)

_, ax= plt.subplots(figsize=(4,4))
ax.scatter(top_region[:,0], top_region[:,1], color='r')
ax.scatter(bottom_region[:,0], bottom_region[:,1], color='b')
draw(x1,x2)
plt.show()

print((calculate_error(line_parameters, all_points, y)))

با سلام 

این کد نهایی این قسمت هست

متاسفانه در حین آپلودفایل ویدیو فراموش کردم کد را بارگذاری کنم.

میشه در مورد error تون هم صحبت کنید تا در صورت امکان به کمک هم برطرفش کنیم.


  • 1399/05/12
  • ساعت 20:37

mamnun az inke enghad be shagerdatun ahamiat midin:)

error:

ZeroDivisionError: float division by zero

import numpy as np
import matplotlib.pyplot as plt

def draw(x1, x2):
    ln = plt.plot(x1, x2)

def sigmoid(score):
    return 1/(1+np.exp(-score))

def calculate_error(line_parameters, points, y):
    m = point.shape[0]
    p = sigmoid(points * line_parameteres)
    cross_entropy = -(1 / m) * (np.log(p).T * y + mp.log(1 - p).T * (1 - y))
    return cross_entropy

def gradient_decent(line_parameters, points, y, alpha):
    m = points.shape[0]
    for i in range(2000):
        p = sigmoid(points * line_parameteres)
        gradient = (points.T * (p - y)) * (alpha / m)
        print(gradient)
        line_parameters = line_parameteres - gradient
        w1 = line_parameteres.item(0)
        w2 = line_parameteres.item(1)
        b = line_parameteres.item(2)
        x1 = np.array([points[:, 0].min(), points[:, 0].max()])
        x2 = -b / w2 + x1 * (-w1 / w2)
    draw(x1, x2)
    

n_pts = 100
np.random.seed(0)
bias = np.ones(n_pts)
top_region = np.array([np.random.normal(10, 2, n_pts), np.random.normal(12, 2, n_pts), bias]).T
bottom_region = np.array([np.random.normal(5, 2, n_pts), np.random.normal(6, 2, n_pts), bias]).T
all_point = np.vstack((top_region, bottom_region))
line_parameteres = np.matrix([np.zeros(3)]).T
y = np.array([np.zeros(n_pts), np.ones(n_pts)]).reshape(n_pts*2, 1)

_, ax = plt.subplots(figsize=(4,4))
ax.scatter(top_region[:, 0], top_region[:, 1], color='r')
ax.scatter(bottom_region[:, 0], bottom_region[:, 1], color = 'b')
gradient_decent(line_parameteres, all_point, y, 0.06)
plt.show

inam code 


  • 1399/05/12
  • ساعت 21:30

code jalase 24 am midin lotfan

 


  • 1399/05/12
  • ساعت 21:45
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

def draw(x1,x2):
  ln=plt.plot(x1,x2)

def sigmoid(score):
  return 1/(1+np.exp(-score))

def calculate_error(line_parameters, points , y):
  n=points.shape[0]
  p= sigmoid(points*line_parameters)
  cross_entropy=-(1/n)*(np.log(p).T*y + np.log(1-p).T*(1-y))
  return cross_entropy

def gradient_descent(line_parameters, points, y , alpha):
  n=points.shape[0]
  for i in range(2000):
    p=sigmoid(points*line_parameters)
    gradient= points.T*(p-y)*(alpha/n)
    line_parameters = line_parameters - gradient
    
    w1=line_parameters.item(0)
    w2=line_parameters.item(1)
    b=line_parameters.item(2)
    
    x1=np.array([points[:,0].min(), points[:,0].max()])
    x2= -b/w2 + (x1*(-w1/w2))
  draw(x1,x2) 

  
n_pts=100
np.random.seed(0)
bias= np.ones(n_pts)
top_region=np.array([np.random.normal(10,2,n_pts), np.random.normal(12,2,n_pts), bias]).T
bottom_region= np.array([np.random.normal(5,2, n_pts), np.random.normal(6,2, n_pts), bias]).T
all_points=np.vstack((top_region, bottom_region))

line_parameters = np.matrix([np.zeros(3)]).T
# x1=np.array([bottom_region[:,0].min(), top_region[:,0].max()])
# x2= -b/w2 + (x1*(-w1/w2))
y=np.array([np.zeros(n_pts), np.ones(n_pts)]).reshape(n_pts*2, 1)

_, ax= plt.subplots(figsize=(4,4))
ax.scatter(top_region[:,0], top_region[:,1], color='r')
ax.scatter(bottom_region[:,0], bottom_region[:,1], color='b')
gradient_descent(line_parameters, all_points, y , 0.06)
plt.show()

خواهش می کنم

این بر طرف کردن error هاست که باعث میشه که پیشرفت حاصل بشه

  m = point.shape[0]

توی این لاین(فک کنم لاین هشتم هست) به جای points گذاشتید point  اینو تغییر بدید ببینید اوکی میشه.


logo-samandehi