Курсовая работа «Применение библиотек машинного обучения в интеллектуальном анализе» по дисциплине: «Технологии баз данных»
Download 0.72 Mb.
|
полная курсовая
Приложения
Код программы Модель персептрон Розенблатта: import matplotlib.pyplot as plt import numpy as np import random import math class SemicircleGenerator(object): """docstring for SemicircleGenerator""" def __init__(self, StartLocation, RadiusList, Orientation): super(SemicircleGenerator, self).__init__() self.StartLocation = StartLocation self.MaxRadius = max(RadiusList) self.MinRadius = min(RadiusList) self.Orientation = Orientation def Gen_SemicircleData(self, BatchSize): for _ in range(BatchSize): Radius = random.uniform(self.MinRadius, self.MaxRadius) BiasX = random.uniform(- Radius, Radius) BiasY = math.sqrt(Radius * Radius - BiasX * BiasX) if self.Orientation == "+": yield [BiasX + self.StartLocation[0], BiasY + self.StartLocation[1]] else: yield [self.StartLocation[0] - BiasX, self.StartLocation[1] - BiasY] class Rosenblatt(object): """docstring for Rosenblatt""" def __init__(self, InputNum): super(Rosenblatt, self).__init__() self.Weight = np.zeros([InputNum + 1, 1]) self.TrainRaito = 1 def ActivitionFunction(self, InputData): # print(InputData, np.zeros(InputData.shape)) # print((InputData > np.zeros(InputData.shape)).all()) if (InputData > np.zeros(InputData.shape)).all() == True: # print("1") return 1 else: # print("-1") return -1 def Feedforward(self, InputData): return self.ActivitionFunction( np.matmul(self.Weight.T, np.hstack((np.ones([1, 1]), InputData)).T)) def TrainOneStep(self, InputData, RightResult): Result = self.Feedforward(InputData) if Result != RightResult: self.ChangeWeight(InputData, RightResult, Result) def ChangeWeight(self, InputData, RightResult, Result): # print(self.TrainRaito * # (RightResult - Result)) self.Weight += self.TrainRaito * \ (RightResult - Result) * np.hstack((np.ones([1, 1]), InputData)).T def SetTrainRatio(self, Ratio): self.TrainRaito = Ratio neural = Rosenblatt(2) dataset1 = SemicircleGenerator([0, 0], [4, 6], "+") dataset2 = SemicircleGenerator([7, -2], [4, 6], "-") testdata_x, testdata_y = [], [] for data in dataset1.Gen_SemicircleData(1000): neural.TrainOneStep(np.array([data]), 1) testdata_x.append(data[0]) testdata_y.append(data[1]) for data in dataset2.Gen_SemicircleData(1000): neural.TrainOneStep(np.array([data]), -1) testdata_x.append(data[0]) testdata_y.append(data[1]) print(neural.Weight) x_data, y_data = [-6, 13], [] print(neural.Weight[1][0]) for i in x_data: y_data.append(- (neural.Weight[0][0] + i * neural.Weight[1][0]) / (neural.Weight[2][0])) plt.plot(x_data, y_data, color="red") plt.scatter(testdata_x, testdata_y) plt.show() Download 0.72 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling