[PYTHON] Add rows to an empty array with numpy

Postscript: I'm a beginner, so I don't know how to use NumPy at all lol Instead of creating and combining Numpy arrays in a loop All you have to do is create a multidimensional list and finally convert it with np.array (). Easy to implement and fast to operate.

In short, I wanted to do something like the article below. https://kakedashi-engineer.appspot.com/2020/02/28/multi-list-numpy/

Whole source code

numpy_join.py


# coding:UTF-8
import numpy as np
a = np.zeros((0,3))
for i in range(5):
    b = np.array([1, 2, 3])
    a = np.r_[a,b.reshape(1,-1)]
print a

I want to make a two-dimensional array in the end, so in this way

python


a = np.zeros((0,3))

The point is to do like this. The number of elements in one row is 3, and a two-dimensional array with 0 rows is created. You can use np.empty, but since it has 0 lines anyway, I dared to use np.zeros.

continue

python


a = np.r_[a,b.reshape(1,-1)]

However, note that b is reshaped to make a two-dimensional array.

result


[[ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]]

The desired result was obtained.

Recommended Posts

Add rows to an empty array with numpy
Create an empty array in Numpy to add rows for each loop
Create a 2D array by adding a row to the end of an empty array with numpy
How to read an array with Python's ConfigParser
How to convert an array to a dictionary with Python [Application]
I wanted to calculate an array with Sympy's subs method
Subscript access to python numpy array
Add fields to features with ArcPy
Extract multiple elements with Numpy array
How to add a package with PyCharm
Add images to iOS photos with Pythonista
What to do if an error occurs when importing numpy with VScode
Convert array (struct) to json with golang
Be careful when adding an array to an array
[Python] Add total rows to Pandas DataFrame
Add Gaussian noise to images with python2.7
Try to generate an image with aliasing
I want to write an element to a file with numpy and check it.
Save an array of numpy to a wav file using the wave module
Convert data with shape (number of data, 1) to (number of data,) with numpy.
Just add the python array to the json data
Send an email to Spushi's address with python
How to add arbitrary headers to response with FastAPI
How to crop an image with Python + OpenCV
I want to be an OREMO with setParam!
Raise "numpy power" with [100 numpy exercises] (11th to 20th questions)
I tried to detect an object with M2Det!
Speed: Add element to end of Python array
Add / remove kernel to use jupyter with venv
Add totals to rows and columns in pandas
Post an article with an image to WordPress with Python
NumPy array manipulation (3)
NumPy array manipulation (1)
Pass an array from PHP to PYTHON and do numpy processing to get the result
I want to convert an image to WebP with lollipop
[Python] Create structured array (store heterogeneous data with NumPy)
Add CSV export function to management screen with django-import-export
How to add help to HDA (with Python script bonus)
Various ways to extract columns in a NumPy array
I also wanted to check type hints with numpy
Add information to the bottom of the figure with Matplotlib
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Just add the driver to the shape key with blender
An introduction to Python distributed parallel processing with Ray
I tried Smith standardizing an integer matrix with Numpy
Reading Note: An Introduction to Data Analysis with Python
Raise "numpy power" with [100 numpy exercises] (1st to 10th questions)
An easy way to create an import module with jupyter
I tried to implement an artificial perceptron with python
How to make an HTTPS server with Go / Gin
I tried to make an OCR application with PySimpleGUI
Convert elements of numpy array from float to int
I tried to find an alternating series with tensorflow
Building an environment to use CaboCha with google colaboratory
I tried it with SymPyLive by referring to "[Ruby] Getting unique elements from an array".