numpy-ndarray

What does -1 mean in numpy reshape?

泄露秘密 提交于 2019-12-17 04:09:53
问题 A numpy matrix can be reshaped into a vector using reshape function with parameter -1. But I don't know what -1 means here. For example: a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]]) b = numpy.reshape(a, -1) The result of b is: matrix([[1, 2, 3, 4, 5, 6, 7, 8]]) Does anyone know what -1 means here? And it seems python assign -1 several meanings, such as: array[-1] means the last element. Can you give an explanation? 回答1: The criterion to satisfy for providing the new shape is that 'The new

What does -1 mean in numpy reshape?

空扰寡人 提交于 2019-12-17 04:09:17
问题 A numpy matrix can be reshaped into a vector using reshape function with parameter -1. But I don't know what -1 means here. For example: a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]]) b = numpy.reshape(a, -1) The result of b is: matrix([[1, 2, 3, 4, 5, 6, 7, 8]]) Does anyone know what -1 means here? And it seems python assign -1 several meanings, such as: array[-1] means the last element. Can you give an explanation? 回答1: The criterion to satisfy for providing the new shape is that 'The new

How to read specific rows/columns of a .CSV file and storing them as a numpy matrix?

元气小坏坏 提交于 2019-12-13 20:42:26
问题 I have a .CSV file with contents like this: DATE OPEN HIGH LOW CLOSE PRICE YCLOSE VOL TICKS 13950309 1000000 1000000 1000000 1000000 1000000 1000000 2100000 74 13950326 1050000 1050010 1050000 1050001 1050000 1000000 1648 5 13950329 1030200 1060000 1030200 1044474 1042265 1050001 28469 108 13950330 1040001 1049999 1040001 1042303 1045001 1044474 6518 10 13950331 1049800 1050000 1048600 1048787 1050000 1042303 277 11 13950401 1059973 1059974 1052000 1053807 1055000 1048787 916 17 13950402

What is the need of ellipsis[…] while modifying array values in numpy?

回眸只為那壹抹淺笑 提交于 2019-12-13 19:43:19
问题 import numpy as np a = np.arange(0,60,5) a = a.reshape(3,4) for x in np.nditer(a, op_flags = ['readwrite']): x[...] = 2*x print 'Modified array is:' print a In the above code, why can't we simply write x=2*x instead of x[...]=2*x? 回答1: No matter what kind of object we were iterating over or how that object was implemented, it would be almost impossible for x = 2*x to do anything useful to that object. x = 2*x is an assignment to the variable x ; even if the previous contents of the x variable

When is the size of an ndarray not fixed?

烈酒焚心 提交于 2019-12-13 14:23:27
问题 The numpy.ndarray documentation states that: An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. I'm surprised by the adjective usually here. I thought an ndarray is always of a fixed size. When is the size of an ndarray not fixed? 回答1: You can change the size of an ndarray, using ndarray.resize. I haven't used it extensively, so I can't speak to advantages or disadvantages. However, it seems pretty simple >>> a = ones(3) >>> a.resize(1) >>> a

numba\jit doesn't allow the use of np argsort in nopython mode

£可爱£侵袭症+ 提交于 2019-12-13 07:48:55
问题 Receiving this error message: Failed at nopython (nopython frontend) [1m[1m[1mInvalid usage of Function(<function argsort at 0x0000000002A67840>) with parameters (array(float64, 2d, C), axis=int64) * parameterized In definition 0: While using this code def rankbids(bids, shifts, groupPeriod, period): rowsSize = bids.shape[0]; finaltable = np.zeros((rowsSize, groupPeriod), dtype=np.float64) for i in range(0, period): #for 0 to 99 #CONSTANT 4 UPDATE WHEN NEEDED for worker in range(rowsSize):

How to save subarray in npy file?

依然范特西╮ 提交于 2019-12-13 03:50:48
问题 My data tracks has following shape : (13044,) Its data types are tracks.dtype.names ('frame_num','mean_x','mean_y','var_x','var_y', 'length', 'scale', 'x_pos','y_pos', 't_pos', 'coords', 'trajectory', 'hog', 'hof', 'mbh_x','mbh_y') dtype([('frame_num', '<i4'), ('mean_x', '<f4'), ('mean_y', '<f4'), ('var_x', '<f4'), ('var_y', '<f4'), ('length', '<f4'), ('scale', '<f4'), ('x_pos', '<f4'), ('y_pos', '<f4'), ('t_pos', '<f4'), ('coords', '<f4', (16, 2)), ('trajectory', '<f4', (15, 2)), ('hog', '

convert list values to rows in pandas

≡放荡痞女 提交于 2019-12-12 17:24:08
问题 I have dataframe where one of the column has numpy.ndarray values with same length, df[list][0] Out[92]: array([0. , 0. , 0. , ..., 0.29273096, 0.30691767, 0.27531403]) I would like to convert these list values to be a dataframe and filled as single column value from df.iloc[,1:len(list)] Example list 1 2 3 ... 0 [..] 0 0 0 1 [..] 0.570642 0.181552 0.794599 2 [..] 0.568440 0.501638 0.186635 3 [..] 0.679125 0.642817 0.697628 . . 回答1: I think need convert values to list s and then call

Convert string containg array of floats to numpy array

北战南征 提交于 2019-12-11 18:25:30
问题 I have a numpy array of floats that I wish to convert to a string to transmit via JSON: import numpy as np #Create an array of float arrays numbers = np.array([[1.0, 2.0],[3.0,4.0],[5.0,6.0]], dtype=np.float64) print(numbers) [[1. 2.] [3. 4.] [5. 6.]] #Convert each row in the array to string and separate by a ',' numbers_to_string_commas = ','.join(str(number) for number in numbers) print(numbers_to_string_commas) [1. 2.],[3. 4.],[5. 6.] Now I wish to convert this string back into the

Numpy 2-D & 3-D matrix “row”-wise multiplication

与世无争的帅哥 提交于 2019-12-11 18:24:10
问题 I started learning numpy and I'm trying to do a kind of strange matrix multiplication between a 2-dimensional and a 3-dimensional matrices. I have a function that does what I need, but I'm curious if there's a better way of doing it. Let's consider we have a matrix M1 with (KxN) dimensions, and have another matrix M2 with (KxNxN) dimensions. I'm trying to multiply each (1xN) rows of M1 with the corresponding (NxN) matrix of M2. Here's my code with sample matrices: a = [[1., 2., 3.], [0., 9.,