def process(self, df): df = self._processor(df) if self.verbose: printv("Values after passing through %s like: " % self.__class__.__name__) printv(df[:5]) return df
def _format_data(self, df): """ Needs already cleaned up data """ printv("Splitting data into train and test") # Split into training and test data. tr, ts = Splitter(TS_SIZE).split(df) # Split columns into input and output tr, ts = self.model.io_split(tr), self.model.io_split(ts) printv("Data split with element count: (train, test) = (%d, %d)" % (len(tr.input), len(ts.input))) return tr, ts
def preprocess_data(df): print = printv df = df.iloc[1:323] for processor in FILTERS: printv("Applying filter: %s" % processor.__class__.__name__) df = processor.process(df) # print(np.isnan(df.any()), np.isfinite(df.all())) df.to_csv("temp.csv") printv("Filters applied. Final columns:") printv(df.columns) printv("\nFinal cleaned up data looks like:\n", df[:3]) printv() return df
def run(self, df): # Make the neural net model if not self.model_name: self.model_name = DEFAULT_MODEL self.model = make_model(self.model_name) # Split data tr, ts = self._format_data(df) with SequentialNeuralNet(self.model, self.args.modelfile) as net: net.train(tr) predicted = pd.DataFrame(net.predict(ts.input), columns=self.model.output_cols) predicted = self.model.post_process(ts, predicted) printv("Predicted data like:\n", predicted[:5]) if self.show_graph: self.graph(ts, predicted)
def graph(self, ts_data, predicted): """ Plot the predicted data vs. original data """ printv("Graphing predicted values") fig, ax = plt.subplots(nrows=int(math.ceil(len(predicted.columns) / 2)), ncols=2) fig.suptitle("Actual vs. predicted values for neural net") for i, ax in enumerate(ax.reshape(-1)): if i == len(predicted.columns): break col_name = predicted.columns[i] self._graph_column(col_name, ax, ts_data.output[:, i], predicted.loc[:, col_name]) plt.show() # Prompt to save save = self.args.graphfile if self.args.graphfile\ else input("Save Graph (Enter filename to save, blank to skip)?: ") if save: self._save_graph(fig, save)
def main(args): df = pd.read_csv(DATA1) # Basic cleanup utils.printv("\n### Preprocessing data ###\n") df = preprocess_data(df) if args.basic_graph or TO_GRAPH: utils.printv("\n### Graphing Data ###\n") basic_graph(df) if args.nnmodel != NOMODEL: # Make an graph the neural net utils.printv("\n### Running the network ###\n") runner = NeuralNetRunner(args) runner.run(df)
def plot(self, block=True): utils.printv("Plotting graphs...") plt.show(block)