Beispiel #1
0
 def ssr_curve(self,
               x,
               y,
               slopes=[
                   0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 1.0, 1.5, 2.0, 2.5, 3.0,
                   4.0, 5.0, 7.5, 10.0
               ]):
     ssrs = []
     for slope in slopes:
         yint = (np.mean(y) - slope * np.mean(x))
         ssrs.append(
             self.get_ss_res(zip(x, y), lambda val: slope * val + yint))
     image_manager = ImageManager()
     plotter = Plotter()
     plotter.set_title('Sum of Squared Residuals')
     plotter.set_axis_labels('Slope Selected', 'Sum of Squared Residual')
     plotter.set_output_filename(g.files['ls-ssr'])
     ssr_plot = ScatterSketch()
     ssr_plot.add_x(slopes)
     ssr_plot.add_y(ssrs)
     plotter.load(ssr_plot)
     plotter.save()
     plotter.close()
     g.debug.prn(self, 'Drawn Sum of Squared Residuals Plot')
     image_manager.scale(g.files['ls-ssr'], g.files['ls-ssr'], 250)
Beispiel #2
0
    def plot(self):
        plotter = Plotter()
        image_manager = ImageManager()
        sketches = []
        plotter.set_output_filename(g.files['ridge-regression'])
        plotter.set_title('Ridge Regression')
        g.debug.prn(self, 'Plot basics set.')

        min_x = min(self.training_x)
        max_x = max(self.training_x)
        x_vals = []
        y_vals = []
        for x in range(min_x * 100, max_x * 100):
            x_vals.append([x / 100])
            y_vals.append([self.f(x / 100)])
        sketches.append(SmoothSketch())
        sketches[-1].add_x(x_vals)
        sketches[-1].add_y(y_vals)
        g.debug.prn(self, 'Linear curve saved as SmoothSketch.')

        sketches.append(ScatterSketch())
        sketches[-1].add_x(list(self.training_x))
        sketches[-1].add_y(list(self.training_y))
        g.debug.prn(self, 'Points saved as ScatterSketch.')

        for i in range(len(self.training_x)):
            if self.f(self.training_x[i]) > self.training_y[i]:
                y_max = self.f(self.training_x[i])
                y_min = self.training_y[i]
            else:
                y_min = self.f(self.training_x[i])
                y_max = self.training_y[i]
            sketches.append(VerticalLineSketch())
            sketches[-1].set_y_max(y_max)
            sketches[-1].set_y_min(y_min)
            sketches[-1].set_x(self.training_x[i])
            g.debug.prn(self, 'Vertical line appended.', 3)
        g.debug.prn(self, 'SSR lines drawn as VerticalLineSketch(s).')

        plotter.load(sketches)
        plotter.save()
        plotter.close()
        g.debug.prn(self, 'All sketches loaded and saved.')

        image_manager.scale(g.files['ridge-regression'],
                            g.files['ridge-regression'], 250)

        del plotter
        del image_manager
        g.debug.prn(self, 'Plotter and ImageManager objects deleted', 3)
Beispiel #3
0
    def plot(self):
        plotter = Plotter()
        image_manager = ImageManager()
        sketches = []
        plotter.set_output_filename(g.files['least-squares'])
        plotter.set_title('Least Squares')

        # min_x = min(self.training.get_output()[x_index])
        # max_x = max(self.training.get_output()[x_index])
        min_x = min(self.training_x)
        max_x = max(self.training_x)
        x_vals = []
        y_vals = []
        for x in range(min_x * 100, max_x * 100):
            x_vals.append([x / 100])
            y_vals.append([self.f(x / 100)])
        sketches.append(SmoothSketch())
        sketches[-1].add_x(x_vals)
        sketches[-1].add_y(y_vals)

        sketches.append(ScatterSketch())
        sketches[-1].add_x(list(self.training_x))
        sketches[-1].add_y(list(self.training_y))

        for i in range(len(self.training_x)):
            if self.f(self.training_x[i]) > self.training_y[i]:
                y_max = self.f(self.training_x[i])
                y_min = self.training_y[i]
            else:
                y_min = self.f(self.training_x[i])
                y_max = self.training_y[i]
            sketches.append(VerticalLineSketch())
            sketches[-1].set_y_max(y_max)
            sketches[-1].set_y_min(y_min)
            sketches[-1].set_x(self.training_x[i])

        plotter.load(sketches)
        plotter.save()
        plotter.close()

        image_manager.scale(g.files['least-squares'], g.files['least-squares'],
                            250)

        del plotter
        del image_manager
Beispiel #4
0
    def f_dist(self, model_type, trials):
        plotter = Plotter()
        image_manager = ImageManager()

        plotter.set_title('F Distribution')
        plotter.set_axis_labels('Frequency', 'F Score')
        plotter.set_output_filename(g.files['least-squares-f'])

        histogram = HistogramSketch()
        for i in range(trials):
            x_vals = g.randomizer.random_list(g.points_to_gen, g.lower_x_bound,
                                              g.upper_x_bound)
            y_vals = g.randomizer.random_list(g.points_to_gen, g.lower_y_bound,
                                              g.upper_y_bound)

            if model_type == LinearModel:
                slope, yint = self.least_squares_slope_yint_eqn(x_vals, y_vals)
                func = lambda x: slope * x + yint
            else:
                g.debug.prn(self, 'Incompatible model type.', 1)
                break

            ss_fit = self.get_ss_res(zip(x_vals, y_vals), func)
            ss_mean = self.get_ss_res(zip(x_vals, y_vals),
                                      lambda x: np.mean(x_vals))
            p_fit = 2  # TODO: Update for Dataframe
            p_mean = 1  # ""
            n = len(x_vals)

            if ss_fit == 0 or (n - p_fit) == 0 or (p_fit - p_mean) == 0:
                self.debug.prn(self, 'F distribution cannot divide by zero.',
                               1)
                continue
            numerator = (ss_mean - ss_fit) / (p_fit - p_mean)
            denominator = ss_fit / (n - p_fit)

            histogram.add_x(numerator / denominator)
            histogram.set_bins()

        plotter.load(histogram)
        plotter.save()
        plotter.close()
        image_manager.scale(g.files['least-squares-f'],
                            g.files['least-squares-f'], 250)
        self.debug.prn(self, 'F distribution created.')
Beispiel #5
0
    def plot(self):
        plotter = Plotter()
        image_manager = ImageManager()
        sketches = []
        plotter.set_output_filename(g.files['logistic-regression'])
        plotter.set_title('Logistic Regression')
        g.debug.prn(self, 'Plot basics set.')

        min_x = min(self.training_x)
        max_x = max(self.training_x)
        min_y = min(self.training_y)
        max_y = max(self.training_y)
        x_vals = []
        y_vals = []
        for x in range(min_x * 100, max_x * 100):
            y_adjust = self.f(x / 100) * (max_y - min_y) + min_y
            x_vals.append([x / 100])
            y_vals.append([y_adjust])
        sketches.append(SmoothSketch())
        sketches[-1].add_x(x_vals)
        sketches[-1].add_y(y_vals)
        g.debug.prn(self, 'Curve added to sketches list.')

        sketches.append(ScatterSketch())
        sketches[-1].add_x(list(self.training_x))
        sketches[-1].add_y(list(self.training_y))
        g.debug.prn(self, 'Scatter of points added to sketches list.')

        plotter.load(sketches)
        plotter.save()
        plotter.close()
        g.debug.prn(self, 'All sketches loaded and saved.')

        image_manager.scale(g.files['logistic-regression'],
                            g.files['logistic-regression'], 250)

        del plotter
        del image_manager
        g.debug.prn(self, 'Plotter and ImageManager objects deleted', 3)