我正在尝试使用 np.newaxis 将一列的二维数组转换为一维向量。到目前为止,我得到的结果是 3D 数组,而不是 1D 矢量或 1D 数组。
二维数组 y1 是:
y1.shape
(506, 1)
y1
array([[0.42 ],
[0.36666667],
[0.66 ],
[0.63333333],
[0.69333333],
... ])
现在我想把它转换成一维数组
import numpy as np
y2=y1[np.newaxis,:]
y2.shape
(1, 506, 1)
你可以看到使用np.newaxis后,y2的形状变成了3D数组,我期待的是(506,)1D数组的形状。
我上面的代码有什么问题?谢谢
答案 0 :(得分:2)
np.newaxis
将维度扩展为 2D -> 3D。如果你想减少你的维度 2D -> 1D,使用 squeeze
:
>>> a
array([[0.42 ],
[0.36666667],
[0.66 ],
[0.63333333],
[0.69333333]])
>>> a.shape()
(5, 1)
>>> a.squeeze()
array([0.42 , 0.36666667, 0.66 , 0.63333333, 0.69333333])
>>> a.squeeze().shape
(5,)
<块引用>
选择元组中的每个 newaxis 对象都用于将结果选择的维度扩展一个单位长度维度。添加的维度是 newaxis 对象在选择元组中的位置。
答案 1 :(得分:2)
static void Main(string[] args)
{
BigInteger currenthighest =new BigInteger(Math.Pow(2,68));
BigInteger currentValue;
Console.WriteLine(currenthighest);
Console.ReadKey();
for (int i = 1; i > -1; i++)
{
Parallel.For(0, 1000000,z=>
{
currentValue = currenthighest;
while (currentValue != 4)
{
if (currentValue % 2 == 0)
{
currentValue = currentValue / 2;
}
else
{
currentValue = (currentValue * 3) + 1;
}
}
currenthighest++;
});
Console.WriteLine(" {0} times done", i * 1000000);
}
}
用于增加数组的维数。它不会减少维度。为了减少维度,您可以使用:
np.newaxis()
reshape()