numpy:np.newaxis 实现将行向量转换成列向量

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

numpy:np.newaxis 实现将行向量转换成列向量

  2021-04-02 我要评论

np.newaxis 新增一个轴

如何将数组[0,1,2]转换成列向量

用ndarray[: , np.newaxis]

代码实质就是将原本的(0,1,2)移到行上,然后新增一列

其实可以更简单

ndarray.shape=(3,1) 



>> x = np.arange(3)
>> x
array([0, 1, 2])
>> x[:, np.newaxis]
array([[0], 
  [1], 
  [2]])

>> x[:, None]
array([[0], 
  [1], 
  [2]])

>> x[:, np.newaxis].shape 
(3, 1)
  
>>> X = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
>>> X[:, 1]
array([2, 6, 10])    % 这里是一个行

>>>X[:, 1][:, np.newaxis]
array([[2], 
  [6], 
  [10]])

将行换成列

当提取数组的某一列时,结果输出是按行输出,用X[:, 1][:, np.newaxis],将行转换成列

https:

以上这篇numpy:np.newaxis 实现将行向量转换成列向量就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们