All Questions

Tagged with
Filter by
Sorted by
Tagged with
568 votes
7 answers
249k views

What are the advantages of NumPy over regular Python lists?

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 ...
Thomas Browne's user avatar
434 votes
10 answers
1.2m views

Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

I have a Numpy array consisting of a list of lists, representing a two-dimensional array with row labels and column names as shown below: data = np.array([['','Col1','Col2'],['Row1',1,2],['Row2',3,4]])...
user3132783's user avatar
  • 5,405
423 votes
6 answers
631k views

Convert NumPy array to Python list

How do I convert a NumPy array into a Python List?
Alex Brooks's user avatar
  • 5,233
265 votes
9 answers
452k views

List of lists into numpy array

How do I convert a simple list of lists into a numpy array? The rows are individual sublists and each row contains the elements in the sublist.
Richa Sachdev's user avatar
243 votes
1 answer
96k views

What is the inverse function of zip in python? [duplicate]

I've used the zip function from the Numpy library to sort tuples and now I have a list containing all the tuples. I had since modified that list and now I would like to restore the tuples so I can use ...
user17151's user avatar
  • 2,687
211 votes
7 answers
181k views

Filtering a list based on a list of booleans

I have a list of values which I need to filter given the values in a list of booleans: list_a = [1, 2, 4, 6] filter = [True, False, True, False] I generate a new filtered list with the following ...
Gabriel's user avatar
  • 41.7k
204 votes
5 answers
325k views

How to convert list of numpy arrays into single numpy array?

Suppose I have ; LIST = [[array([1, 2, 3, 4, 5]), array([1, 2, 3, 4, 5],[1,2,3,4,5])] # inner lists are numpy arrays I try to convert; array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, ...
erogol's user avatar
  • 13.4k
200 votes
12 answers
337k views

Index all *except* one item in python

Is there a simple way to index all elements of a list (or array, or whatever) except for a particular index? E.g., mylist[3] will return the item in position 3 milist[~3] will return the whole list ...
choldgraf's user avatar
  • 3,619
135 votes
9 answers
297k views

How to save a list as numpy array in python?

Is possible to construct a NumPy array from a python list?
Hossein's user avatar
  • 40.9k
120 votes
1 answer
133k views

Convert 2d numpy array into list of lists [duplicate]

I use an external module (libsvm), which does not support numpy arrays, only tuples, lists and dicts. But my data is in a 2d numpy array. How can I convert it the pythonic way, aka without loops. &...
Framester's user avatar
  • 34.3k
109 votes
6 answers
644k views

List to array conversion to use ravel() function

I have a list in python and I want to convert it to an array to be able to use ravel() function.
user2229953's user avatar
  • 1,559
86 votes
4 answers
196k views

How to use python numpy.savetxt to write strings and float number to an ASCII file?

I have a set of lists that contain both strings and float numbers, such as: import numpy as num NAMES = num.array(['NAME_1', 'NAME_2', 'NAME_3']) FLOATS = num.array([ 0.5 , 0.2 , 0.3 ]) ...
Victor's user avatar
  • 1,034
81 votes
5 answers
168k views

python pandas flatten a dataframe to a list

I have a df like so: import pandas a=[['1/2/2014', 'a', '6', 'z1'], ['1/2/2014', 'a', '3', 'z1'], ['1/3/2014', 'c', '1', 'x3'], ] df = pandas.DataFrame.from_records(a[1:],columns=a[0]) I ...
jason's user avatar
  • 4,093
81 votes
3 answers
63k views

Python Array Slice With Comma?

I was wondering what the use of the comma was when slicing Python arrays - I have an example that appears to work, but the line that looks weird to me is p = 20*numpy.log10(numpy.abs(numpy.fft.rfft(...
SolarLune's user avatar
  • 1,229
77 votes
7 answers
476k views

'list' object has no attribute 'shape'

how to create an array to numpy array? def test(X, N): [n,T] = X.shape print "n : ", n print "T : ", T if __name__=="__main__": X = [[[-9.035250067710876], [7.453250169754028], [33....
sam's user avatar
  • 19k
71 votes
10 answers
136k views

How to create a numpy array of lists?

I want to create a numpy array in which each element must be a list, so later I can append new elements to each. I have looked on google and here on stack overflow already, yet it seems nowhere to be ...
Ricardo Silveira's user avatar
64 votes
6 answers
83k views

How to check if a variable is either a python list, numpy array or pandas series

I have a function that takes in a variable that would work if it is any of the following three types 1. pandas Series 2. numpy array (ndarray) 3. python list Any other type should be rejected. ...
Zhang18's user avatar
  • 4,890
63 votes
10 answers
133k views

numpy-equivalent of list.pop?

Is there a numpy method which is equivalent to the builtin pop for python lists? Popping obviously doesn't work on numpy arrays, and I want to avoid a list conversion.
Anton Alice's user avatar
59 votes
3 answers
117k views

Python List of np arrays to array

I'm trying to turn a list of 2d numpy arrays into a 2d numpy array. For example, dat_list = [] for i in range(10): dat_list.append(np.zeros([5, 10])) What I would like to get out of this list ...
mike's user avatar
  • 23.3k
55 votes
2 answers
121k views

How to access a column in a list of lists in python

I have a 2D array in python modeled by a list of lists and I want to extract the column. I made a quick research and I found a way that uses numpy arrays. The problem is that I do not want to use ...
Oussama Boussif's user avatar
51 votes
5 answers
222k views

Convert list or numpy array of single element to float in python

I have a function which can accept either a list or a numpy array. In either case, the list/array has a single element (always). I just need to return a float. So, e.g., I could receive: list_ = [4]...
Jesper - jtk.eth's user avatar
48 votes
6 answers
6k views

Is there a difference between `board[x, y]` and `board[x][y]` in Python?

I'm working through a tutorial on GeekforGeeks website and noticed that they are checking a point in an array using board[x,y], which I've never seen before. I don't think this would work, but when I ...
Broski-AC's user avatar
  • 769
44 votes
2 answers
82k views

concatenate numpy arrays which are elements of a list

I have a list containing numpy arrays something like L=[a,b,c] where a, b and c are numpy arrays with sizes N_a in T, N_b in T and N_c in T. I want to row-wise concatenate a, b and c and get a numpy ...
TNM's user avatar
  • 1,411
43 votes
9 answers
79k views

Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, (<class 'list'> containing values of types {"<class 'int'>"})

history = model.fit(X, y, batch_size=32, epochs=40, validation_split=0.1) the line problem was this Showing error: ValueError: Failed to find data adapter that can handle input: <class 'numpy....
Neo's user avatar
  • 431
42 votes
10 answers
67k views

How to make List from Numpy Matrix in Python [duplicate]

I using the dot() function from numpy to multiply a matrix of 3x3 with a numpy.array of 1x3. The output is for example this: [[ 0.16666667 0.66666667 0.16666667]] which is of type: <class '...
Javaaaa's user avatar
  • 3,808
38 votes
2 answers
50k views

Merge two arrays vertically to array of tuples using numpy

I have two numpy arrays: x = np.array([-1, 0, 1, 2]) y = np.array([-2, -1, 0, 1]) Is there a way to merge these arrays together like tuples: array = [(-1, -2), (0, -1), (1, 0), (2, 1)]
Steven's user avatar
  • 1,143
38 votes
1 answer
59k views

AttributeError: 'numpy.ndarray' object has no attribute 'toList'

I'm trying to append certain columns of Pandas Data Frames from CSV files into a numpy array. I have no idea how to instantiate an empty numpy array, so I'm testing it first with a list. def windows(...
user avatar
37 votes
6 answers
67k views

How to delete an object from a numpy array without knowing the index

Is it possible to delete an object from a numpy array without knowing the index of the object but instead knowing the object itself? I have seen that it is possible using the index of the object using ...
MhmdMnsr's user avatar
  • 483
34 votes
2 answers
12k views

Numpy individual element access slower than for lists

I just started using Numpy and noticed that iterating through each element in a Numpy array is ~4x slower than doing the same but with a list of lists. I know now that this defeats the purpose of ...
emschorsch's user avatar
  • 1,659
30 votes
1 answer
65k views

TypeError: only integer arrays with one element can be converted to an index 3

I'm having this error in the title, and don't know what's wrong. It's working when I use np.hstack instead of np.append, but I would like to make this faster, so use append. time_list a list of ...
GeoffreyB's user avatar
  • 526
25 votes
1 answer
41k views

How to create a dynamic array

As I understand, the list type in Python is a dynamic pointer array, which will increase it's capacity when items are appended to it. And an array in NumPy uses a continuous memory area to hold all ...
HYRY's user avatar
  • 96k
22 votes
4 answers
10k views

Difference between list(numpy_array) and numpy_array.tolist()

What is the difference between applying list() on a numpy array vs. calling tolist()? I was checking the types of both outputs and they both show that what I'm getting as a result is a list, however,...
atoregozh's user avatar
  • 425
21 votes
4 answers
43k views

Grab unique tuples in python list, irrespective of order

I have a python list: [ (2,2),(2,3),(1,4),(2,2), etc...] What I need is some kind of function that reduces it to its unique components... which would be, in the above list: [ (2,2),(2,3),(1,4) ] ...
Chris's user avatar
  • 30k
20 votes
3 answers
26k views

How to return back a list instead of tuple in psycopg2

I have the following query cursor.execute( """ SELECT transform(row_to_json(t)) FROM (select * from table where a = %s and b = %s limit 1000) t; """ , ...
Ouwen Huang's user avatar
  • 1,067
19 votes
6 answers
6k views

Finding the index of a numpy array in a list

import numpy as np foo = [1, "hello", np.array([[1,2,3]]) ] I would expect foo.index( np.array([[1,2,3]]) ) to return 2 but instead I get ValueError: The truth value of an array with more ...
Lee88's user avatar
  • 1,255
18 votes
1 answer
10k views

Numpy: Difference between a[i][j] and a[i,j]

Coming from a Lists background in Python and that of programming languages like C++/Java, one is used to the notation of extracting elements using a[i][j] approach. But in NumPy, one usually does a[i,...
Nipun Batra's user avatar
  • 11.2k
17 votes
10 answers
4k views

Python function that identifies if the numbers in a list or array are closer to 0 or 1

I have a numpy array of numbers. Below is an example: [[-2.10044520e-04 1.72314372e-04 1.77235336e-04 -1.06613465e-04 6.76617611e-07 2.71623057e-03 -3.32789944e-05 1.44899758e-05 5.79249863e-05 ...
alyssaeliyah's user avatar
  • 2,244
17 votes
11 answers
23k views

return index of last non-zero element in list

I get data in the following format: [-2, -2, 0, 0, 0, 0, 0] [-2, 20, -1, 0, 3, 0, 0] with each line being a different input. The lists could be longer than 7 elements. I need to return the index ...
Vincent's user avatar
  • 2,739
17 votes
6 answers
2k views

Get second minimum values per column in 2D array

How can I get the second minimum value from each column? I have this array: A = [[72 76 44 62 81 31] [54 36 82 71 40 45] [63 59 84 36 34 51] [58 53 59 22 77 64] [35 77 60 76 57 44]...
Mr Dan's user avatar
  • 221
17 votes
2 answers
9k views

Python Slice Notation with Comma/List

I have come across some python code with slice notation that I am having trouble figuring out. It looks like slice notation but uses a comma and a list: list[:, [1, 2, 3]] Is this syntax valid? If ...
Stephan's user avatar
  • 17.3k
17 votes
3 answers
995 views

Why can itertools.groupby group the NaNs in lists but not in numpy arrays

I'm having a difficult time to debug a problem in which the float nan in a list and nan in a numpy.array are handled differently when these are used in itertools.groupby: Given the following list and ...
MSeifert's user avatar
  • 149k
17 votes
4 answers
10k views

Comparing two large lists in python

I have one list which contains about 400 words. And another list of lists, in which each list contains about 150,000 words. This list has 20 such lists. Now I want to see how many of these 400 words ...
avi's user avatar
  • 9,506
16 votes
4 answers
15k views

bitwise operations between elements in a list

I have a of list of bitwise elements, e.g. [1,1,1], and I want to do a bitwise OR operation between every element in the list. So, e.g. for [1,1,1] do 1 | 1 | 1 = 1 or for [1,17,1] do 1 | 17 ...
havok2063's user avatar
  • 557
16 votes
2 answers
1k views

numpy.unique gives wrong output for list of sets

I have a list of sets given by, sets1 = [{1},{2},{1}] When I find the unique elements in this list using numpy's unique, I get np.unique(sets1) Out[18]: array([{1}, {2}, {1}], dtype=object) As can ...
rashid's user avatar
  • 664
15 votes
2 answers
3k views

Find nearest indices for one array against all values in another array - Python / NumPy

I have a list of complex numbers for which I want to find the closest value in another list of complex numbers. My current approach with numpy: import numpy as np refArray = np.random.random(16); ...
Alexander's user avatar
  • 333
15 votes
3 answers
18k views

Fastest way to check if duplicates exist in a python list / numpy ndarray

I want to determine whether or not my list (actually a numpy.ndarray) contains duplicates in the fastest possible execution time. Note that I don't care about removing the duplicates, I simply want to ...
Scott Gigante's user avatar
14 votes
2 answers
15k views

Deleting masked elements in numpy array

I have some arrays that contain masked elements (from Numpy.MaskedArray), e.g. data = [0,1,masked,3,masked,5,...] Where the mask doesn't follow a regular pattern. I want to iterate through the ...
rh1990's user avatar
  • 920
14 votes
2 answers
12k views

Finding the indices of the top three values via argmin() or min() in python/numpy without mutation of list?

So I have this list called sumErrors that's 16000 rows and 1 column, and this list is already presorted into 5 different clusters. And what I'm doing is slicing the list for each cluster and finding ...
Shinjitsu's user avatar
  • 639
14 votes
1 answer
18k views

What is the difference between a NumPy array and a python list? [duplicate]

Why do we use numpy arrays in place of lists in python? What is the main difference between them?
Mobi Zaman's user avatar
13 votes
7 answers
24k views

Python numpy array vs list

I need to perform some calculations a large list of numbers. Do array.array or numpy.array offer significant performance boost over typical arrays? I don't have to do complicated manipulations on ...
math4tots's user avatar
  • 8,742

1
2 3 4 5
48