#numpy##ndarray运算

ndarray运算

1 逻辑运算

1.1.创建一个有20个元素的数组
import numpy as np
a = np.arange(0,1000,50)
print(a)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\人工智能\numpy.py 
[  0  50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850
 900 950]
1.2.更改数组的形状
import numpy as np
a = np.arange(0,1000,50)
print(a)
b = a.reshape(-1,4)
print(b)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\人工智能\numpy.py 
[  0  50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850
 900 950]
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]

Process finished with exit code 0
1.3.对数组进行切片
import numpy as np
a = np.arange(0,1000,50)
print(a)
b = a.reshape(-1,4)
print(b)
c = b[0:3,0:3]
print(c)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\人工智能\numpy.py 
[  0  50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850
 900 950]
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[[  0  50 100]
 [200 250 300]
 [400 450 500]]

Process finished with exit code 0
1.4.将满足条件的设置为指定的值-布尔索引
import numpy as np
a = np.arange(0,1000,50)
print(a)
b = a.reshape(-1,4)
print(b)
c = b[0:3,0:3]
print(c)
print(c > 350)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\人工智能\numpy.py 
[  0  50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850
 900 950]
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[[  0  50 100]
 [200 250 300]
 [400 450 500]]
[[False False False]
 [False False False]
 [ True  True  True]]

Process finished with exit code 0
import numpy as np
a = np.arange(0,1000,50)
print(a)
b = a.reshape(-1,4)
print(b)
c = b[0:3,0:3]
print(c)
print(c > 350)
c[c > 350] = 888
print(c)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\人工智能\numpy.py 
[  0  50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850
 900 950]
[[  0  50 100 150]
 [200 250 300 350]
 [400 450 500 550]
 [600 650 700 750]
 [800 850 900 950]]
[[  0  50 100]
 [200 250 300]
 [400 450 500]]
[[False False False]
 [False False False]
 [ True  True  True]]
[[  0  50 100]
 [200 250 300]
 [888 888 888]]

Process finished with exit code 0

2 通用判断函数

np.all() 全部都满足才返回True
np.any()只要有一个满足才返回True

import numpy as np
a = np.arange(0,1000,50)
print(a)
b = np.all(a > 300)
print(b)
c = np.any(a > 300)
print(c)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\人工智能\numpy.py 
[  0  50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850
 900 950]
False
True

Process finished with exit code 0

3 np.where(三元运算符)

通过使用np.where能够进行更加复杂的运算

创建一个数组,如果数组里面元素大于300赋值1,否则赋值0

import numpy as np
a = np.arange(0,1000,50)
print(a)
b = np.where(a > 300,1,0)
print(b)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\人工智能\numpy.py 
[  0  50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850
 900 950]
[0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1]
Process finished with exit code 0

复合逻辑需要结合np.logical_and和np.logical_or使用

创建一个数组,数组内元素同时满足大于300并且小于400,赋值1,否则赋值0

import numpy as np
a = np.arange(0,1000,50)
print(a)
b = np.where(np.logical_and(a > 300,a < 400),1,0)
print(b)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\人工智能\numpy.py 
[  0  50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850
 900 950]
[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0]

Process finished with exit code 0

创建一个数组,数组内元素满足大于300或者小于400,赋值1,否则赋值0

import numpy as np
a = np.arange(0,1000,50)
print(a)
b = np.where(np.logical_or(a > 300,a < 400),1,0)
print(b)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\人工智能\numpy.py 
[  0  50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850
 900 950]
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]

Process finished with exit code 0

4 统计运算

4.1 统计指标

在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。常用的指标如下:

min(a[, axis, out, keepdims])
Return the minimum of an array or minimum along an axis.
max(a[, axis, out, keepdims])
Return the maximum of an array or maximum along an axis.
median(a[, axis, out,overwrite_input, keepdims])
Compute the median along the specified axis.
mean(a[, axis, dtype, out, keepdims])
Compute the arithmetic mean along the specified axis.
std(a[, axis, dtype, out, ddof, keepdims])
Compute the standard deviation along the specified axis.
var(a[, axis, dtype, out, ddof, keepdims])
Compute the variance along the specified axis.

import numpy as np
a = np.arange(0,1000,50)
print(a)
b = a.max()
print(b)
D:\Anaconda3\python.exe C:\Users\Windows11\Desktop\人工智能\numpy.py 
[  0  50 100 150 200 250 300 350 400 450 500 550 600 650 700 750 800 850
 900 950]
950

Process finished with exit code 0
axis 轴的取值并不一定,Numpy中不同的API轴的值都不一样,在这里,axis 0代表列, axis 1代表行去进行统计
import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
c = b.max(axis = 0)
print(c)

在这里插入图片描述

import numpy as np
a = np.arange(0,999,50)
b = a.reshape(5,-1)
print(b)
c = b.max(axis = 1)
print(c)

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值