对numpy.c_的理解

文档描述

来自官方文档的叙述(这里只简单翻译一部分)
numpy.c_

numpy.c_ = <numpy.lib.index_tricks.CClass object>

Translates slice objects to concatenation along the second axis.
将要拼接的对象转换为沿第二轴的连接。
第二个轴即axis=1

This is short-hand for np.r_[’-1,2,0’, index expression], which is useful because of its common occurrence. In particular, arrays will be stacked along their last axis after being upgraded to at least 2-D with 1’s post-pended to the shape (column vectors made out of 1-D arrays).

关于python科学计算(pandas、numpy)中axis(轴)的理解

在这里插入图片描述
在这里插入图片描述

理解

作用是将多个矩阵左右拼接在一起,构成一个新矩阵。

实例:

#摘自文档
>>> np.c_[np.array([1,2,3]), np.array([4,5,6])]
array([[1, 4],
       [2, 5],
       [3, 6]])
>>> np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
array([[1, 2, 3, 0, 0, 4, 5, 6]])

我实际遇到的应用:

>>>import numpy as np
>>>from sklearn.datasets import fetch_california_housing

>>>housing=fetch_california_housing()
>>>print(housing)
{'data': array([[   8.3252    ,   41.        ,    6.98412698, ...,    2.55555556,
          37.88      , -122.23      ],
       [   8.3014    ,   21.        ,    6.23813708, ...,    2.10984183,
          37.86      , -122.22      ],
       [   7.2574    ,   52.        ,    8.28813559, ...,    2.80225989,
          37.85      , -122.24      ],
       ...,
       [   1.7       ,   17.        ,    5.20554273, ...,    2.3256351 ,
          39.43      , -121.22      ],
       [   1.8672    ,   18.        ,    5.32951289, ...,    2.12320917,
          39.43      , -121.32      ],
       [   2.3886    ,   16.        ,    5.25471698, ...,    2.61698113,
          39.37      , -121.24      ]]), 'target': a
          
>>>m,n=housing.data.shape
>>>print(m,n)
20640 8
#np.ones((m,1))构造一个m行1列,数值全为1的矩阵
#np.c_(A,B)将A与B延第二个轴拼接在一起。
>>>housing_data_plus_bias=np.c_[np.ones((m,1)),housing.data]
>>>print(housing_data_plus_bias)

[[   1.            8.3252       41.         ...    2.55555556
    37.88       -122.23      ]
 [   1.            8.3014       21.         ...    2.10984183
    37.86       -122.22      ]
 [   1.            7.2574       52.         ...    2.80225989
    37.85       -122.24      ]
 ...
 [   1.            1.7          17.         ...    2.3256351
    39.43       -121.22      ]
 [   1.            1.8672       18.         ...    2.12320917
    39.43       -121.32      ]
 [   1.            2.3886       16.         ...    2.61698113
    39.37       -121.24      ]]

可以看到,最左侧一列是矩阵np.ones((m,1)),而其余列均为矩阵housing.data

OK!记录完毕

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值