[Python] numpy.empty initial value setting

I always used only numpy.zeros or numpy.ones when setting initial values with numpy. In addition to this, there was a method to set the initial value, numpy.empty, so write it. Also, check how to use the initial value settings.

environment

linux pyhton numpy.empty This argument ↓

numpy.empty(shape, dtype=float, order='C')

Experiment 1 Do not have anything in the argument.

python


>>> import numpy
>>> numpy.empty()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: empty() missing required argument 'shape' (pos 1

I get an error that there is no shape argument.

Experiment 2 Give it a shape.

python


>>> import numpy
>>> numpy.empty(shape=1)
array([0.])
>>> numpy.empty(shape=2)
array([-5.73021895e-300,  6.93125508e-310])
>>> numpy.empty(shape=3)
array([0., 0., 0.])
>>> numpy.empty(shape=4)
array([0., 0., 0., 0.])
>>> numpy.empty(shape=5)
array([4.66352184e-310, 4.03179200e-313, 4.66352172e-310, 5.54048513e+228,
       7.56680154e+168])
>>> numpy.empty(shape=6)
array([4.66352179e-310, 5.72938864e-313, 6.93125428e-310, 6.93125428e-310,
       0.00000000e+000, 1.16707455e-072])
>>> numpy.empty((2,3))
array([[4.66352179e-310, 5.72938864e-313, 6.93125428e-310],
       [6.93125428e-310, 0.00000000e+000, 1.16707455e-072]])
>>> numpy.empty(5.6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer

did it. .. .. From this, it was found that shape = does not have to be present, the number cannot be a decimal number, and the initial value may be 0 depending on the shape.

Experiment 3 Have a dtype. Since it was found from Experiment 1 that shape is essential, we also gave it shape.

python


>>> import numpy
>>> numpy.empty(2,dtype=float)
array([-5.73021895e-300,  6.93125508e-310])
>>> numpy.empty(2,dtype=int)
array([-9093133594791772939,      140290164735896])
>>> numpy.empty(2,int)
array([130238442063002869,    140290164735896])

From this, it can be seen that when dtype is set to float, the initial value becomes a decimal number, and when dtype is set to int, the initial value becomes an integer. Also, it can be seen from numpy.empty (2, int) that the dtype = part of the argument can be omitted.

Experiment 4 Change order ='C'.

python


>>> import numpy
>>> numpy.empty(2,dtype=float,order='C')
array([-5.73021895e-300,  6.93125508e-310])
>>> numpy.empty(2,dtype=float,order='F')
array([5.73021895e-300, 6.93125508e-310])
>>> numpy.empty(2,dtype=float,order='E')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: order must be one of 'C', 'F', 'A', or 'K' (got 'E')
>>> numpy.empty(2,dtype=float,order='A')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: only 'C' or 'F' order is permitted

From this, it can be seen that only order ='C' or order ='F' can be used. Also, there was no difference between order ='C' and order ='F'. Therefore, I thought that either one would be fine.

Recommended Posts

[Python] numpy.empty initial value setting
python Environmentally-friendly Japanese setting
Raspberry Pi 4B initial setting
Parameter setting by python configparser
[python] Value of function object (?)
Shapley value calculation in Python
python url setting include function
Initial setting of Mac ~ Python (pyenv) installation is the fastest
Python> dictionary> get ()> optional value
ConoHa VPS (ubuntu 18.04) Initial setting memo
[Python] Find the second smallest value.
Replace dictionary value with Python> update ()
[Automation with python! ] Part 1: Setting file
Try singular value decomposition with Python
[Python] Variable (definition, output, value change)
Raspberry pi initial setting (for myself)
Vim + Python development environment setting memo
[Python] What is a formal argument? How to set the initial value