ndarray的基本运算--附源码

import numpy as np

1.逻辑运算

score = np.random.randint(40, 100, (10, 5)) # 40`100生成10*5数组
score
array([[65, 92, 42, 52, 91],
       [41, 92, 82, 55, 95],
       [77, 66, 88, 94, 43],
       [75, 92, 53, 89, 86],
       [94, 59, 50, 60, 73],
       [57, 57, 67, 99, 72],
       [84, 77, 83, 84, 49],
       [88, 81, 72, 85, 85],
       [72, 90, 61, 69, 72],
       [54, 63, 70, 85, 99]])
test_score = score[6:, 0: 5] # 取6`10组,每组取0`5元素
test_score
array([[84, 77, 83, 84, 49],
       [88, 81, 72, 85, 85],
       [72, 90, 61, 69, 72],
       [54, 63, 70, 85, 99]])
test_score > 60
array([[ True,  True,  True,  True, False],
       [ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True],
       [False,  True,  True,  True,  True]])
test_score[test_score > 60] = 1
test_score
array([[ 1,  1,  1,  1, 49],
       [ 1,  1,  1,  1,  1],
       [ 1,  1,  1,  1,  1],
       [54,  1,  1,  1,  1]])

2.通用判断函数

np.all(score[0:2, :] > 60) # 0`2组数据所有大于60才为1
False
score
array([[65, 92, 42, 52, 91],
       [41, 92, 82, 55, 95],
       [77, 66, 88, 94, 43],
       [75, 92, 53, 89, 86],
       [94, 59, 50, 60, 73],
       [57, 57, 67, 99, 72],
       [ 1,  1,  1,  1, 49],
       [ 1,  1,  1,  1,  1],
       [ 1,  1,  1,  1,  1],
       [54,  1,  1,  1,  1]])
np.any(score[0:2, :] > 90) # 0~2组数据其一大于90则为1
True

3.np.where(三元运算符)

temp = score[:4, :4]
temp
array([[65, 92, 42, 52],
       [41, 92, 82, 55],
       [77, 66, 88, 94],
       [75, 92, 53, 89]])
np.where(temp > 60, 1, 0) # temp >60 为1 否则为0
array([[1, 1, 0, 0],
       [0, 1, 1, 0],
       [1, 1, 1, 1],
       [1, 1, 0, 1]])
np.where(np.logical_and(temp > 60, temp < 90), 1, 0) # 复合上逻辑与
array([[1, 0, 0, 0],
       [0, 0, 1, 0],
       [1, 1, 1, 0],
       [1, 0, 0, 1]])
np.where(np.logical_or(temp > 90, temp < 60), 1, 0) # 复合上逻辑与或
array([[0, 1, 1, 1],
       [1, 1, 0, 1],
       [0, 0, 0, 1],
       [0, 1, 1, 0]])

4.统计运算

temp = score[:4, :]
temp
array([[65, 92, 42, 52, 91],
       [41, 92, 82, 55, 95],
       [77, 66, 88, 94, 43],
       [75, 92, 53, 89, 86]])
np.max(temp) # 返回所有值中max
95
np.mean(temp)# 返回所有值中的中位
73.5
np.max(temp, axis=0) # axis表示列或者行,具体自己试试, np.min同理
array([77, 92, 88, 94, 95])
np.argmax(temp) # 返回最大值下标
9
np.argmax(temp, axis = 1)# 返回最大值下标
array([1, 4, 3, 1], dtype=int64)

5.数组间运算

a = np.array([[1,2,3],[3,4,5]])
a
array([[1, 2, 3],
       [3, 4, 5]])
a + 3
array([[4, 5, 6],
       [6, 7, 8]])
a / 2
array([[0.5, 1. , 1.5],
       [1.5, 2. , 2.5]])
a = [1,2,3]
a*3 # python原生和ndarray区别
[1, 2, 3, 1, 2, 3, 1, 2, 3]
arr1 = np.random.randint(1,10,(2,6))
arr2 = np.random.randint(5,8,(2,4))
print(arr1
      ,arr2)
[[9 3 9 7 7 4]
 [4 1 8 5 7 9]] [[6 6 5 6]
 [5 6 7 6]]
# arr1 + arr2 不可以进行运算
arr2=np.array([[1],[3]])
print(arr1.shape
,arr2.shape) # 每一个纬度中,对应数相同或某方为1,则可以广播机制运算
(2, 6) (2, 1)
arr1+arr2
array([[10,  4, 10,  8,  8,  5],
       [ 7,  4, 11,  8, 10, 12]])

6.矩阵运算

a = np.random.randint(80,90,(6,2))
a
array([[87, 80],
       [81, 81],
       [84, 88],
       [89, 87],
       [84, 86],
       [84, 86]])
b = np.array([[0.7],[0.3]])
np.dot(a,b) # 支持点乘
array([[84.9],
       [81. ],
       [85.2],
       [88.4],
       [84.6],
       [84.6]])
np.matmul(a, b)  # 不支持点乘
array([[84.9],
       [81. ],
       [85.2],
       [88.4],
       [84.6],
       [84.6]])

源码

#!/usr/bin/env python
# coding: utf-8

# In[2]:


import numpy as np


# # 逻辑运算

# In[3]:


score = np.random.randint(40, 100, (10, 5)) # 40`100生成10*5数组


# In[4]:


score


# In[5]:


test_score = score[6:, 0: 5] # 取6`10组,每组取0`5元素


# In[6]:


test_score


# In[7]:


test_score > 60


# In[8]:


test_score[test_score > 60] = 1


# In[9]:


test_score


# # 通用判断函数

# In[10]:


np.all(score[0:2, :] > 60) # 0`2组数据所有大于60才为1


# In[11]:


score


# In[12]:


np.any(score[0:2, :] > 90) # 0~2组数据其一大于90则为1


# # np.where(三元运算符)

# In[13]:


temp = score[:4, :4]


# In[14]:


temp


# In[15]:


np.where(temp > 60, 1, 0) # temp >60 为1 否则为0


# In[16]:


np.where(np.logical_and(temp > 60, temp < 90), 1, 0) # 复合上逻辑与


# In[17]:


np.where(np.logical_or(temp > 90, temp < 60), 1, 0) # 复合上逻辑与或


# # 统计运算

# In[18]:


temp = score[:4, :]


# In[19]:


temp


# In[20]:


np.max(temp) # 返回所有值中max


# In[21]:


np.mean(temp)# 返回所有值中的中位


# In[22]:


np.max(temp, axis=0) # axis表示列或者行,具体自己试试, np.min同理


# In[23]:


np.argmax(temp) # 返回最大值下标


# In[24]:


np.argmax(temp, axis = 1)# 返回最大值下标


# # 数组间运算

# In[25]:


a = np.array([[1,2,3],[3,4,5]])


# In[26]:


a


# In[27]:


a + 3


# In[28]:


a / 2


# In[29]:


a = [1,2,3]
a*3 # python原生和ndarray区别


# In[30]:


arr1 = np.random.randint(1,10,(2,6))
arr2 = np.random.randint(5,8,(2,4))


# In[31]:


print(arr1
      ,arr2)


# In[32]:


# arr1 + arr2 不可以进行运算


# In[33]:


arr2=np.array([[1],[3]])
print(arr1.shape
,arr2.shape) # 每一个纬度中,对应数相同或某方为1,则可以广播机制运算


# In[34]:


arr1+arr2


# # 矩阵运算

# In[35]:


a = np.random.randint(80,90,(6,2))


# In[36]:


a


# In[37]:


b = np.array([[0.7],[0.3]])


# In[39]:


np.dot(a,b) # 支持点乘


# In[40]:


np.matmul(a, b)  # 不支持点乘


# In[ ]:





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

popcorn_min

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

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

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

打赏作者

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

抵扣说明:

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

余额充值