# # , import seaborn import matplotlib.pyplot as plt import numpy as np # : w1 * x1 + w2 * x2 + b = 0 def line(x1, x2): return -3 * x1 - 5 * x2 - 2 # x2 = f(x1) ( ) def line_x1(x1): return (-3 * x1 - 2) / 5 # np.random.seed(0) x1x2 = np.random.randn(200, 2) * 2 # for x1, x2 in x1x2: value = line(x1, x2) if (value == 0): # — plt.plot(x1, x2, 'ro', color='blue') elif (value > 0): # — plt.plot(x1, x2, 'ro', color='green') elif (value < 0): # — plt.plot(x1, x2, 'ro', color='red') # plt.gca().set_aspect('equal', adjustable='box') # x1_range = np.arange(-5.0, 5.0, 0.5) plt.plot(x1_range, line_x1(x1_range), color='blue') # plt.xlabel('x1') plt.ylabel('x2') # ! plt.show()
dist(x)= frac|wTx+b|||w||
# # # , import seaborn import matplotlib.pyplot as plt import numpy as np # : w1 * x1 + w2 * x2 + b = 0 def line(x1, x2): return -3 * x1 - 5 * x2 - 2 # x2 = f(x1) ( ) def line_x1(x1): return (-3 * x1 - 2) / 5 # np.random.seed(0) x1x2 = np.random.randn(200, 2) * 2 # for x1, x2 in x1x2: value = line(x1, x2) # , — # — [0, 0.75] # (0) — , - (0.75) — color = str(max(0, 0.75 - np.abs(value) / 30)) plt.plot(x1, x2, 'ro', color=color) # plt.gca().set_aspect('equal', adjustable='box') # x1_range = np.arange(-5.0, 5.0, 0.5) plt.plot(x1_range, line_x1(x1_range), color='blue') # plt.xlabel('x1') plt.ylabel('x2') # ! plt.show()
logistic(x)= frac11+e−x
# # , import seaborn import matplotlib.pyplot as plt import numpy as np # def logit(x): return 1 / (1 + np.exp(-x)) # : w1 * x1 + w2 * x2 + b = 0 def line(x1, x2): return 3 * x1 + 5 * x2 + 2 # x2 = f(x1) ( ) def line_x1(x1): return (-3 * x1 - 2) / 5 # np.random.seed(0) xy = np.random.randn(200, 2) * 2 # for x1, x2 in x1x2: # — value = logit(line(x1, x2) / 2) if (value < 0.001): color = 'red' elif (value > 0.999): color = 'green' else: color = str(0.75 - value * 0.5) plt.plot(x1, x2, 'ro', color=color) # plt.gca().set_aspect('equal', adjustable='box') # x1_range = np.arange(-5.0, 5.0, 0.5) plt.plot(x1_range, line_x1(x1_range), color='blue') # plt.xlabel('x1') plt.ylabel('x2') # ! plt.show()
f(x)= frac11+eAx+B
# # , import seaborn import matplotlib.pyplot as plt import numpy as np # 1 def line1(w1, w2): return -3 * w1 - 5 * w2 - 8 # w2 = f1(w1) ( ) def line1_w1(w1): return (-3 * w1 - 8) / 5 # 2 def line2(w1, w2): return 2 * w1 - 3 * w2 + 4 # w2 = f2(w1) ( ) def line2_w1(w1): return (2 * w1 + 4) / 3 # 3 def line3(w1, w2): return 1.2 * w1 - 3 * w2 + 4 # w2 = f2(w1) ( ) def line3_w1(w1): return (1.2 * w1 + 4) / 3 # 4 def line4(w1, w2): return -5 * w1 - 5 * w2 - 8 # w2 = f2(w1) ( ) def line4_w1(w1): return (-5 * w1 - 8) / 5 # w1_range = np.arange(-5.0, 5.0, 0.5) w2_range = np.arange(-5.0, 5.0, 0.5) # (w1, w2), for w1 in w1_range: for w2 in w2_range: value1 = line1(w1, w2) value2 = line2(w1, w2) value3 = line3(w1, w2) value4 = line4(w1, w2) if (value1 < 0 and value2 > 0 and value3 > 0 and value4 < 0): color = 'green' else: color = 'pink' plt.plot(w1, w2, 'ro', color=color) # plt.gca().set_aspect('equal', adjustable='box') # () 1 plt.plot(w1_range, line1_w1(w1_range), color='blue') # 2 plt.plot(w1_range, line2_w1(w1_range), color='blue') # 3 plt.plot(w1_range, line3_w1(w1_range), color='blue') # 4 plt.plot(w1_range, line4_w1(w1_range), color='blue') # — plt.axis([-7, 7, -7, 7]) # plt.xlabel('w1') plt.ylabel('w2') # ! plt.show()
wnew=wold+ynxnbnew=bold+yn
# # , import seaborn # import matplotlib.pyplot as plt import numpy as np # — np.random.seed(17) # x1x2_green = np.random.randn(200, 2) * 2 + 21 # x1x2_red = np.random.randn(200, 2) * 4 + 5 # x1x2 = np.concatenate((x1x2_green, x1x2_red)) # : +1, -1 labels = np.concatenate((np.ones(x1x2_green.shape[0]), -np.ones(x1x2_red.shape[0]))) # indices = np.array(range(x1x2.shape[0])) np.random.shuffle(indices) x1x2 = x1x2[indices] labels = labels[indices] # w1_ = -1.1 w2_ = 0.5 b_ = -20 # ( ) def lr_line(x1, x2): return w1_ * x1 + w2_ * x2 + b_ # -1 # +1 def decision_unit(value): return -1 if value < 0 else 1 # lines = [[w1_, w2_, b_]] for max_iter in range(100): # # mismatch_count = 0 # for i, (x1, x2) in enumerate(x1x2): # value = lr_line(x1, x2) # (-1, +1) true_label = int(labels[i]) # (-1, +1) pred_label = decision_unit(value) # if (true_label != pred_label): # , .. # — (x1, x2) — +1 # — (-x1, -x2) — -1 # .. +1 w1_ = w1_ + x1 * true_label w2_ = w2_ + x2 * true_label # b_ = b_ + true_label # mismatch_count = mismatch_count + 1 # if (mismatch_count > 0): # lines.append([w1_, w2_, b_]) else: # — break # ( ) for i, (x1, x2) in enumerate(x1x2): pred_label = decision_unit(lr_line(x1, x2)) if (pred_label < 0): plt.plot(x1, x2, 'ro', color='red') else: plt.plot(x1, x2, 'ro', color='green') # plt.gca().set_aspect('equal', adjustable='box') # plt.xlabel('x1') plt.ylabel('x2') # x1_range = np.arange(-30, 50, 0.1) # , # x2 = f(x1) = -(w1 * x1 + b) / w2 def f_lr_line(w1, w2, b): def lr_line(x1): return -(w1 * x1 + b) / w2 return lr_line # it = 0 for coeff in lines: lr_line = f_lr_line(coeff[0], coeff[1], coeff[2]) plt.plot(x1_range, lr_line(x1_range), label = 'it: ' + str(it)) it = it + 1 # plt.axis([-15, 30, -15, 30]) # plt.legend(loc = 'lower left') # ! plt.show()
# NB . . # w1_range = np.arange(0, 10, 0.1) for i, (x1, x2) in enumerate(x1x2): if (labels[i] == 1): color = 'green' else: color = 'red' # x1 * w1 + x2 * w2 + b = 0 # w2 = f(w1) # ( ) def line(w1): return -(x1 * w1 + b_) / x2 # ; — plt.plot(w1_range, line(w1_range), color = color) # , plt.plot(w1_, w2_, 'ro', color='blue') # plt.axis([0, 10, 0, 10]) # plt.gca().set_aspect('equal', adjustable='box') # plt.xlabel('w1') plt.ylabel('w2') # ! plt.show()
# NB . . # w1_range = np.arange(0, 10, 0.5) w2_range = np.arange(0, 10, 0.5) # , for i, (x1, x2) in enumerate(x1x2): def line(w1): return -(x1 * w1 + b_) / x2 if (labels[i] == 1): color = 'green' else: color = 'red' plt.plot(x1_range, line(x1_range), color = color) # (), plt.plot(w1_, w2_, 'ro', color='blue') # def f(w1, w2, x1, x2): value = x1 * w1 + x2 * w2 + b_ return -1 if value < 0 else 1 # ( ) # (data space) good_weights = [] # , # for w1 in w1_range: for w2 in w2_range: in_range = True for i, (x1, x2) in enumerate(x1x2): if (labels[i] != f(w1, w2, x1, x2)): in_range = False break if (in_range): good_weights.append([w1, w2, b_]) # () plt.plot(w1, w2, 'ro', color = 'magenta') # plt.axis([0, 10, 0, 10]) # plt.gca().set_aspect('equal', adjustable='box') # plt.xlabel('w1') plt.ylabel('w2') # ! plt.show() # ( ) def lr_line(x1, x2): return w1_ * x1 + w2_ * x2 + b_ # -1 # +1 def decision_unit(value): return -1 if value < 0 else 1 # ( ) for i, (x1, x2) in enumerate(x1x2): pred_label = decision_unit(lr_line(x1, x2)) if (pred_label < 0): plt.plot(x1, x2, 'ro', color='red') else: plt.plot(x1, x2, 'ro', color='green') # x1_range = np.arange(-30, 50, 0.1) for (w1, w2, _b) in good_weights: # , x1, x2 — def w_line(x1): return -(w1 * x1 + b_) / w2 # plt.plot(x1_range, w_line(x1_range)) # plt.axis([0, 25, 0, 25]) # plt.gca().set_aspect('equal', adjustable='box') # plt.xlabel('x1') plt.ylabel('x2') # ! plt.show()
Source: https://habr.com/ru/post/324736/
All Articles