NumPy zeros can be defined even with a size of 0

Overview

--NumPy allows you to define zeros of size 0 like np.zeros ((0, 4)) --With this, you can also define an empty list and add it with append () with array.

I want to do something like list.append () with NumPy array

List can use append

What I often do when creating lists in Python

a = []
for i in range(10):
    a.append(1)

Someone like

Array has no append

If you try to do the same with a NumPy Array, you'll end up using numpy.vstack () or numpy.hstack () → Since the number of rows (or columns) of the two Arrays to be attached must match, we first defined an Array with shape = (1, 4).

Bad person


import numpy as np
a = np.zeros((1, 4))
for i in range(10):
    ones = np.ones((1, 4))
    a = a.vstack((a, ones))
a = a[1:, :] #Only the first defined part is excluded

Array of size 0 can be defined

I felt uncomfortable with the operation of defining an Array of shape = (1, 4) first and then removing it, so I had to define zeros of size 0 as a trial.

Easy to see


import numpy as np
a = np.zeros((0, 4)) #Zero size zeros
for i in range(10):
    ones = np.ones((1, 4))
    a = a.vstack((a, ones))


Recommended Posts

NumPy zeros can be defined even with a size of 0
Format summary of formats that can be serialized with gensim
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
Detect objects of a specific color and size with Python
Let's make a diagram that can be clicked with IPython
[Python] A program that finds the maximum number of toys that can be purchased with your money
[Python] Make a graph that can be moved around with Plotly
Make a Spinbox that can be displayed in Binary with Tkinter
I made a shuffle that can be reset (reverted) with Python
Make a currency chart that can be moved around with Plotly (2)
Comparison of 4 styles that can be passed to seaborn with set_context
Make a Spinbox that can be displayed in HEX with Tkinter
Make a currency chart that can be moved around with Plotly (1)
Can I be a data scientist?
Draw a beautiful circle with numpy
requirements.txt can be commented out with #
Get a list of camera parameters that can be set with cv2.VideoCapture and make it a dictionary type
Get the id of a GPU with low memory usage
NumPy zeros can be defined even with a size of 0
Can I be a data scientist?
How many bytes of files can be placed on a 50M disk?
What Java users thought of using the Go language for a day
How many bytes of files can be placed on a 50M disk?
Create a web app that can be easily visualized with Plotly Dash
[Can be done in 10 minutes] Create a local website quickly with Django
Maximum number of function parameters that can be defined in each language
Draw a graph that can be moved around with HoloViews and Bokeh
Be careful of the type when making an image mask with Numpy
Article that can be a human resource who understands and masters the mechanism of API (with Python code)
Align the size of the colorbar with matplotlib
I made a life game with Numpy
Read a character data file with numpy
Weighting of random.choice even under numpy v1.6
A memo for making a figure that can be posted to a journal with matplotlib
Tensorflow, it seems that even the eigenvalues of the matrix can be automatically differentiated
A class for PYTHON that can be operated without being aware of LDAP
A personal memo of Pandas related operations that can be used in practice
To output a value even in the middle of a cell with Jupyter Notebook
Since python is read as "Pichon", it can be executed with "Pichon" (it is a story)
Format DataFrame data with Pytorch into a form that can be trained with NN
I made a familiar function that can be used in statistics with Python