Find nearest value in numpy array
Is there a numpy-thonic way, e.g. function, to find the nearest value in an array? Example: np.find_nearest( array, value ) 19 Answers 19
Is there a numpy-thonic way, e.g. function, to find the nearest value in an array? Example: np.find_nearest( array, value ) 19 Answers 19
What does np.random.seed do in the below code from a Scikit-Learn tutorial? I’m not very familiar with NumPy’s random state generator stuff, so I’d really appreciate a layman’s terms explanation of this. np.random.seed(0) indices = np.random.permutation(len(iris_X)) 11 Answers 11
I have a dataframe along the lines of the below: Type Set 1 A Z 2 B Z 3 B X 4 C Y I want to add another column to the dataframe (or generate a series) of the same length as the dataframe (equal number of records/rows) which sets a colour ‘green’ if Set … Read more
Can someone explain to me what is the purpose of meshgrid function in Numpy? I know it creates some kind of grid of coordinates for plotting, but I can’t really see the direct benefit of it. I am studying “Python Machine Learning” from Sebastian Raschka, and he is using it for plotting the decision borders. … Read more
How can I sort an array in NumPy by the nth column? For example, a = array([[9, 2, 3], [4, 5, 6], [7, 0, 5]]) I’d like to sort rows by the second column, such that I get back: array([[7, 0, 5], [9, 2, 3], [4, 5, 6]]) 16 Answers 16 To sort by the … Read more
When calling df = pd.read_csv(‘somefile.csv’) I get: /Users/josh/anaconda/envs/py27/lib/python2.7/site-packages/pandas/io/parsers.py:1130: DtypeWarning: Columns (4,5,7,16) have mixed types. Specify dtype option on import or set low_memory=False. Why is the dtype option related to low_memory, and why would making it False help with this problem? 11 Answers 11 The deprecated low_memory option The low_memory option is not properly deprecated, but … Read more
I wonder if there is a direct way to import the contents of a CSV file into a record array, much in the way that R’s read.table(), read.delim(), and read.csv() family imports data to R’s data frame? Or is the best way to use csv.reader() and then apply something like numpy.core.records.fromrecords()? 13 s 13 You … Read more
I want to apply my custom function (it uses an if-else ladder) to these six columns (ERI_Hispanic, ERI_AmerInd_AKNatv, ERI_Asian, ERI_Black_Afr.Amer, ERI_HI_PacIsl, ERI_White) in each row of my dataframe. I’ve tried different methods from other questions but still can’t seem to find the right answer for my problem. The critical piece of this is that if … Read more
index() will give the first occurrence of an item in a list. Is there a neat trick which returns all indices in a list for an element? 17 s 17 You can use a list comprehension with enumerate: indices = [i for i, x in enumerate(my_list) if x == “whatever”] The iterator enumerate(my_list) yields pairs … Read more
What are the advantages of NumPy over regular Python lists? I have approximately 100 financial markets series, and I am going to create a cube array of 100x100x100 = 1 million cells. I will be regressing (3-variable) each x with each y and z, to fill the array with standard errors. I have heard that … Read more