python中numpy模块的around方法_numpy模块的基本操作

numpy模块基本使用方法

1.导入模块

import numpy as np

2.创建数组

np.array([ ])/np.array([[ ],[ ]]):能创建一维,二维或者多维数组

np.arange(start,stop,step):只能创建一维数组

np.linspace(start,stop,num=50):只能创建一维数组

np.random系列:

np.random.rand():创建随机数为0-1的数组

np.random.randint(low,high,size):创建一个size大小的数组

np.random.randn():创建随机数组

可以使用np.random.seed()来固定数组元素

其他(不常用):

np.ones(shape)

np.ones_like(a)

np.zeros(shape)

np.zeros_like(a)

np.eye():创建单位矩阵

np.array([1,2,3])

array([1, 2, 3])

np.arange(1,10,step=2)

array([1, 3, 5, 7, 9])

np.linspace(1,10,num=20)

array([ 1. , 1.47368421, 1.94736842, 2.42105263, 2.89473684,

3.36842105, 3.84210526, 4.31578947, 4.78947368, 5.26315789,

5.73684211, 6.21052632, 6.68421053, 7.15789474, 7.63157895,

8.10526316, 8.57894737, 9.05263158, 9.52631579, 10. ])

np.random.seed(3)

print(np.random.rand(2,3))

print(np.random.randint(1,10,size=(2,5)))

print(np.random.randn(2,3))

[[0.5507979 0.70814782 0.29090474]

[0.51082761 0.89294695 0.89629309]]

[[6 8 7 1 5]

[8 9 2 7 3]]

[[-0.34733018 0.06629873 -1.62758528]

[ 0.39788586 -1.62915743 -0.63893553]]

3.数据的属性

arr.shape:数组的形状,可以通过arr.reshape(shape)来改变数组的形状

arr.dtype:数组元素的类型,可以通过arr.dtype=""来改变数组元素的类型

arr.ndim:数组的维度

arr.size:数组元素的个数

type(arr):数组类型

4.ndarray的数据类型

bool 用一个字节存储的布尔类型(True或False)

inti 由所在平台决定其大小的整数(一般为int32或int64)

int8 一个字节大小,-128 至 127

int16 整数,-32768 至 32767

int32 整数,-2 ** 31 至 2 ** 32 -1

int64 整数,-2 ** 63 至 2 ** 63 - 1

uint8 无符号整数,0 至 255

uint16 无符号整数,0 至 65535

uint32 无符号整数,0 至 2 ** 32 - 1

uint64 无符号整数,0 至 2 ** 64 - 1

float16 半精度浮点数:16位,正负号1位,指数5位,精度10位

float32 单精度浮点数:32位,正负号1位,指数8位,精度23位

float64或float 双精度浮点数:64位,正负号1位,指数11位,精度52位

complex64 复数,分别用两个32位浮点数表示实部和虚部

complex128或complex 复数,分别用两个64位浮点数表示实部和虚部

arr = np.random.randn(4,5)

print(arr.shape)

print(arr.ndim)

print(arr.dtype)

print(arr.size)

print(type(arr))

(2, 3)

2

float64

6

5.数组的索引及切片

基本的索引及切片arr[行,列]

取前三行的数据:arr[0:3]或者arr[0:3,:]

取前两列的数据:arr[:,0:3]

取前三行前两列的数据:arr[0:3,0:2]

布尔型的索引及切片

arr

array([[ 0.2613125 , -0.69459727, -2.02469194, 0.84595316, -0.47455326],

[-1.39576555, -1.73278446, -0.23322423, -0.36848667, -1.8131661 ],

[ 0.25453554, 1.06394904, -0.98961218, -0.96505109, -1.42170273],

[-0.21899587, 0.0673387 , -1.43275264, -0.03472461, -0.87321135]])

#取前三行的数据

arr[0:3]

arr[0:3,:]

array([[ 0.2613125 , -0.69459727, -2.02469194, 0.84595316, -0.47455326],

[-1.39576555, -1.73278446, -0.23322423, -0.36848667, -1.8131661 ],

[ 0.25453554, 1.06394904, -0.98961218, -0.96505109, -1.42170273]])

#取前两列的数据

arr[:,0:2]

array([[ 0.2613125 , -0.69459727],

[-1.39576555, -1.73278446],

[ 0.25453554, 1.06394904],

[-0.21899587, 0.0673387 ]])

#取前三行前两列的数据

arr[0:3,0:2]

array([[ 0.2613125 , -0.69459727],

[-1.39576555, -1.73278446],

[ 0.25453554, 1.06394904]])

ar = np.arange(12).reshape(3,4)

i = np.array([True,False,True])

j = np.array([True,True,False,False])

print(ar)

print(i)

print(j)

print(ar[i,:]) # 在第一维度做判断,只保留True,这里第一维度就是行,ar[i,:] = ar[i](简单书写格式)

print(ar[:,j]) # 在第二维度做判断,这里如果ar[:,i]会有警告,因为i是3个元素,而ar在列上有4个

# 布尔型索引:以布尔型的矩阵去做筛选

m = ar > 5

print(m) # 这里m是一个判断矩阵

print(ar[m]) # 用m判断矩阵去筛选ar数组中>5的元素

[[ 0 1 2 3]

[ 4 5 6 7]

[ 8 9 10 11]]

[ True False True]

[ True True False False]

[[ 0 1 2 3]

[ 8 9 10 11]]

[[0 1]

[4 5]

[8 9]]

[[False False False False]

[False False True True]

[ True True True True]]

[ 6 7 8 9 10 11]

6.级联操作

将多个numpy数组进行横向或者纵向的拼接

axis轴向的理解

0:列

1:行

arr = np.random.randint(1,16,size=(3,4))

arr

array([[ 2, 6, 2, 9],

[ 9, 8, 14, 1],

[ 1, 10, 2, 8]])

np.concatenate((arr,arr),axis=1)

array([[ 2, 6, 2, 9, 2, 6, 2, 9],

[ 9, 8, 14, 1, 9, 8, 14, 1],

[ 1, 10, 2, 8, 1, 10, 2, 8]])

7.常用的聚合操作

sum求和,max最大值,min最小值,mean平均值

arr

array([[ 2, 6, 2, 9],

[ 9, 8, 14, 1],

[ 1, 10, 2, 8]])

print(arr.sum())#计算全部数组的和

print(arr.sum(axis=0))#计算每一列的和

print(arr.sum(axis=1))#计算每一行的和

#max,min,mean同sum

72

[12 24 18 18]

[19 32 21]

8.常用的统计函数

np.amin() 和 np.amax(),用于计算数组中的元素沿指定轴的最小、最大值。

np.ptp():计算数组中元素最大值与最小值的差(最大值 - 最小值)。

np.median() 函数用于计算数组中元素的中位数(中值)

标准差std():标准差是一组数据平均值分散程度的一种度量。

公式:std = sqrt(mean((x - x.mean())**2))

方差var():统计中的方差(样本方差)是每个样本值与全体样本值的平均数之差的平方值的平均数,即 mean((x - x.mean())** 2)。换句话说,标准差是方差的平方根。

arr

array([[ 2, 6, 2, 9],

[ 9, 8, 14, 1],

[ 1, 10, 2, 8]])

np.ptp(arr,axis=0)

array([ 8, 4, 12, 8])

np.median(arr,axis=0)

array([2., 8., 2., 8.])

np.std(arr)

arr.std()

4.123105625617661

np.var(arr)

arr.var()

17.0

9.常用的数学函数

NumPy 提供了标准的三角函数:np.sin()、np.cos()、np.tan()

np.around(a,decimals) 函数返回指定数字的四舍五入值。

参数说明:

a: 数组

decimals: 舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置

np.sin(arr)

array([[ 0.90929743, -0.2794155 , 0.90929743, 0.41211849],

[ 0.41211849, 0.98935825, 0.99060736, 0.84147098],

[ 0.84147098, -0.54402111, 0.90929743, 0.98935825]])

np.around(arr,decimals=-1)

array([[ 0, 10, 0, 10],

[10, 10, 10, 0],

[ 0, 10, 0, 10]])

原文链接:https://blog.csdn.net/weixin_43816759/article/details/108778390

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Numerical Python by Robert Johansson shows you how to leverage the numerical and mathematical capabilities in Python, its standard library, and the extensive ecosystem of computationally oriented Python libraries, including popular packages such as NumPy, SciPy, SymPy, Matplotlib, Pandas, and more, and how to apply these software tools in computational problem solving. Python has gained widespread popularity as a computing language: It is nowadays employed for computing by practitioners in such diverse fields as for example scientific research, engineering, finance, and data analytics. One reason for the popularity of Python is its high-level and easy-to-work-with syntax, which enables the rapid development and exploratory computing that is required in modern computational work. After reading and using this book, you will have seen examples and case studies from many areas of computing, and gained familiarity with basic computing techniques such as array-based and symbolic computing, all-around practical skills such as visualisation and numerical file I/O, general computat ional methods such as equation solving, optimization, interpolation and integration, and domain-specific computational problems, such as differential equation solving, data analysis, statistical modeling and machine learning. Specific topics that are covered include: How to work with vectors and matrices using NumPy How to work with symbolic computing using SymPy How to plot and visualize data with Matplotlib How to solve linear and nonlinear equations with SymPy and SciPy How to solve solve optimization, interpolation, and integration problems using SciPy How to solve ordinary and partial differential equations with SciPy and FEniCS How to perform data analysis tasks and solve statistical problems with Pandas and SciPy How to work with statistical modeling and machine learning with statsmodels and scikit-learn How to handle file I/O using HDF5 and other common file formats for numerical data How to optimize Python code using Numba and Cython

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值