

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>hep_ml.speedup &mdash; hep_ml 0.7.0 documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> hep_ml
          

          
          </a>

          
            
            
              <div class="version">
                0.7.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../index.html">hep_ml documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../gb.html">Gradient boosting</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../losses.html">Losses for Gradient Boosting</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../uboost.html">uBoost</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../metrics.html">Metric functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../nnet.html">Neural networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../preprocessing.html">Preprocessing data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../reweight.html">Reweighting algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../speedup.html">Fast predictions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../splot.html">sPlot</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notebooks.html">Code Examples</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">hep_ml</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>hep_ml.speedup</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for hep_ml.speedup</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">**hep_ml.speedup** is module to obtain formulas with machine learning,</span>
<span class="sd">which can be applied very fast (with a speed comparable to simple selections),</span>
<span class="sd">while keeping high quality of classification.</span>

<span class="sd">In many application (i.e. triggers in HEP) it is pressing to get really fast formula.</span>
<span class="sd">This module contains tools to prepare formulas, which can be applied with the speed comparable to cuts.</span>

<span class="sd">Example</span>
<span class="sd">-------</span>
<span class="sd">Let&#39;s show how one can use some really heavy classifier and still have fast predictions:</span>

<span class="sd">&gt;&gt;&gt; from sklearn.ensemble import RandomForestClassifier</span>
<span class="sd">&gt;&gt;&gt; from hep_ml.speedup import LookupClassifier</span>
<span class="sd">&gt;&gt;&gt; base_classifier = RandomForestClassifier(n_estimators=1000, max_depth=25)</span>
<span class="sd">&gt;&gt;&gt; classifier = LookupClassifier(base_estimator=base_classifier, keep_trained_estimator=False)</span>
<span class="sd">&gt;&gt;&gt; classifier.fit(X, y, sample_weight=sample_weight)</span>

<span class="sd">Though training takes much time, all predictions are precomputed and saved to lookup table,</span>
<span class="sd">so you are able to predict millions of events per second using single CPU:</span>

<span class="sd">&gt;&gt;&gt; classifier.predict_proba(testX)</span>


<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span><span class="p">,</span> <span class="n">print_function</span><span class="p">,</span> <span class="n">absolute_import</span>
<span class="kn">import</span> <span class="nn">numpy</span>
<span class="kn">import</span> <span class="nn">pandas</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span>
<span class="kn">from</span> <span class="nn">sklearn.base</span> <span class="kn">import</span> <span class="n">ClassifierMixin</span><span class="p">,</span> <span class="n">BaseEstimator</span><span class="p">,</span> <span class="n">clone</span>
<span class="kn">from</span> <span class="nn">.commonutils</span> <span class="kn">import</span> <span class="n">to_pandas_dataframe</span><span class="p">,</span> <span class="n">check_xyw</span><span class="p">,</span> <span class="n">check_sample_weight</span><span class="p">,</span> <span class="n">weighted_quantile</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s1">&#39;Alex Rogozhnikov&#39;</span>


<div class="viewcode-block" id="LookupClassifier"><a class="viewcode-back" href="../../speedup.html#hep_ml.speedup.LookupClassifier">[docs]</a><span class="k">class</span> <span class="nc">LookupClassifier</span><span class="p">(</span><span class="n">BaseEstimator</span><span class="p">,</span> <span class="n">ClassifierMixin</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">base_estimator</span><span class="p">,</span> <span class="n">n_bins</span><span class="o">=</span><span class="mi">16</span><span class="p">,</span> <span class="n">max_cells</span><span class="o">=</span><span class="mi">500000000</span><span class="p">,</span> <span class="n">keep_trained_estimator</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        LookupClassifier splits each of features into bins, trains a base_estimator to use this data.</span>
<span class="sd">        To predict class for new observation, results of base_estimator are kept for all possible combinations of bins,</span>
<span class="sd">        and saved together</span>

<span class="sd">        :param base_estimator: classifier used to build predictions</span>
<span class="sd">        :param n_bins:</span>

<span class="sd">            * int: how many bins to use for each axis</span>
<span class="sd">            * dict: feature_name -&gt; int, specialize how many bins to use for each axis</span>
<span class="sd">            * dict: feature_name -&gt; list of floats, set manually edges of bins</span>

<span class="sd">            By default, the (weighted) quantiles are used to compute bin edges.</span>
<span class="sd">        :type n_bins: int | dict</span>

<span class="sd">        :param int max_cells: raise error if lookup table will have more items.</span>
<span class="sd">        :param bool keep_trained_estimator: if True, trained estimator will be saved.</span>

<span class="sd">        See also: this idea is used inside LHCb triggers, see V. Gligorov, M. Williams, &#39;Bonsai BDT&#39;</span>

<span class="sd">        Resulting formula is very simple and can be rewritten in other language or environment (C++, CUDA, etc).</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">base_estimator</span> <span class="o">=</span> <span class="n">base_estimator</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">n_bins</span> <span class="o">=</span> <span class="n">n_bins</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_cells</span> <span class="o">=</span> <span class="n">max_cells</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">keep_trained_estimator</span> <span class="o">=</span> <span class="n">keep_trained_estimator</span>

<div class="viewcode-block" id="LookupClassifier.check_dimensions"><a class="viewcode-back" href="../../speedup.html#hep_ml.speedup.LookupClassifier.check_dimensions">[docs]</a>    <span class="k">def</span> <span class="nf">check_dimensions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bin_edges</span><span class="p">):</span>
        <span class="n">cumulative_size</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">cumprod</span><span class="p">([</span><span class="nb">len</span><span class="p">(</span><span class="n">bin_edge</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">bin_edge</span> <span class="ow">in</span> <span class="n">bin_edges</span><span class="o">.</span><span class="n">items</span><span class="p">()])</span>
        <span class="k">if</span> <span class="n">numpy</span><span class="o">.</span><span class="n">any</span><span class="p">(</span><span class="n">cumulative_size</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_cells</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;the total size of lookup table exceeds </span><span class="si">{}</span><span class="s1">, &#39;</span>
                             <span class="s1">&#39;reduce n_bins or number of features in use&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_cells</span><span class="p">))</span></div>

<div class="viewcode-block" id="LookupClassifier.fit"><a class="viewcode-back" href="../../speedup.html#hep_ml.speedup.LookupClassifier.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">sample_weight</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Train a classifier and collect predictions for all possible combinations.</span>

<span class="sd">        :param X: pandas.DataFrame or numpy.array with data of shape [n_samples, n_features]</span>
<span class="sd">        :param y: array with labels of shape [n_samples]</span>
<span class="sd">        :param sample_weight: None or array of shape [n_samples] with weights of events</span>
<span class="sd">        :return: self</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">classes_</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
        <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">normed_weights</span> <span class="o">=</span> <span class="n">check_xyw</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">sample_weight</span><span class="o">=</span><span class="n">sample_weight</span><span class="p">,</span> <span class="n">classification</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">X</span> <span class="o">=</span> <span class="n">to_pandas_dataframe</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
        <span class="n">normed_weights</span> <span class="o">=</span> <span class="n">check_sample_weight</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">sample_weight</span><span class="o">=</span><span class="n">normed_weights</span><span class="p">,</span> <span class="n">normalize_by_class</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">normalize</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">bin_edges</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_compute_bin_edges</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">normed_weights</span><span class="o">=</span><span class="n">normed_weights</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_dimensions</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bin_edges</span><span class="p">)</span>

        <span class="n">n_parameter_combinations</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">prod</span><span class="p">([</span><span class="nb">len</span><span class="p">(</span><span class="n">bin_edge</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">bin_edge</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">bin_edges</span><span class="o">.</span><span class="n">items</span><span class="p">()])</span>

        <span class="n">transformed_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
        <span class="n">trained_estimator</span> <span class="o">=</span> <span class="n">clone</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">base_estimator</span><span class="p">)</span>
        <span class="n">fit_params</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="n">sample_weight</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">fit_params</span><span class="p">[</span><span class="s1">&#39;sample_weights&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">sample_weight</span>
        <span class="n">trained_estimator</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">transformed_data</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="o">**</span><span class="n">fit_params</span><span class="p">)</span>

        <span class="n">all_lookup_indices</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">n_parameter_combinations</span><span class="p">))</span>
        <span class="n">all_combinations</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">convert_lookup_index_to_bins</span><span class="p">(</span><span class="n">all_lookup_indices</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_lookup_table</span> <span class="o">=</span> <span class="n">trained_estimator</span><span class="o">.</span><span class="n">predict_proba</span><span class="p">(</span><span class="n">all_combinations</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">keep_trained_estimator</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">trained_estimator</span> <span class="o">=</span> <span class="n">trained_estimator</span>

        <span class="k">return</span> <span class="bp">self</span></div>

    <span class="k">def</span> <span class="nf">_compute_bin_edges</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">normed_weights</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute edges of bins, weighted quantiles are used,</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">bins_over_axis</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">X</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n_bins</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
                <span class="n">bins_over_axis</span><span class="p">[</span><span class="n">column</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">n_bins</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">bins_over_axis</span><span class="p">[</span><span class="n">column</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">n_bins</span><span class="p">[</span><span class="n">column</span><span class="p">]</span>

        <span class="n">bin_edges</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">column</span><span class="p">,</span> <span class="n">column_bins</span> <span class="ow">in</span> <span class="n">bins_over_axis</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">column_bins</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
                <span class="n">quantiles</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="n">column_bins</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">bin_edges</span><span class="p">[</span><span class="n">column</span><span class="p">]</span> <span class="o">=</span> <span class="n">weighted_quantile</span><span class="p">(</span><span class="n">X</span><span class="p">[</span><span class="n">column</span><span class="p">],</span> <span class="n">quantiles</span><span class="o">=</span><span class="n">quantiles</span><span class="p">,</span> <span class="n">sample_weight</span><span class="o">=</span><span class="n">normed_weights</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">bin_edges</span><span class="p">[</span><span class="n">column</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">column_bins</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">bin_edges</span>

<div class="viewcode-block" id="LookupClassifier.convert_bins_to_lookup_index"><a class="viewcode-back" href="../../speedup.html#hep_ml.speedup.LookupClassifier.convert_bins_to_lookup_index">[docs]</a>    <span class="k">def</span> <span class="nf">convert_bins_to_lookup_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bins_indices</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param bins_indices: numpy.array of shape [n_samples, n_columns], filled with indices of bins.</span>
<span class="sd">        :return: numpy.array of shape [n_samples] with corresponding index in lookup table</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">lookup_indices</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">bins_indices</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
        <span class="n">bins_indices</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">bins_indices</span><span class="p">)</span>
        <span class="k">assert</span> <span class="n">bins_indices</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bin_edges</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">column_name</span><span class="p">,</span> <span class="n">bin_edges</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bin_edges</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
            <span class="n">lookup_indices</span> <span class="o">*=</span> <span class="nb">len</span><span class="p">(</span><span class="n">bin_edges</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="n">lookup_indices</span> <span class="o">+=</span> <span class="n">bins_indices</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">lookup_indices</span></div>

<div class="viewcode-block" id="LookupClassifier.convert_lookup_index_to_bins"><a class="viewcode-back" href="../../speedup.html#hep_ml.speedup.LookupClassifier.convert_lookup_index_to_bins">[docs]</a>    <span class="k">def</span> <span class="nf">convert_lookup_index_to_bins</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lookup_indices</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param lookup_indices: array of shape [n_samples] with positions at lookup table</span>
<span class="sd">        :return: array of shape [n_samples, n_features] with indices of bins.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">result</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">([</span><span class="nb">len</span><span class="p">(</span><span class="n">lookup_indices</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bin_edges</span><span class="p">)],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;uint8&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">column_name</span><span class="p">,</span> <span class="n">bin_edges</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bin_edges</span><span class="o">.</span><span class="n">items</span><span class="p">()))[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
            <span class="n">n_columns</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">bin_edges</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="n">result</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">lookup_indices</span> <span class="o">%</span> <span class="n">n_columns</span>
            <span class="n">lookup_indices</span> <span class="o">=</span> <span class="n">lookup_indices</span> <span class="o">//</span> <span class="n">n_columns</span>

        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="LookupClassifier.transform"><a class="viewcode-back" href="../../speedup.html#hep_ml.speedup.LookupClassifier.transform">[docs]</a>    <span class="k">def</span> <span class="nf">transform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Convert data to bin indices.</span>

<span class="sd">        :param X: pandas.DataFrame or numpy.array with data</span>
<span class="sd">        :return: numpy.array, where each column is replaced with index of bin</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">X</span> <span class="o">=</span> <span class="n">to_pandas_dataframe</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
        <span class="k">assert</span> <span class="nb">list</span><span class="p">(</span><span class="n">X</span><span class="o">.</span><span class="n">columns</span><span class="p">)</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bin_edges</span><span class="o">.</span><span class="n">keys</span><span class="p">()),</span> <span class="s1">&#39;passed dataset with wrong columns&#39;</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;uint8&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">X</span><span class="o">.</span><span class="n">columns</span><span class="p">):</span>
            <span class="n">edges</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bin_edges</span><span class="p">[</span><span class="n">column</span><span class="p">]</span>
            <span class="n">result</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">searchsorted</span><span class="p">(</span><span class="n">edges</span><span class="p">,</span> <span class="n">X</span><span class="p">[</span><span class="n">column</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="LookupClassifier.predict"><a class="viewcode-back" href="../../speedup.html#hep_ml.speedup.LookupClassifier.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Predict class for each event</span>

<span class="sd">        :param X: pandas.DataFrame with data</span>
<span class="sd">        :return: array of shape [n_samples] with predicted class labels.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="n">numpy</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">predict_proba</span><span class="p">(</span><span class="n">X</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)]</span></div>

<div class="viewcode-block" id="LookupClassifier.predict_proba"><a class="viewcode-back" href="../../speedup.html#hep_ml.speedup.LookupClassifier.predict_proba">[docs]</a>    <span class="k">def</span> <span class="nf">predict_proba</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Predict probabilities for new observations</span>

<span class="sd">        :param X: pandas.DataFrame with data</span>
<span class="sd">        :return: probabilities, array of shape [n_samples, n_classes]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">bins_indices</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
        <span class="n">lookup_indices</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">convert_bins_to_lookup_index</span><span class="p">(</span><span class="n">bins_indices</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lookup_table</span><span class="p">[</span><span class="n">lookup_indices</span><span class="p">]</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2015-2017, Yandex; Alex Rogozhnikov and contributors.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>