numpy-ndarray

Docs about boolean scalars in indexing of numpy array

Docs about boolean scalars in indexing of numpy array Question: The NumPy’s array indexing documentation seems to contain no mention of array indexing Of the type x[True], or x[False]. Empirically, using True inserts a new dimension of size 1, while using False inserts a new dimension of size 0. The behavior of x[True, True] only …

Total answers: 1

Assigning Values to Specific Rows and Columns of Numpy Array at the Same Time

Assigning Values to Specific Rows and Columns of Numpy Array at the Same Time Question: My Expected Output for Code below: array([[ 0, 1, 2, 3], [ 4, 999, 999, 7], [ 8, 999, 999, 11], [12, 13, 14, 15]]) Code: import numpy as np A = np.array(range(16)).reshape((4,4)) A[[1,3],:][:, [1,3]] = [[999,999],[999,999]] print(A) However, the …

Total answers: 2

How to Convert NumPy Array to String and Include Commas

How to Convert NumPy Array to String and Include Commas Question: I have a NumPy array called ‘colors’. Colors has a shape of (N, 3). I’m trying to convert colors into a string and format it so that each item in axis = 0 is seperated by a comma. EX. (1,2,3), (4,5,6), (7,8,9), … I …

Total answers: 1

"ValueError: x and y must be the same size" when plotting

"ValueError: x and y must be the same size" when plotting Question: I’m writing a python code that implements euler’s method to solve a 1st order ODE for an arbitrary range of values of time-step h. The simplest solution I’ve come up with is to declare a Python list to store the results at the …

Total answers: 1

How to insert multiple numpy arrays into another numpy array at different indices

How to insert multiple numpy arrays into another numpy array at different indices Question: Let’s say we have 4 numpy array A B,C and D, like following: A = np.array([11,22,33,44,55,66,77]) B = np.array([1,2,3,4,5]) C = np.array([6,7,8,9]) D = np.array([10,11]) I want to insert arrays B, C and D into array A. I have been given …

Total answers: 2

Sum rows of 2D array with elements of 1D array

Sum rows of 2D array with elements of 1D array Question: I have two ndarrays: a = [[1, 2], [100, 200]] and b = [10, 20] Is it possible to get such ndarray using numpy: [[1 + 10, 2 + 10], [100 + 20, 200 + 20]] Asked By: wowonline || Source Answers: You just …

Total answers: 3

Add the same value to every row in a numpy array

Add the same value to every row in a numpy array Question: I have a numpy array that looks like this: [[0.67058825 0.43529415 0.33725491] [0.01568628 0.30980393 0.96862751] [0.24705884 0.63529414 0.29411766] [0.27843139 0.63137257 0.37647063] [0.26274511 0.627451 0.33333334] [0.25098041 0.61960787 0.30980393]] I want to add a 1 to every row like this: [[0.67058825 0.43529415 0.33725491 1] [0.01568628 …

Total answers: 3