Python数据分析之Numpy库的使用详细讲解

本文详细介绍了Python数据分析的重要库Numpy,包括如何安装、创建数组、数组属性、切片索引、维度变换、数组拼接、分割、转置及各种数学函数的使用。重点讲解了数组的创建,如array、arange、随机数生成,并通过实例展示了如何进行多维数组的操作,是学习Numpy库的实用教程。
摘要由CSDN通过智能技术生成

本篇文章目录

一、简介

Numpy是科学计算基础库,提高大量科学计算的功能,比如数据统计,随机数生成等,其提供最核心类型为多维数组类型(ndarray),支持大量的维度数组与矩阵运算,Numpy支持向量处理ndarry对象,提高程序的运算速度。

二、安装

  • pip install numpy

三、数组的创建

3.1 array创建

array函数参数为:array(p_object, dtype=None, copy=True, order=‘K’, subok=False, ndmin=0),下面只介绍常用参数类型。
1、p_object:数组对象
2、dtype:指定转换的数据中的数据类型
3、ndmin:指定维度数

  • 一维数组创建
    一维数组的创建只需传入一个列表即可,输出的类型为numpy.ndarray类型
    示例代码:
import numpy as np

# 使用array函数创建一维数组
x = np.array([1, 2, 3, 4])
print(x)	# [1 2 3 4]
print(type(x))	# <class 'numpy.ndarray'>
  • 二维数组创建
    同一维数组一样,同样传入列表即可。
    注意:传入的列表最外层还有一层中括号,如果不加则会报错。
    示例代码:
import numpy as np

# 使用array函数创建二维数组
y = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])	# 创建二维数组
print(y)
输出结果:
[[1 2 3]
[4 5 6]
[7 8 9]]
print(type(x))	# <class 'numpy.ndarray'>
  • 三维数组创建
    三位数组同二维一样,只不过在最外层又加上了一层中括号。
    示例代码:
import numpy as np

# 使用array函数创建三维数组
z = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])	# 创建三维数组
print(z)
输出结果:
[[[1 2 3]
[4 5 6]
[7 8 9]]]
  • 数组中dtype()的使用。
    dtype可以将创建的数组转换为其他类型。
    注意:转换的只是列表中的数据类型,并不是numpy.ndarray类型。
    示例代码:
import numpy as np

# dtype的使用,转换类型
w = np.array([1, 2, 3], dtype=float)	# 将列表中的数据转换为浮点型,还可以转换为字符串类型
print(w)	#[1. 2. 3.],列表中的数据转换为浮点型
print(type(w))	#<class 'numpy.ndarray'>,返回的类型仍然是ndarray类型
  • ndmin的使用,指定维度数
    ndmin可以指定数组的维度数,单个列表也可以创建出三位数组。但指定的高维数组转换不了低维数组。
    示例代码:一维数组通过ndmin转换为三维数组
import numpy as np

# ndmin的使用,指定维度数
a = np.array([1, 2, 3], dtype=float, ndmin=3)
print(a)	# [[[1. 2. 3.]]]转换为三维数据
print(type(a))	# <class 'numpy.ndarray'>

示例代码:三位数组使用ndmin转换不了低维数组

# ndmin的使用,指定维度数
a = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]], dtype=float, ndmin=1)	# 指定的数组为三维,ndmin为1,输出结果还是三维
print(a)
输出结果:
[[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]]

3.2 arange创建

arange函数的参数:arange([start,] stop[, step,], dtype=None)
1、start:起始值
2、stop:终止值
3、step:步长,默认为1
4、dtype:转换类型,如果不指定,则会使用数据类型
示例代码:

import numpy as np

# 创建1-10的数组
a = np.arange(0, 10, dtype=int)
print(a)
输出结果:
[0 1 2 3 4 5 6 7 8 9]

设置步长代码:

import numpy as np

# 创建1-10的数组
a = np.arange(0, 10, 2, dtype=int)	# 设置步长为2的数组
print(a)
输出结果:
[0 2 4 6 8]

3.3 随机数创建数组

3.3.1 创建随机小数

随机数创建数组采用的是numpy库里面的random()函数。
语法:np.random.random(size=None) 返回的是0.0-1.0之间的随机数,但不包括1.0,size指定随机数的个数。
创建一维数组示例代码:

import numpy as np

# 生成0-1之间的随机数组,共10个数据
a = np.random.random(10)
print(a)	
输出结果:
[0.10646094 0.55262119 0.1376072  0.09820666 0.25679125 0.75362808
0.92731829 0.7927795  0.69050268 0.1210358 ]

创建二维数组时传入的size参数,指定的是数组的行和列
创建二维数组示例代码:

import numpy as np

# 创建二维随机数组
b = np.random.random(size=(2, 3))	# 创建2行3列的随机数组 
print(b)
输出结果:
[[0.37721388 0.37544322 0.21520047]
[0.53715603 0.30601605 0.06256915]]

创建三维数组时传入的size参数为三个。
创建三位数组示例代码:

import numpy as np

# 创建三维随机数组
c = np.random.random(size=(2, 3, 4))	# 创建2个三行四列的数组
print(c)
输出结果:
[[[0.36208984 0.81947471 0.14298442 0.79046203]
  [0.54460578 0.24994409 0.46474527 0.32052213]
  [0.17897466 0.82689056 0.90160973 0.45336997]]

 [[0.45532895 0.8677309  0.09609607 0.51677404]
  [0.57373632 0.27907846 0.70424555 0.84253691]
  [0.57790128 0.20907129 0.49335608 0.87549777]]]

3.3.2 创建随机整数

创建随机整数使用的是randint()函数,参数为:randint(low, high=None, size=None, dtype=‘l’)
low:最小值
high:最大值
size:指定维度数,一维、二维、三维。
dtype:指定数组类型
创建一维随机整数示例代码:

import numpy as np

# 生成一维0-5之间的10个随机整数
a = np.random.randint(0, 5, size=10)	# size为10 ,表示创建一维10个随机整数。
print(a)	
输出结果:
[4 1 0 1 2 0 3 1 0 2]

创建二维数组时size的参数为一个元组,指定行和列
创建二维随机整数示例代码:

import numpy as np

# 创建二维2-8之间的随机数组
b = np.random.randint(2, 8, size=(2, 3))
print(b)	
输出结果:
[[3 6 3]
[3 4 4]]

创建三维随机整数示例代码:

import numpy as np

# 创建三维2-8之间的随机数组
c = np.random.randint(2, 8, size=(2, 3, 4))	# 创建三维数组
print(c)
输出结果:
[[[6 5 6 4]
  [2 3 4 5]
  [5 5 7 5]]

 [[5 2 2 5]
  [3 4 5 5]
  [3 3 6 3]]]

3.3.3 创建标准正态分布数组

创建标准正态分布使用的时randn()方法。
randn(d0, d1, …, dn),返回一个或一组样本,具有标准正态分布(期望为0,均值为1)
dn表示维度数
创建一维标准正态分布示例代码:

import numpy as np

# 创建一维标准正态分布
a = np.random.randn(6)
print(a)
输出结果:
[ 0.58850012 -0.88685655 -1.00077417  2.50602404 -0.69627562  1.19382079]

创建二维标准正态分布:

import numpy as np

# 创建一维标准正态分布
b = np.random.randn(2, 3)	# 指定行和列
print(b)
输出结果:
[[-1.59507936  2.00741908 -0.19886889]
 [-0.78522123 -0.94702049  0.10118063]]

创建三维标准正态分布:

import numpy as np

# 创建一维标准正态分布
c = np.random.randn(2, 3, 4)
print(c)
输出结果:
[[[ 2.25392453 -1.03092967  0.62695321 -1.59550922]
  [ 0.21379353 -1.14740262  1.39019012  0.01449549]
  [ 1.29115361  0.01583029 -1.53528833 -1.65218213]]

 [[ 1.36693468  1.27192511 -0.36759254 -0.67529018]
  [ 0.12840871 -0.40780793  0.3604168   0.88594743]
  [ 0.57778304 -2.42864619 -0.5829699  -0.29083045]]]

3.3.4 创建指定期望与方差的正态分布数组

创建指定期望与方差时的数组,采用的是normal()函数。
参数为:normal(loc=0.0, scale=1.0, size=None)
loc:指定期望
scale:指定方差
size:指定维度
创建默认期望与方差的一维数组示例代码:

import numpy as np

# 创建默认期望与均值的一维数组
a = np.random.normal(size=5)  # 默认期望loc=0.0,方差scale=1.0
print(a)
输出结果:
[ 0.68913158 -0.24866231  0.93683785 -0.33245719  1.56009623]

创建指定期望与方差的二维数组示例代码:

import numpy as np

# 创建默认期望与均值的一维数组
a = np.random.normal(loc=2, scale=3, size=(2, 3))  # 指定期望为2,方差为3的二维数组
print(a)
输出结果:
[[ 6.13038568  4.63502362  1.5378486 ]
 [ 0.47091329  2.1003756  -0.93129833]]

四、ndarray对象的属性

Numpy最重要的一个特点是其N维数组对象ndarray,它是一系列同类型数据的集合,以0为下标开始进行集合中元素的索引。
ndarray对象是用于存放同类型元素的多维数组,ndarray中的每个元素在内存中都有相同存储的大小的区域。
ndarray内部由以下内容组成:

  • 一个指向数据(内存或内存映射文件中的一块数据)的指针。
  • 数据类型或dtype,描述在数组中固定大小值的格子。
  • 一个表示数组形状(shape)的元组,表示各维度大小的元组。
    在这里插入图片描述

五、其他形式创建数组

5.1 zeros创建数组

zeros创建语法:zeros(shape, dtype=float, order=‘C’)
Return a new array of given shape and type, filled with zeros.返回一个数组,给定形状和类型,以0填充。
示例代码:
zreos创建一维数组

import numpy as np

# zeros函数创建一维列表
a = np.zeros(5)  # 默认数据为浮点型,可指定数据类型
print(a)
输出结果:
[0. 0. 0. 0. 0.]

zreos创建二维数组

import numpy as np

# zeros函数创建二维列表
b = np.zeros((2, 3), dtype=int)  # 默认数据为浮点型,可指定数据类型
print(b)
输出结果:
[[0 0 0]
 [0 0 0]]

5.2 ones创建数组

ones参数:ones(shape, dtype=None, order=‘C’)
Return a new array of given shape and type, filled with ones.返回一个数组,给定形状和类型,以1填充。
ones绘制一维数组示例代码:

import numpy as np

c = np.ones(5, dtype=int)  # 默认数据为浮点型,可指定数据类型
print(c)
输出结果:
[1 1 1 1 1]

ones绘制二维数组示例代码:

import numpy as np

# ones绘制二维数组
d = np.ones((3, 4), dtype=int)  # 默认数据为浮点型,可指定数据类型
print(d)
输出结果:
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]

5.3 empty创建数组

empty()方法用来创建一个指定形状(shape)、数据类型(dtype)且为初始化的数组,里面的元素的值是之前内存的值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值