numpy:矩阵或者数组相减

# -*- coding: utf-8 -*-
"""
numpy:矩阵或者数组相减
"""
import numpy as np


if __name__ == '__main__':
    feature = np.array([2,3,5])
    center = np.array([1,2,3])
    
    print("原始数据维度:")
    print(feature.shape)
    print(center.shape)
    
    result = feature - center
    print("维度相同的减法:")
    print(result)
    
    print('\n******************\n')
    
    feature2 = np.array([[2,3,5]])
    center2 = np.array([[1,2,3],[2,3,4]])
    
    print("扩充维度:")
    print(feature2.shape)
    print(center2.shape)
    
    result2 = feature2 - center2
    print("维度不同的减法:")
    print(result2)   
    print(result2.shape)
    
    #产生这种结果的原因:是因为由于维度不同,在计算的时候将feature2变为了与center2同样的维度,等同于如下的计算:   
    feature3 = np.array([[2,3,5],[2,3,5]])
    result3 = feature3 - center2
    print("python的广播机制:")
    print(result3)   
    print(result3.shape)

结果:

原始数据维度:
(3,)
(3,)
维度相同的减法:
[1 1 2]

******************

扩充维度:
(1, 3)
(2, 3)
维度不同的减法:
[[1 1 2]
 [0 0 1]]
(2, 3)
python的广播机制:
[[1 1 2]
 [0 0 1]]
(2, 3)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peanut_范

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值