<div dir="ltr"><div>To summarize - for mds, spectralembedding, it looks like there is no transform method that will satisfy both<br></div><div><br></div><div>  1. fit(X).transform(X)  == fit_transform(X)</div><div>  2. transform(X)[i:i+1] == transform(X[i:i+1])</div><div><br></div><div>that's because the current fit_transform doesn't factor nicely into those 2 steps. The last step returns a subset of eigenvalues of a modified Gram matrix.</div><div><br></div><div>For PCA, kernel PCA, LLE, fit_transform is something like: center data, do U,S,V = SVD, project data onto submatrix of V. The last step is matrix multiplication. Last step in transform methods there are np.dot(...). That factors nicely.</div><div><br></div><div>There could be a transform_batch method for mds which would satisfy 1., then transform could call transform_batch rowwise, to satisfy 2, but no single method will work.</div><div><br></div><div>I don't know if there is appetite for separation and modification of unittests involved.</div><font color="#888888"><div><br></div><div>Charles</div></font></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jan 21, 2020 at 9:19 PM Charles Pehlivanian <<a href="mailto:pehlivaniancharles@gmail.com">pehlivaniancharles@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">    This is what I thought we usually do. It looks like you said we are
    doing a greedy transform.<br>    I'm not sure I follow that. In particular for spectral embedding for
    example there is a pretty way to describe<br>    the transform and that's what we're doing. You could also look at
    doing transductive learning but that's<br><div>    not really the standard formulation, is it?</div><div><br></div><div>Batch transform becomes greedy if one does:</div><div><br></div><div>        for x_i in X:<br>            X_new_i = self.transform(x_i)</div><div><br></div><div>I said that LLE uses greedy algorithm. The algorithm implemented is pointwise. It may be that that's the only approach (in which case it's not greedy), but I don't think so - looks like all of the spectral embedding, lle, mds transforms have batch versions. So I probably shouldn't call it greedy. Taking a *true* batch transform and enclosing it in a loop like that - I'm calling that greedy. I'm honestly not sure if the LLE qualifies.</div><div><br></div><div>Spectral embedding - agree, the method you refer to is implemented in fit_transform(). How to apply to oos points? </div><div><br><span style="white-space:pre-wrap">    Non-distributable, non-subset-invariant, optimal batch transform</span><br></div>    Can you give an example of that?<div><br></div><div>Most of the manifold learners can be expressed as solutions to eigenvalue/vector problems. For MDS batch transform, form a new constrained double-centered distance matrix and solve a constrained least-squares problem that mimics the SVD solution to the eigenvalue problem.  They're all like this - least-squares estimates for some constrained eigenvalue problem. The question is whether you want to solve the full problem, or solve on each point, adding one row and optimzing each time, ... that would be subset-invariant though.</div><div><br></div><div>For this offline/batch approach to an oos transform, the only way I see to make it pass tests is to enclose it in a loop as above. That's what I see at least.</div><div><br></div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jan 21, 2020 at 8:35 PM Andreas Mueller <<a href="mailto:t3kcit@gmail.com" target="_blank">t3kcit@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
  
    
  
  <div>
    <br>
    <br>
    <div>On 1/21/20 8:23 PM, Charles Pehlivanian
      wrote:<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">
        <pre style="white-space:pre-wrap;color:rgb(0,0,0)">I understand - I'm kind of conflating the idea of data sample with test set, my view assumes there are a sample space of samples, might require rethinking the cross-validation setup...</pre>
        <pre style="white-space:pre-wrap;color:rgb(0,0,0)">I also think that part of it relies on the notion of online vs. offline algorithm. For offline fits, a batch transform (non-subset invariant) is preferred. For a transformer that can only be used in an online sense, or is primarily used that way, keep the invariant.</pre>
      </div>
    </blockquote>
    <br>
    <blockquote type="cite">
      <div dir="ltr">
        <pre style="white-space:pre-wrap;color:rgb(0,0,0)">I see 3 options here - all I can say is that I don't vote for the first </pre>
        <pre style="white-space:pre-wrap;color:rgb(0,0,0)">+ No transform method on the manifold learners, so no cross-validation
</pre>
      </div>
    </blockquote>
    This is what I thought we usually do. It looks like you said we are
    doing a greedy transform.<br>
    I'm not sure I follow that. In particular for spectral embedding for
    example there is a pretty way to describe<br>
    the transform and that's what we're doing. You could also look at
    doing transductive learning but that's<br>
    not really the standard formulation, is it?<br>
    <br>
    <blockquote type="cite">
      <div dir="ltr">
        <pre style="white-space:pre-wrap;color:rgb(0,0,0)">+ Pointwise, distributable, subset-invariant, suboptimal greedy transform
</pre>
        <pre style="white-space:pre-wrap;color:rgb(0,0,0)">+ Non-distributable, non-subset-invariant, optimal batch transform
</pre>
      </div>
    </blockquote>
    Can you give an example of that?<br>
    <blockquote type="cite">
      <div dir="ltr">
        <pre style="white-space:pre-wrap;color:rgb(0,0,0)">-Charles</pre>
        <pre style="white-space:pre-wrap;color:rgb(0,0,0)">On Mon., Jan. 20, 21:24:52 2020 <<a href="mailto:scikit-learn%40python.org?Subject=Re%3A%20%5Bscikit-learn%5D%20Why%20is%20subset%20invariance%20necessary%20for%0A%20transfom%28%29%3F&In-Reply-To=%3CCAAkaFLWfWyu%2BDdQ3RX5tBays6jLX6A3W_QpqAcWn_RAxbRz5cQ%40mail.gmail.com%3E" title="[scikit-learn] Why is subset invariance necessary for transfom()?" style="font-family:"Times New Roman";font-size:medium" target="_blank">joel.nothman at gmail.com</a>> wrote</pre>
        <pre style="white-space:pre-wrap;color:rgb(0,0,0)">I think allowing subset invariance to not hold is making stronger</pre>
        <pre style="white-space:pre-wrap;color:rgb(0,0,0)">assumptions than we usually do about what it means to have a "test set".
Having a transformation like this that relies on test set statistics
implies that the test set is more than just selected samples, but rather
that a large collection of samples is available at one time, and that it is
in some sense sufficient or complete (no more samples are available that
would give a better fit). So in a predictive modelling context you might
have to set up your cross validation splits with this in mind.

In terms of API, the subset invariance constraint allows us to assume that
the transformation can be distributed or parallelized over samples. I'm not
sure whether we have exploited that assumption within scikit-learn or
whether related projects do so.

I see the benefit of using such transformations in a prediction Pipeline,
and really appreciate this challenge to our assumptions of what "transform"
means.

Joel

On Tue., 21 Jan. 2020, 11:50 am Charles Pehlivanian, <
<a href="https://mail.python.org/mailman/listinfo/scikit-learn" target="_blank">pehlivaniancharles at gmail.com</a>> wrote:

><i> Not all data transformers have a transform method. For those that do,
</i>><i> subset invariance is assumed as expressed
</i>><i> in check_methods_subset_invariance(). It must be the case that
</i>><i> T.transform(X)[i] == T.transform(X[i:i+1]), e.g. This is true for classic
</i>><i> projections - PCA, kernel PCA, etc., but not for some manifold learning
</i>><i> transformers - MDS, SpectralEmbedding, etc. For those, an optimal placement
</i>><i> of the data in space is a constrained optimization, may take into account
</i>><i> the centroid of the dataset etc.
</i>><i>
</i>><i> The manifold learners have "batch" oos transform() methods that aren't
</i>><i> implemented, and wouldn't pass that test. Instead, those that do -
</i>><i> LocallyLinearEmbedding - use a pointwise version, essentially replacing a
</i>><i> batch fit with a suboptimal greedy one [for LocallyLinearEmbedding]:
</i>><i>
</i>><i>     for i in range(X.shape[0]):
</i>><i>         X_new[i] = np.dot(self.embedding_[ind[i]].T, weights[i])
</i>><i>
</i>><i> Where to implement the batch transform() methods for MDS,
</i>><i> SpectralEmbedding, LocallyLinearEmbedding, etc?
</i>><i>
</i>><i> Another verb? Both batch and pointwise versions? The latter is easy to
</i>><i> implement once the batch version exists. Relax the test conditions?
</i>><i> transform() is necessary for oos testing, so necessary for cross
</i>><i> validation. The batch versions should be preferred, although as it stands,
</i>><i> the pointwise versions are.
</i>><i>
</i>><i> Thanks
</i>><i> Charles Pehlivanian
</i>><i> _______________________________________________
</i>><i> scikit-learn mailing list
</i>><i> <a href="https://mail.python.org/mailman/listinfo/scikit-learn" target="_blank">scikit-learn at python.org</a>
</i>><i> <a href="https://mail.python.org/mailman/listinfo/scikit-learn" target="_blank">https://mail.python.org/mailman/listinfo/scikit-learn</a>
</i>><i>
</i>-------------- next part --------------
An HTML attachment was scrubbed...
URL: <<a href="http://mail.python.org/pipermail/scikit-learn/attachments/20200121/b402c42e/attachment.html" target="_blank">http://mail.python.org/pipermail/scikit-learn/attachments/20200121/b402c42e/attachment.html</a>>
</pre>
        <br>
      </div>
      <br>
      <fieldset></fieldset>
      <pre>_______________________________________________
scikit-learn mailing list
<a href="mailto:scikit-learn@python.org" target="_blank">scikit-learn@python.org</a>
<a href="https://mail.python.org/mailman/listinfo/scikit-learn" target="_blank">https://mail.python.org/mailman/listinfo/scikit-learn</a>
</pre>
    </blockquote>
    <br>
  </div>

_______________________________________________<br>
scikit-learn mailing list<br>
<a href="mailto:scikit-learn@python.org" target="_blank">scikit-learn@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/scikit-learn" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/scikit-learn</a><br>
</blockquote></div></div></div>
</blockquote></div>