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]
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
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
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
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
通过使用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
在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。常用的指标如下:
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)