numpy-ndarray

How to insert data in a numpy array, preserving its shape?

How to insert data in a numpy array, preserving its shape? Question: I have a collection of several data of fixed shape, and I need to store these values in an array, only creating a new dimension, so I can preserve its initial shape. Here is a small example, arr = np.array([0,1,2]) values = np.array([[3,4,5], …

Total answers: 2

Polars Series.to_numpy() does not return ndarray

Polars Series.to_numpy() does not return ndarray Question: I was trying to convert a series to a numpy array via .to_numpy() but unlike what the documentation shows i am not getting a ndarray out but a seriesview Running exactly the example in the documentation: https://pola-rs.github.io/polars/py-polars/html/reference/series/api/polars.Series.to_numpy.html s = pl.Series("a", [1, 2, 3]) arr = s.to_numpy() arr type(arr) …

Total answers: 1

How to perform basic math on numpy ndarray

How to perform basic math on numpy ndarray Question: So, I have a numpy ndarray with dimensions (984, 1977, 2). What I want to accomplish is to have a numpy ndarray where I do basic math on the final values. So let’s say data is my ndarray. And data[0][0] equals to [72 46]. So I …

Total answers: 1

Trying to translate for loops into numpy matrix operation

Trying to translate for loops into numpy matrix operation Question: This is a bit of an odd question, but I am trying to improve the runtime of my code by losing the for loops and relying on numpy operations. I am still a beginner at handling numpy matrix operations, and I cant seem to translate …

Total answers: 1

Creating a curved array diagram without contours

Creating a curved array diagram without contours Question: So I have a python array and a code that produces a 2D representation of it: import numpy as np import matplotlib.pyplot as plt # Define the input data as a 2D NumPy array arr = np.array([ [‘x’, ‘x’, ‘x’, ‘y’, ‘y’, ‘z’, ‘z’, ‘z’, ‘z’, ‘z’, …

Total answers: 1

Multidimentional array to pandas dataframe

Multidimentional array to pandas dataframe Question: Suppose I have the following 4D array: A = np.array([ [[[0, 1, 2, 3], [3, 0, 1, 2], [2, 3, 0, 1], [1, 3, 2, 1], [1, 2, 3, 0]]], [[[9, 8, 7, 6], [5, 4, 3, 2], [0, 9, 8, 3], [1, 9, 2, 3], [1, 0, -1, …

Total answers: 1

Get average between consecutive pairs of numpy array

Get average between consecutive pairs of numpy array Question: Say I have a numpy array like this [1,2,3,4,5] I want to generate an array that is the equal to the average of consecutive elements [1.5,2.5,3.5,4.5] Is there any efficient way to do this outside of just iterating through? I’m not really sure what to do …

Total answers: 2

Using np.multiply(out=array[mask]) does not work

Using np.multiply(out=array[mask]) does not work Question: I tested the behavior of in-place operations with numpy. I observed the following: test = np.arange(0, 10).reshape(2, 5) mask = test > 5 This works np.multiply(test, 3, out=test) # Output: [[ 0 3 6 9 12] [15 18 21 24 27]] but this np.multiply(test[mask], 3, out=test[mask]) print(test) # Output: …

Total answers: 1

Python Find matching item in a list of dictionaries

Python Find matching item in a list of dictionaries Question: I have a big list of dictionaries. Each dictionary contains timeseries data of each sensor when each data point is collected. I want to know the index location of a specific sensor and date. So, I can update the sensor value. My code: big_list = …

Total answers: 7

Python: Send list of multidimensional numPy arrays over socket

Python: Send list of multidimensional numPy arrays over socket Question: i wish to send a list consisting of multi-dimensional NumPy arrays over the socket to my server and restore its format right after. The List of arrays (variable aggregated_ndarrays) looks like the following: [array([[[[-1.04182057e-01, 9.81570184e-02, 8.69736895e-02, -6.61955923e-02, -4.51700203e-02], [ 5.26290983e-02, -1.18473642e-01, 2.64136307e-02, -9.26332623e-02, -6.63961545e-02], [-8.80082026e-02, …

Total answers: 2