This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.datasets import load_iris | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.utils import shuffle | |
# get shuffled iris data | |
X, y = load_iris(return_X_y=True) | |
X, y = shuffle(X, y, random_state=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
import numpy as np | |
from sklearn.metrics import euclidean_distances | |
from pyemd import emd as pyemd | |
def word_movers_distance(a, b, embeddings): | |
"""Word Mover's Distance. | |
A measure of text similarity: earth mover's distance in embedding metric space. |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Reason for publishing this Gist and the use case: | |
In many cases we use functions in libraries that print the variable we want instead of returning it. This Gist shows | |
how to get hold of the printed values from these functions and store in a variable. I found the solution in | |
couple of posts on different forums: | |
* https://stackoverflow.com/questions/1218933/can-i-redirect-the-stdout-in-python-into-some-sort-of-string-buffer | |
* https://wrongsideofmemphis.wordpress.com/2010/03/01/store-standard-output-on-a-variable-in-python/ | |
* https://groups.google.com/forum/#!topic/comp.lang.python/tkK6n1oVKhM | |
* https://bytes.com/topic/python/answers/849106-redirection-standard-output-python-command-python-variable | |
In addition to the trick demonstrated in these posts, I found a case where I would need to store multiple printouts |
NewerOlder