Let a = np.arange(1, 4). To get the 2 dimensional multiplication table for a, I do: For 3 dimensions, I can do the following: How could I write a function that takes a numpy array a and a number of dimensions n as input and ouputs the n dimensional multiplication table for a? Answer This should do what you need:
Tag: numpy-ndarray
How do I construct an incidence matrix from two dataframe columns using scipy.sparse.coo_matrix((data, (i, j)))?
I have a pandas DataFrame containing two columns [‘A’, ‘B’]. Each column is made up of integers. I want to construct a sparse matrix with the following properties: row index is all integers from 0 to the max value in the dataframe column index is the same as row index entry i,j = 1 if [i,j] or [j,i] is a
numpy filling the diagonal 0 for 3D array
Suppose I have a 3D array, how can I fill the diag of the first two dimensions to zero. For example Is there a way to replace the for loop? Answer The following is one of the solution
create a matrix from combinations with values
I have some combinations like Any idea on how I efficiently can create a four by four matrix with these 0,1 values from all these combinations? So the result will be something like: Answer Imagine if the “combinations” are stored in a file in the following format (or similar): Then you can do: Example (using your sample data): Now df
Python add 1d array to 2d array by column
I have a 1d array and 2d array I want: I tried np.vstack, np.concenrate but all failed Answer You can use numpy.column_stack:
How to convert a pandas dataframe column to an image array i.e. a numpy array with shape (n,n) in Python?
Suppose my dataframe has 750 rows in a column and I want to convert that column to an image array of (20,20) numpy array. How to do that? EDIT1: I want to use the array for ax.contourf (x,y,z) as z. I got x,y by doing x,y=np.meshgrid(df3.x,df.y) now I want to convert another column to an (n,n) array to vary the
Reshaping a 3D array to a 2D array to produce a DataFrame: keep track of indices to produce column names
The following code generates a pandas.DataFrame from a 3D array over the first axis. I manually create the columns names (defining cols): is there a more built-in way to do this (to avoid potential errors e.g. regarding C-order)? –> I am looking for a way to guarantee the respect of the order of the indices after the reshape operation (here
is there a rule of thumb to know if i’m modifying a value or a referenced value?
Consider the following : How am i supposed to know that modifying hull.points (or foo, a reference to hull.points) is modifying pts ? The documentation only say : The inspector in pycharm also tell me that both foo and hull.points are a ndarray and nothing in the code, documentation, inspector tell me that my variables are, in fact, pointers referencing
copy from two multidimensional numpy array to another with different shape
I have two numpy arrays of the following shape: Array a is empty, as I just need this predefined shape, I created it with: Now I would like to copy data from array b to array a so that the second and third dimension of array a gets filled with the same 3 values of the corresponding row of array
Combination of rows in numpy.ndarray
I have the following numpy.ndarray I want to find all the possible combinations of sum of each row (sum of individual elements of a row except the last column) of S[0,:,:] with each row of S[1,:,:], i.e., my desired result is (order does not matter): which is a 9-by-2 array resulting from 9 possible combinations of S[0,:,:] and S[1,:,:]. Although