np.newaxis与np.shape的一些细节

前言:由于在一次的学习过程中,学到了np.newaxis这一部分,使得对数组的问题,包括数据的维度问题产生了一些疑惑,通过上网搜查好像这方面的讲解没有找到,于是为了验证这个问题,只有通过自己动手实验来考察哪种结论是对的。


1、shape的问题---------->shape(2,)和shape(2,1)区别

这部分的内容网上搜的到,这里也总结下。

       在遇到数组问题的时候,经常会看到(2,)和(2,1)这两种情况出现, 刚开始没注意他们的区别,觉得也差不多啦,后来才发现大错特错。通过例子来进行说明。

import numpy as np
a =np.arange(1,3)
print("a",a)
print("a.shape",a.shape)

b= a.reshape(2,1)
print("b",b)
print("b.shape",b.shape)

c = a.reshape(1,2)
print("c",c)
print("c.shape",c.shape)

# 其中np.dot是矩阵乘法
r = a*c   -------------------------点乘,就是对应元素相乘
result = np.dot(a,b)
# resu = np.dot(a,c)
print(r)
print(result)

结果:
a [1 2]
a.shape (2,)
b [[1]
 [2]]
b.shape (2, 1)
c [[1 2]]
c.shape (1, 2)
[[1 4]]
[5]

Process finished with exit code 0

1.1.shape(2,)和shape(2,1)的区别

a[1,2]的shape值(2,),意思是一维数组,数组中有2个元素

b[[1],[2]]的shape值是(2,1),意思是一个二维数组,每行有1个元素

c [[1,2]]的shape值是(1,2),意思是一个二维数组,每行有2个元素

1.2一维数组的方向

从上面的这个例子可以看出,一维数组默认的方向是行向量(因为我用的是矩阵乘法),为什么这么说,我们在给个例子,上面的例子可以看到有个resu = np.dot(a,c)被我注释了,下面我把它放开再看。

import numpy as np
a =np.arange(1,3)
print("a",a)
print("a.shape",a.shape)

b= a.reshape(2,1)
print("b",b)
print("b.shape",b.shape)

c = a.reshape(1,2)
print("c",c)
print("c.shape",c.shape)

# 其中np.dot是矩阵乘法
r = a*c
result = np.dot(a,b)
resu = np.dot(a,c)
print(r)
print(result)

结果:
a [1 2]
a.shape (2,)
b [[1]
 [2]]
b.shape (2, 1)
c [[1 2]]
c.shape (1, 2)
Traceback (most recent call last):
  File "E:/python代码文件/作业/Tensorflow/Tersorflow-1.py", line 72, in <module>
    resu = np.dot(a,c)
ValueError: shapes (2,) and (1,2) not aligned: 2 (dim 0) != 1 (dim 0)

Process finished with exit code 1

上面的代码,我未做任何的修改,只是放开了一句代码,结果就报错了。

shapes (2,) and (1,2) not aligned: 2 (dim 0) != 1 (dim 0)--------------->>>未对齐的形状(2,)和(1,2)

这说明个问题呢?

好像在说明numpy默认的方向是行向量,好像是的。还怀疑的话,我们将它转化为矩阵再来看看说明情况。

A = np.arange(1,3)
B = np.mat(A)
print(B)
print(B.shape)
C=A.reshape(2,1)
D = np.mat(C)
print(D)
res = np.dot(B,D)
print(res)

结果:
[[1 2]]
(1, 2)
[[1]
 [2]]
[[5]]

Process finished with exit code 0

给出结论:

Numpy默认的生成的向量是行向量。

 

2、np.newaxis的问题

这部分网上也是有讲解的。这里还是捎带总结下。

针对一维的情况

b = np.array([1, 2, 3, 4, 5, 6])
c = b[np.newaxis]  #equals c = b[np.newaxis,:]
d = b[:,np.newaxis]
print(b.shape)
print(c.shape)
print(d.shape)

结果:
(6,)
(1, 6)
(6, 1)

Process finished with exit code 0

接下来,这部分见解是来自一个大牛------>>>

>> type(np.newaxis)
NoneType
>> np.newaxis == None
True

np.newaxis 在使用和功能上等价于 None,查看源码发现:newaxis = None,其实就是 None 的一个别名。源码我自己 没有看过。

通过上面的例子,我们仿佛发现了问题,np.newaxis 从字面上是插入新的维度的意思。【np.row,np.col】在按照np.newaxis是放在行的位置上还是列的位置上,相应的位置就会新增一个维度。

多维的暂时还没用到。

2.1维度相互转化的问题

方法1:使用shape,将一维数组变为二维数组。(10,)--------------->>>(10,1)

y=np.arange(1, 11)
y.shape=(10,1)
print(y)

结果:

[[ 1]
 [ 2]
 [ 3]
 [ 4]
 [ 5]
 [ 6]
 [ 7]
 [ 8]
 [ 9]
 [10]]

2. 使用np.newaxis

print(np.arange(0, 10)[:, np.newaxis])

结果:

[[0]
 [1]
 [2]
 [3]
 [4]
 [5]
 [6]
 [7]
 [8]
 [9]]

3 使用reshape,将数组(n,1)转换成(n,)

y=np.arange(1, 11)
y.shape=(-1,1)
print(y)

[[ 1]
 [ 2]
 [ 3]
 [ 4]
 [ 5]
 [ 6]
 [ 7]
 [ 8]
 [ 9]
 [10]]

Process finished with exit code 0

使用reshape也是可以的

将二维数组变化为一维数组(10,1)--------------->>>(10,)

y=np.arange(1, 11)[:,np.newaxis]
y_re = y.reshape(-1,)
print(y_re)

结果:
[ 1  2  3  4  5  6  7  8  9 10]

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/qq_20412595/article/details/82791130