python实战应用讲解-【numpy专题篇】numpy常见函数使用示例(二)(附python示例代码)

目录

Python numpy.correlate()函数

Python Numpy count_nonzero函数

Python中的numpy.diff()

Python numpy.dtype.kind()函数

Python numpy.dtype.subdtype()函数

Python numpy.find_common_type()函数


Python numpy.correlate()函数

numpy.correlate()函数定义了两个一维序列的交叉相关。这个函数计算的是信号处理文本中通常定义的相关性:c_{av}[k] = sum_n a[n+k] * conj(v[n])

语法: numpy.correlate(a, v, mode = ‘valid’)

参数 :
a, v : [array_like] 输入序列。
mode : [{‘valid’, ‘same’, ‘full’}, optional] 请参考convolve文档串。默认为’有效’。

返回: [ndarray] a和v的离散交叉相关。

代码#1:

# Python program explaining
# numpy.correlate() function
       
# importing numpy as geek 
import numpy as geek 
   
a = [2, 5, 7]
v = [0, 1, 0.5]
   
gfg = geek.correlate(a, v)
   
print (gfg)

输出 :

[8.5]

代码#2:

# Python program explaining
# numpy.correlate() function
       
# importing numpy as geek 
import numpy as geek 
   
a = [2, 5, 7]
v = [0, 1, 0.5]
   
gfg = geek.correlate(a, v, "same")
   
print (gfg)

输出 :

[4.5 8.5 7. ]

Python Numpy count_nonzero函数

numpy.count_nonzero()函数计算数组arr中非零值的数量。

语法: numpy.count_nonzero(arr, axis=None)

参数 :
arr : [array_like] 用于计算非零的数组。
axis : [int or tuple, optional] 用于计算非零点的轴或轴的元组。默认为无,意味着非零点将沿着Arr的一个扁平化版本进行计数。

返回 : [int or array of int] 数组中沿给定轴的非零值的数量。否则,将返回数组中非零值的总数。

代码#1:

# Python program explaining
# numpy.count_nonzero() function
  
# importing numpy as geek 
import numpy as geek
  
arr = [[0, 1, 2, 3, 0], [0, 5, 6, 0, 7]]
  
gfg = geek.count_nonzero(arr)
  
print (gfg) 

输出 :

6

代码#2:

# Python program explaining
# numpy.count_nonzero() function
  
# importing numpy as geek 
import numpy as geek
  
arr = [[0, 1, 2, 3, 4], [5, 0, 6, 0, 7]]
  
gfg = geek.count_nonzero(arr, axis = 0)
  
print (gfg) 

输出 :

7

 

Python中的numpy.diff()

numpy.diff(arr[, n[, axis]])函数在我们计算沿给定轴的n阶离散差时使用。一阶差值由out[i] = arr[i+1] – arr[i]沿给定轴给出。如果我们要计算更高的差值,我们要递归地使用diff。

语法: numpy.diff()
参数:
arr : [array_like] 输入阵列。
n : [int, optional] 数值被差异化的次数。
axis : [int, optional] 取差的轴,默认是最后一个轴。
返回: [ndarray]第n个离散差值。其输出与a相同,只是沿轴线的维度要小n。

代码#1:

# Python program explaining
# numpy.diff() method
 
   
# importing numpy
import numpy as geek
 
# input array
arr = geek.array([1, 3, 4, 7, 9])
  
print("Input array  : ", arr)
print("First order difference  : ", geek.diff(arr))
print("Second order difference : ", geek.diff(arr, n = 2))
print("Third order difference  : ", geek.diff(arr, n = 3))

输出:

Input array  :  [1 3 4 7 9]
First order difference  :  [2 1 3 2]
Second order difference :  [-1  2 -1]
Third order difference  :  [ 3 -3]

代码#2:

# Python program explaining
# numpy.diff() method
 
   
# importing numpy
import numpy as geek
 
# input array
arr = geek.array([[1, 2, 3, 5], [4, 6, 7, 9]])
  
print("Input array  : ", arr)
print("Difference when axis is 0 : ", geek.diff(arr, axis = 0))
print("Difference when axis is 1 : ", geek.diff(arr, axis = 1))

输出:

Input array  :  [[1 2 3 5]
 [4 6 7 9]]
Difference with axis 0 :  [[3 4 4 4]]
Difference with axis 1 :  [[1 1 2]
 [2 1 2]]

Python numpy.dtype.kind()函数

numpy.dtype.kind()函数通过识别数据的一般种类来确定字符编码。

语法: numpy.dtype.kind(type)

参数 :
type : [dtype] 输入的数据类型。
返回:通过识别数据的一般种类返回字符编码。

代码#1:

# Python program explaining
# numpy.dtype.kind() function
            
# importing numpy as geek 
import numpy as geek 
  
dtype = geek.dtype('f4')
  
gfg = dtype.kind
  
print (gfg)

输出 :

f

代码#2:

# Python program explaining
# numpy.dtype.kind() function
            
# importing numpy as geek 
import numpy as geek 
  
dtype = geek.dtype('i4')
  
gfg = dtype.kind
  
print (gfg)

输出 :

i

 

Python numpy.dtype.subdtype()函数

numpy.dtype.subdtype()函数如果这个dtype描述了一个子数组,则返回Tuple(item_dtype, shape),否则返回None。

语法: numpy.dtype.subdtype(type)

type : [dtype] 输入的数据类型。
返回 : 如果这个dtype描述了一个子数组,则返回Tuple(item_dtype, shape),否则返回None。

代码#1:

# Python program explaining
# numpy.dtype.subdtype() function
         
# importing numpy as geek 
import numpy as geek 
     
x = geek.dtype('8f')
   
gfg = x.subdtype
   
print (gfg)

输出 :

(dtype('float32'), (8, ))

代码#2:

# Python program explaining
# numpy.dtype.subdtype() function
         
# importing numpy as geek 
import numpy as geek 
     
x = geek.dtype('i2')
   
gfg = x.subdtype
   
print (gfg)

输出 :

None

Python numpy.find_common_type()函数

numpy.find_common_type()函数按照标准的强制规则确定公共类型。

语法: numpy.find_common_type(array_types, scalar_types)

参数 :
array_types : [sequence] 一个代表数组的dtypes或dtype可转换对象的列表。
scalar_types : [序列] 代表标量的dtypes或dtype可转换对象的列表。
返回 : [dtype] 普通数据类型,是数组类型的最大值,忽略标量类型,除非标量_类型的最大值是不同的类型。

代码#1:

# Python program explaining
# numpy.find_common_type() function
            
# importing numpy as geek 
import numpy as geek 
        
gfg = geek.find_common_type([geek.float32], [geek.int64, geek.float64])
      
print (gfg)

输出 :

float32

代码#2:

# Python program explaining
# numpy.find_common_type() function
            
# importing numpy as geek 
import numpy as geek 
        
gfg = geek.find_common_type([geek.float32], [complex])
      
print (gfg)

输出 :

complex128

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

格图素书

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

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

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

打赏作者

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

抵扣说明:

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

余额充值