def call_tsne(self, other_args: List[str]):
        """Process tsne command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="tsne",
            description=
            """Get similar companies to compare with using sklearn TSNE.""",
        )
        parser.add_argument(
            "-l",
            "--learnrate",
            default=200,
            dest="lr",
            type=check_non_negative,
            help="TSNE Learning rate.  Typical values are between 50 and 200",
        )
        parser.add_argument("-p",
                            "--no_plot",
                            action="store_true",
                            default=False,
                            dest="no_plot")

        try:
            ns_parser = parse_known_args_and_warn(parser, other_args)
            if not ns_parser:
                return
            self.similar = yahoo_finance_model.get_sp500_comps_tsne(
                self.ticker, lr=ns_parser.lr, no_plot=ns_parser.no_plot)
            print(f"[ML] Similar Companies: {', '.join(self.similar)}", "\n")
        except Exception as e:
            print(e, "\n")
    def call_tsne(self, other_args: List[str]):
        """Process tsne command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="tsne",
            description=
            """Get similar companies to compare with using sklearn TSNE.""",
        )
        parser.add_argument(
            "-r",
            "--learnrate",
            default=200,
            dest="lr",
            type=check_non_negative,
            help="TSNE Learning rate.  Typical values are between 50 and 200",
        )
        parser.add_argument(
            "-l",
            "--limit",
            default=10,
            dest="limit",
            type=check_positive,
            help=
            "Limit of stocks to retrieve. The subsample will occur randomly.",
        )
        parser.add_argument("-p",
                            "--no_plot",
                            action="store_true",
                            default=False,
                            dest="no_plot")
        if other_args and "-" not in other_args[0][0]:
            other_args.insert(0, "-l")
        ns_parser = parse_known_args_and_warn(parser, other_args)
        if ns_parser:
            if self.ticker:
                self.similar = yahoo_finance_model.get_sp500_comps_tsne(
                    self.ticker,
                    lr=ns_parser.lr,
                    no_plot=ns_parser.no_plot,
                    num_tickers=ns_parser.limit,
                )

                self.similar = [self.ticker] + self.similar
                console.print(
                    f"[ML] Similar Companies: {', '.join(self.similar)}", "\n")

            else:
                console.print(
                    "You need to 'set' a ticker to get similar companies from first!"
                )
Example #3
0
def test_get_sp500_comps_tsne(mocker, recorder):
    # FORCE SINGLE THREADING
    yf_download = yahoo_finance_model.yf.download

    def mock_yf_download(*args, **kwargs):
        kwargs["threads"] = False
        return yf_download(*args, **kwargs)

    mocker.patch("yfinance.download", side_effect=mock_yf_download)

    mocker.patch(
        "gamestonk_terminal.stocks.comparison_analysis.yahoo_finance_model.normalize",
        side_effect=lambda x: x,
    )
    mocker.patch("matplotlib.pyplot.show")
    mocker.patch(
        "sklearn.manifold.TSNE.fit_transform",
        side_effect=lambda x: np.full((len(x), 2), 1),
    )
    result_df = yahoo_finance_model.get_sp500_comps_tsne(
        ticker="TOT.TO",
    )

    recorder.capture(result_df)