Given a sparse matrix listing, what’s the best way to calculate the cosine similarity between each of the columns (or rows) in the matrix? I would rather not iterate n-choose-two times. Say the input matrix is: The sparse representation is: In Python, it’s straightforward to work with the matrix-i…
cross product in python of arbitrarily many lists
I know you can take the pairwise cross product of lists in the ways posted here: Pairwise crossproduct in Python but I want to take a list L and a positive integer n and return the cross-product of L with itself n times. Is there a built in way to do this or will I just have to iterate the
How can unrar a file with python
How can I extract a .zip or .rar file using Python? Answer Late, but I wasn’t satisfied with any of the answers. Works on Windows and linux without any other libraries needed.
How to handle both `with open(…)` and `sys.stdout` nicely?
Often I need to output data either to file or, if file is not specified, to stdout. I use the following snippet: I would like to rewrite it and handle both targets uniformly. In ideal case it would be: but this will not work well because sys.stdout is be closed when leaving with block and I don’t want t…
Convert range(r) to list of strings of length 2 in python
I just want to change a list (that I make using range(r)) to a list of strings, but if the length of the string is 1, tack a 0 on the front. I know how to turn the list into strings using but I want to be able to also change the length of those strings. Input: Output: And if
The Ignore callback for python shutil.copytree() does not accept full path
I’d like to specify full paths to ignorable files and directories when calling shutil.copytree(). Something like After this, the excluded file is still there unless I return simply the filename instead of full path. The thing is I really want to set up a particular file instead of all matching filenames…
How to use Selenium with Python?
How do I set up Selenium to work with Python? I just want to write/export scripts in Python, and then run them. Are there any resources for that? I tried googling, but the stuff I found was either referring to an outdated version of Selenium (RC), or an outdated version of Python. Answer You mean Selenium Web…
How to add pandas data to an existing csv file?
I want to know if it is possible to use the pandas to_csv() function to add a dataframe to an existing csv file. The csv file has the same structure as the loaded data. Answer You can specify a python write mode in the pandas to_csv function. For append it is ‘a’. In your case: The default mode is…
Convert set to string and vice versa
Set to string. Obvious: String to set? Maybe like this? Extremely ugly. Is there better way to serialize/deserialize sets? Answer Use repr and eval: Note that eval is not safe if the source of string is unknown, prefer ast.literal_eval for safer conversion: help on repr:
Dropping infinite values from dataframes in pandas?
How do I drop nan, inf, and -inf values from a DataFrame without resetting mode.use_inf_as_null? Can I tell dropna to include inf in its definition of missing values so that the following works? Answer First replace() infs with NaN: and then drop NaNs via dropna(): For example: The same method also works for …