Use numpy to generate all possible combinations in a range using numpy in python
Is there a faster way to generate possible combinations without repetitions in numpy? For example, if I needed all 3 number combinations in range(1,5) I will get (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4).I'm using the following code which works but is quite slow for me and am wondering if there's a faster method:
from itertools import *
result=[]
for c in combinations(range(1,48), 6):
result.append(c)
See also questions close to this topic

how to prevent duplicate file being uploaded on the server using checksum and python
how to prevent duplicate file being uploaded on the server using python and checksum. Assume you need to upload large files in terabytes. Which is the most efficient way of doing it?

TFlearn  VocabularyProcessor errors out for given vocabulary
I am using the
VocabularyProcessor
of TFlearn to map documents to integer arrays. However, I don't seem to be able to initialize theVocabularyProcessor
with my own vocabulary. In the docs it says that I can provide a vocabulary when creating theVocabularyProcessor
as in:vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length, vocabulary=vocab) However, when creating the VocabularyProcessor like this I cannot transform my documents correctly. I am providing the vocabulary as a dictionary, using the word indices as values: vocab={'<UNK>': 0, 'hello':3, 'here':5, 'world':20} sentences= ['hello summer ', 'summer is here'] vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length, vocabulary=vocab) np.array(list(vocab_processor.transform(sentences)))
I get the error below:
Traceback (most recent call last): File "featurespredict.py", line 74, in x_test = np.array(list(vocab_processor.transform(data_text))) File "/usr/local/lib/python3.5/distpackages/tensorflow/contrib/learn/python/learn/preprocessing/text.py", line 188, in transform word_ids[idx] = self.vocabulary_.get(token) TypeError: int() argument must be a string, a byteslike object or a number, not 'NoneType'
Instead shouldn't it be:
[3 0 0] [0 0 5]

Setting meta data tags with piexif
I try to set specific meta data for jpegs with the piexif module. I get the respective dicts out of the piexif.load().
data = piexif.load()
.They return {'GPS': {}, 'Exif': {}, 'Interop': {}, 'thumbnail': None, '1st': {}, '0th': {}}. (Maybe the answer is very obviouse but I am a little confused to the dicts)
However, I would like to know where and what to write to set my focus length, the camera maker and model.
The reason for that, I want to use the Regard3D reconstruction GUI from http://www.regard3d.org/index.php/documentation/details/pictureset.
Therefore, I need to add the meta to the jpegs and the data of the camera into the camera db. This is needed for the triangulation step.
Thank you very much in advance

Fastest way to generate 2D Gaussian
I am trying to generate an asymmetric 2D Gaussian with dim 7x7, but I want to do it as fast as possible. I have tried 4 different ways:
x=np.arange(7)[None].astype(np.float); y=x.T xx,yy=np.meshgrid(x,y) mx,my,sx,sy=3.0,4.0,1.0,2.0 #centre and width params gauss=np.exp((xxmx)**2/sx(yymy)**2/sy) #full grid gauss=np.exp((xmx)**2/sx(ymy)**2/sy) #x and y vectors gauss=np.exp((ymy)**2/sy)*(np.exp((xmx)**2/sx)) #multiplication gauss=np.exp((ymy)**2/sy).dot(np.exp((xmx)**2/sx)) #dot product
Benchmarks on my computer with %timeit:
#full grid: 13.4 us #x,y vectors: 16.5 us #multiplication: 17.7 us #dot prod: 16.5
I am surprised the fastest approach is the one using the full grid. Also, generating this full grid is not an issue since it will be reused for many computations. Is there any faster way to compute
gauss
? 
Pandas DF  Measure frequency, attached to appropriate rows and normalize by max(freq)
Assuming I create the following data frame:
df = pd.DataFrame({'A':np.random.random(20), 'B':np.random.random(20)}) df Out[162]: A B 0 0.888651 0.380360 1 0.513343 0.605991 2 0.560978 0.076174 3 0.209426 0.498564 4 0.121748 0.771653 5 0.843299 0.279264 6 0.644060 0.725061 7 0.200187 0.349093 8 0.807808 0.657373 9 0.212760 0.384311 10 0.000725 0.023815 11 0.614540 0.534569 12 0.083690 0.228761 13 0.202334 0.266114 14 0.104520 0.757514 15 0.039944 0.014512 16 0.465300 0.164657 17 0.247370 0.894628 18 0.980589 0.833938 19 0.734673 0.745574
I then, want to:
 Know the frequency of the 'B' columns which falls in the bins:
np.arange(0, 1.05, 0.05)
 Add that information as column 'freq'. So, for example,
row[0]
where'B'
is 0.38 and is between[0.35, 0.40)
is happening 2 times in the data frame. Therefor we will havedf['freq'][0] = 2
 Then, I want to have a new column called
'weights'
which will be, for each row, max(freq)/freq
I can solve 1 with something like:
df.groupby(pd.cut(df['B'], np.arange(0, 1.05, 0.05))).count()
though there might be a more elegant way to do soI fail to solve 2
And 3 is pretty straight forward.
Eventually, I only need the 'weights' column that is created by 1, 2 and 3.
 Know the frequency of the 'B' columns which falls in the bins:

I want to calculate theoretical value of 2D array
I want to calculate theoretical value of 2D array.I have a 2D array like
arr = [[1,3,4],[5,7,9],[8,1,7]]
So this 2D array's theoretical array is [5,3,7] I tried to get the array by the code
theory = np.median(arr)
but when I print out theory, only 4.67 is returned.I read numpy document ,median method can be gotten array. What is wrong in my code?How should I fix this?

Generating Kmultisets when order matters and repetition is allowed
Sorry if my question is basic but I have not coded in the past 15 years but I am trying to learn to code again for a research project. I have a set of 12 objects [A B C D E F G H I J K L] and I want to create a list of any possible Kmultiset for any K between 1 and 6. (I do have a list of selection probabilities for them but at this stage of the project I can assume an equal probability of selection for all). Order matters and repetition is allowed (so they are just ktuples). For example: [A], [A A] [B A A] [A B A], [B A A A A A] etc. I tried to use
weightednofwithrepeats
in theRND
extension but it seems that order does not matter in it so [B A] and [A B] are the same thing and are reported as [A B]. Can you please help? 
Generate matrix of unique combination using 2 variables from a data frame r
I have a data frame as df< as.data.frame(expand.grid(0:1, 0:4, 0:3,0:7, 2:7)) I want to get all unique combinations using 2 variables of the given 5 variables in the data frame df

How to iterate a YAML file to give all possible combinations from items in different lists in PYTHON
I have a YAML document with sequences like this
 One:  a  b  c Two:  d  e Three:  f  g  h  i
I need to get all possible combinations of the elements taken from each list one at a time, only one element at every instance from the list and all list must be used.
I need to do this is python.
Until now, I can print the YAML file using:
#!/usr/bin/env python import yaml with open("parameters.yaml", 'r') as stream: try: print(yaml.load(stream)) except yaml.YAMLError as exc: print(exc)