Newaxis contained in
numpy can add one dimension to the original array
np.newaxis produces a different array
depending on where it is placed
one-dimensional array
x = np.random.randint(1, 8, size=5)
x
Out[48]: array([4, 6, 6, 6, 5])
x1 = x[np.newaxis, :]
x1
Out[50]: array([[4, 6, 6, 6, 5]])
x2 = x[:, np.newaxis]
x2
Out[52]:
array([[4],
[6],
[6],
[6],
[5]])
as you can see from the above code,
when putting newaxis first
, which used to be 5, now becomes 1
x span> span> span> span> span> span> span>
< script type=”math/tex” id=”MathJax-Element-124″> \times< /script> 5, so the first dimension has changed, the second dimension has changed
and when you put newaxis in the end, the shape of the new array that you output is 5
x span> span> span> span> span> span> span>
< script type=”math/tex” id=”MathJax-Element-125″> \times< /script> So 1, that’s another dimension that’s less than /p>
So, where you put newaxis, you’ll see an extra dimension in your shape that’s less than /p b>
is as follows:
general problem
is often a problem where you need to take a portion of the data out of the array, that is, take a “slice” or a “strip”
, for example, you need to extract a column
from a two-dimensional array
when you take out the dimension becomes one
if we want to reduce it to two dimensions, we need the above method
Read More:
- Anaconda builds a new environment and installs sklearn, numpy and other modules
- Warning when using numpy: runtimewarning: numpy.dtype size changed, may indicate binary incompatibility
- UserWarning: Failed to initialize NumPy: No module named ‘numpy.core._multiarray_umath‘
- Unable to call numpy in pychar, module notfounderror: no module named ‘numpy’
- Clion automatically adds add_ executable
- Pymong adds a unique index pymongo. Errors. Duplicatekeyerror
- Numpy realizes the forward propagation process of CNN
- JQ adds a click event to the option of select
- numpy.random.rand()
- The SSM framework adds the mybatis paging plug-in to start the error report
- Clion develops STM32, adds files and compiles with an error “no such file or directory”“
- [Python] numpy library array splicing np.concatenate Detailed explanation and examples of official documents
- MacOS adds code command to vs Code
- Dataframe to numpy.ndarray Related issues of
- Memory error in Python numpy matrix
- Pandas generates new columns through LOC
- Importing the multiarray numpy extension module failed
- Numpy error: oserror: failed to interpret file as a pickle
- AttributeError: module ‘numpy‘ has no attribute ‘getbuffer‘
- NotImplementedError: Cannot convert a symbolic Tensor (LSTM/strided_slice:0) to a numpy array