Not a duplicate  How can I search for first occurrence of a number less than threshold in a 1D numpy array?
This question was incorrectly marked as a duplicate.
I have an n x 1 numpy array. I want to find the first occurrence of an entry in the array that is less than a threshold.
my code is as follows:
import numpy as np
aa = np.array([4,3,5,7])
print(aa)
np.argmin(aa<3)
output:
[ 4 3 5 7]
0
I expect argmin to return 2 but I'm getting 0. How can I make this work?
See also questions close to this topic

read and split information from text file python
I'm stuck on a problem:
I have a text file called id_numbers.txt that contains this information:
325255, Jan Jansen
334343, Erik Materus
235434, Ali Ahson
645345, Eva Versteeg
534545, Jan de Wilde
345355, Henk de Vries
I need python to split the information at the comma and write a program that will the display the information as follows:
Jan Jansen has cardnumber: 325255
Erik Materus has cardnumber: 334343
Ali Ahson has cardnumber: 235434
Eva Versteeg has cardnumber: 645345
I've tried to convert to list and split(",") but that ends up adding the next number like this:
['325255', ' Jan Jansen\n334343', ' Erik Materus\n235434', ' Ali Ahson\n645345', ' Eva Versteeg\n534545', ' Jan de Wilde\n345355', ' Henk de Vries']
Help would be appreciated!

Excel library to use
I'm quite new to programming, I've just started an internship as a programmer and have been learning since then. My team uses PHPExcel library and they are thinking about swaping to another one, in python, so probably openpyxl.
I have been doing some research but could not find any information about which one has a better potential, only that the python one was developed based on PHPExcel, can anyone share information on this?
I know this might not be the right place to ask but I could not find anywhere else.

python break not working(nested for loop)
I don't know why is the break statement still continue to execute the outer for loop
x=0 password="" password=input("enter your password") crack="" c=0 for a in range(9,127): print("length",a) for a1 in range(9,127): for a2 in range(9,127): for a2 in range(9,127): if password==crack: print("password",crack) crack=chr(a)+chr(a1)+chr(a2) print("password",crack)

Not enough values to unpack python and opencv
I'm new to python and opencv. I tried this motion detection code and it worked fine.
import numpy as np import cv2 from os import walk import os sdThresh = 10 font = cv2.FONT_HERSHEY_SIMPLEX def distMap(frame1, frame2): """outputs pythagorean distance between two frames""" frame1_32 = np.float32(frame1) frame2_32 = np.float32(frame2) diff32 = frame1_32  frame2_32 norm32 = np.sqrt(diff32[:,:,0]**2 + diff32[:,:,1]**2 + diff32[:,:,2]**2)/np.sqrt(255**2 + 255**2 + 255**2) dist = np.uint8(norm32*255) return dist cv2.namedWindow('frame') cv2.namedWindow('dist') #capture video stream from camera source. 0 refers to first camera, 1 referes to 2nd and so on. loc = os.path.abspath('') cap = cv2.VideoCapture(loc+'/inputs/video.avi') _, frame1 = cap.read() _, frame2 = cap.read() while(True): _, frame3 = cap.read() rows, cols, _ = np.shape(frame3) cv2.imshow('dist', frame3) dist = distMap(frame1, frame2) frame1 = frame2 frame2 = frame3 # apply Gaussian smoothing mod = cv2.GaussianBlur(dist, (15,15), 0) # apply thresholding _, thresh = cv2.threshold(mod, 100, 200, 0) # calculate st dev test _, stDev = cv2.meanStdDev(mod) cv2.imshow('dist', mod) cv2.putText(frame2, "Standard Deviation  {}".format(round(stDev[0][0],0)), (70, 70), font, 1, (255, 0, 255), 1, cv2.LINE_AA) if stDev > sdThresh: print("Motion detected.."); else: print("No motion Detected"); cv2.imshow('frame', frame2) if cv2.waitKey(1) & 0xFF == 27: break cap.release() cv2.destroyAllWindows()
But when i tried to modify it by putting it inside of a function it is giving me an error saying not enough values to unpack and in my editor variables cols, rows and thresh are indicated as unused. I cannot figure out what exactly is wrong with it. A help will be kindly appreciated
import numpy as np import cv2 from os import walk import os sdThresh = 10 font = cv2.FONT_HERSHEY_SIMPLEX def distMap(frame1, frame2): """outputs pythagorean distance between two frames""" frame1_32 = np.float32(frame1) frame2_32 = np.float32(frame2) diff32 = frame1_32  frame2_32 norm32 = np.sqrt(diff32[:,:,0]**2 + diff32[:,:,1]**2 + diff32[:,:,2]**2)/np.sqrt(255**2 + 255**2 + 255**2) dist = np.uint8(norm32*255) return dist def MotionDetect(): cv2.namedWindow('frame') cv2.namedWindow('dist') loc = os.path.abspath('') cap = cv2.VideoCapture(loc+'/inputs/video.avi') _, frame1 = cap.read() _, frame2 = cap.read() while(True): _, frame3 = cap.read() rows, cols, _ = np.shape(frame3) cv2.imshow('dist', frame3) dist = distMap(frame1, frame2) frame1 = frame2 frame2 = frame3 # apply Gaussian smoothing mod = cv2.GaussianBlur(dist, (15,15), 0) # apply thresholding _, thresh = cv2.threshold(mod, 100, 200, 0) # calculate st dev test _, stDev = cv2.meanStdDev(mod) cv2.imshow('dist', mod) cv2.putText(frame2, "Standard Deviation  {}".format(round(stDev[0][0],0)), (70, 70), font, 1, (255, 0, 255), 1, cv2.LINE_AA) if stDev > sdThresh: print("Motion detected..") else: print("No motion Detected") cv2.imshow('frame', frame2) if cv2.waitKey(1) & 0xFF == 27: break cap.release() cv2.destroyAllWindows() return MotionDetect()

numpy.minimum for complex numbers
numpy.minimum does not seem to work for complex numbers:
np.minimum(5+3*1j,4+30*1j) (4+30j)
I want to keep the value with the maximum magnitude. It only compares the real part. Any other function for elementwise minimum comparison? MATLAB min does work with complex numbers. Thanks

Map python nested list into multiple sublists
I have a python list
[['a'],['b','c'],['c','d']]
.How can I map this array into multiple arrays ?
['a', 'b', 'c'] ['a', 'c', 'c'] ['a', 'b', 'd'] ['a', 'c', 'd']
I tried to do it as matrix multiplication, but I cannot do multiple in numpy for string type.

Scipy: Use UnivariateSpline to fit data tightly
I have a bunch of x, y points that represent a sigmoidal function:
x=[ 1.00094909 1.08787635 1.17481363 1.2617564 1.34867881 1.43562284 1.52259341 1.609522 1.69631283 1.78276102 1.86426648 1.92896789 1.9464453 1.94941586 2.00062852 2.073691 2.14982808 2.22808316 2.30634034 2.38456905 2.46280126 2.54106611 2.6193345 2.69748825] y=[0.10057627 0.10172142 0.10320428 0.10378959 0.10348456 0.10312503 0.10276956 0.10170055 0.09778279 0.08608644 0.05797392 0.00063599 0.08732999 0.16429878 0.2223306 0.25368884 0.26830932 0.27313931 0.27308756 0.27048902 0.26626313 0.26139534 0.25634544 0.2509893 ]
I use
scipy.interpolate.UnivariateSpline()
to fit to some cubic spline as follows:from scipy.interpolate import UnivariateSpline s = UnivariateSpline(x, y, k=3, s=0) xfit = np.linspace(x.min(), x.max(), 200) plt.scatter(x,y) plt.plot(xfit, s(xfit)) plt.show()
Note that I use
s=0
, which should make my spline adhere completely to my data. And yet, it does not do so. Using a higherk
value leads to even more wiggles.So my questions are 
 How should I correctly use
scipy.interpolate.UnivariateSpline()
to fit my data?  Is this even the correct choice for this kind of a sigmoidal function? Should I be using something like
scipy.optimize.curve_fit()
with a trialtanh(x)
function instead?
 How should I correctly use
 Stretching an image with lastly available pixels(background)

Scipy minimization for a caffe neural network gives the same initial condition as the result
I'm applying a set of thresholds to the weights of a caffe neural network and get the accuracy.But this accuracy is less than the conventional model without the threshold.
What I tried was define a function that takes in the threshold as a parameter and in there apply it to the neural network and return the reciprocal of the accuracy.
Then I used scipt.optimization.minimize with the method 'SLSQP' to tune this threshold such that it gives the maximum accuracy.
import numpy as np import caffe from scipy.optimize import minimize from kernel_apprx import * import time def objective(ths): #return ths[0]**2+ths[1]**2 net=caffe.Net('cifar10_quick_customised_opt.prototxt','cifar10_quick_iter_4000.caffemodel',caffe.TEST) k_aprox(net,"conv1","conv1_cus",ths[0],ths[1]) net.forward() acc=net.blobs["accuracy"].data*100 print "################## report #################\n\tthresholds: %s\n\taccuracy: %.16f%%"%(ths,acc) return 1/float(acc) con={'type':'ineq','fun':con1} constr=[con] th0=[0.01,0.98] b1=(0.0001,1.0) bnds=(b1,b1) tic=time.time() sol=minimize(objective,th0,method='SLSQP',bounds=bnds,options={'disp': True ,'eps' : 1e4,'ftol': 1e10})
Here 'objective' is the function containing the neural network. But when I run it, it runs for about 3 iterations and gives the same initial condition as the result no matter what I put in the initial condition saying 'Optimization terminated successfully.'.In the above example it returns [0.2,0.98]
please help.
thank you.

Efficiency problem of customizing numpy's vectorized operation
I have a python function given below:
def myfun(x): if x > 0: return 0 else: return np.exp(x)
where
np
is thenumpy
library. I want to make the function vectorized in numpy, so I use:vec_myfun = np.vectorize(myfun)
I did a test to evaluate the efficiency. First I generate a vector of 100 random numbers:
x = np.random.randn(100)
Then I run the following code to obtain the runtime:
%timeit np.exp(x) %timeit vec_myfun(x)
The runtime for
np.exp(x)
is1.07 µs ± 24.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
.The runtime for
vec_myfun(x)
is71.2 µs ± 1.68 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
My question is: compared to
np.exp
,vec_myfun
has only one extra step to check the value of $x$, but it runs much slowly thannp.exp
. Is there an efficient way to vectorizemyfun
to make it as efficient asnp.exp
? 
Vectorized Python code for iterating and changing each column of a Pandas DataFrame within a window
I have a dataframe of ones and zeros. I iterate over each column with a loop. If I get a one at an iteration, I should keep it in the column. But if in the next
n
positions after this one there are some ones, I should turn them into zeros. Then repeat the same up to the end of the column, and then repeat all this on each column.Is it possible to get rid of the loop and vectorize everything with dataframe/matrix/array operations in pandas/numpy? And how should I go about it?
n
could be anywhere from 2 to 100.I tried this function, but failed, it only keeps ones if there are at least
n
zeros between them which is obviously not what I need:def clear_window(df, n): # create buffer of size n pad = pd.DataFrame(np.zeros([n, df.shape[1]]), columns=df.columns) padded_df = pd.concat([pad, df]) # compute rolling sum and cut off the buffer roll = (padded_df .rolling(n+1) .sum() .iloc[n:, :] ) # delete ones where rolling sum is above 1 or below 1 result = df * ((roll == 1.0)  (roll == 1.0)).astype(int) return result

Get symbolicated statements in GCC's vectorization report
I'm using the following flags to get a vectorization report from
g++
during compilation:O3 march=native mtune=native g foptinfovecall=<filename>
The report has helped me speed up many places in my code but now I'm stuck trying to find statements GCC thinks may be pointeraliased.
Messages in the generated file often look like this:
my_file.h:78:35: note: versioning for alias required: can't determine dependence between MEM[(const struct lookup_table *)this_61(D) + 512B].x0_ and MEM[(double &)_219] 93709 consider runtime aliasing test between MEM[(const struct lookup_table *)this_61(D) + 512B].x0_ and MEM[(double &)_219] ... my_file.h:78:35: note: bad data dependence.
While I know what
lookup_table::x0_
is, I'm struggling to find_219
in my code. It would help a lot if there was a way to demangle (or symbolicate) names like this. Or find the scope, in which GCC can't assert independence of both memory locations.Is this possible?