Python数据分析之numpy数组(一)

创建数组的方式

  • np.array():传入列表、元组、数组或其他序列类型
  • 创建全0数组- np.zeros():传入数字或元组
  • 创建全1数组-np.ones():传入数字或元组
  • 创建单位矩阵-np.eye()np.identity()
    1.np.eye(2)np.eye(2, 3)
    2.np.identity(2)
  • np.empty():传入数字或元组;只分配内存空间,不填充任何值。
  • np.linspace()
import numpy as np

# np.linspace(start, stop, num), 默认产生的数据包含stop;
np.linspace(1, 5, 5)

输出:

array([1., 2., 3., 4., 5.])

输入:

# 设置endpoint=False后,不包含stop;num为产生的数组中的元素的个数
np.linspace(1, 5, 2, endpoint=False)

输出:

array([1., 3.])

数组的属性

np.shape–m行n列;np.ndim–m行;np.dtype–数据类型

  1. arr.shape:返回数组的(行数, 列数)
  2. arr.ndim:返回数组的行数
  3. arr.dtype:返回数组中数据的类型
  4. arr.size:返回数组元素的个数
# 输入
a = np.zeros((2, 3))
a
# 输出
array([[0., 0., 0.],
       [0., 0., 0.]])
# 输入
a.shape
# 输出
(2, 3)

# 输入
a.ndim
# 输出
2

# 输入
a.dtype
# 输出
dtype('float64')

# 输入
a.size
# 输出
6

数组中产生随机数据的几种方法

  1. np.random.rand():填充由均匀分布得到的随机样本
  2. np.random.randn():填充由标准正态分布得到的随机样本
  3. np.random.randint():产生随机整数
  4. np.random.uniform():产生随机小数
  5. np.random.normal():从正态分布中随机抽取样本
  6. np.random.seed():指定随机种子后,再调用其他方法时,得到的随机数据相同。
# 输入
np.random.rand(2, 3)
# 输出
array([[0.68491142, 0.85887755, 0.92849893],
       [0.15450491, 0.70758483, 0.18847558]])

# 输入
np.random.randn(2, 3)
# 输出
array([[ 1.62434536, -0.61175641, -0.52817175],
       [-1.07296862,  0.86540763, -2.3015387 ]])

# 输入, 5 取不到
np.random.randint(1, 5, (2, 3))
# 输出
array([[3, 2, 3],
       [1, 4, 1]])

# 输入, 4 取不到
np.random.uniform(1, 4, size=(2, 3))
# 输出
array([[3.90488148, 3.98437347, 1.15750283],
       [2.78057645, 3.09223221, 3.30434653]])

# 输入
mu, sigma = 0, 0.1
np.random.normal(mu, sigma, size=(2, 3))
# 输出
array([[ 0.08332937, -0.01786283,  0.01910761],
       [-0.05188648, -0.13277132, -0.1204348 ]])

# 输入,加入np.random.seed(1)后,每次输出都相同。
# np.random.seed(s)
np.random.seed(1)
np.random.normal(0, 0.1, size=(2, 3))
# 输出
array([[ 0.16243454, -0.06117564, -0.05281718],
       [-0.10729686,  0.08654076, -0.23015387]])

多维数组变一维数组的方法

第一种:

# 二维变一维
# 输入
arr = np.array([[1, 2, 3, 4],[5, 6, 7, 8]])
arr
# 输出
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

# 输入,这种改变后,可以直接输出结果的语句,原数据一般不会变化,即arr仍然是二维的。
arr.reshape(arr.shape[0]*arr.shape[1])
# 输出
array([1, 2, 3, 4, 5, 6, 7, 8])

#若是n维,需要使用arr.shape[0]*arr.shape[1]*...*arr.shape[n-1]

第二种

# 不需要知道原来的维数,直接使用flatten方法
arr.flatten()

数据类型及修改

  • 查看数组中数据的类型
# 输入
arr = np.array([[1, 2, 3, 4],[5, 6, 7, 8]])
arr.dtype
# 输出
dtype('int32')
  • 创建数组时,参数dtype用于定义数据类型
# 输入
arr = np.array([[1, 2, 3, 4],[5, 6, 7, 8]], dtype='float')
arr.dtype
# 输出
dtype('float64')
  • 调整数据类型
# 输入
arr = np.array([[1, 2, 3, 4],[5, 6, 7, 8]], dtype='float')
arr1 = arr.astype('int')
arr1.dtype
# 输出
dtype('int32')

# 输入
arr = np.array([[1, 2, 3, 4],[5, 6, 7, 8]], dtype='float')
arr1 = arr.astype('int8')
arr1.dtype
# 输出
dtype('int8')

nan和inf产生的原因

B站老师讲的

  • 出现nan的原因
    ① 读取本地文件,数据是float类型的,有缺失时,会出现。
    ② 不合适的计算:inf-inf
# 输入
from numpy import nan, inf
inf - inf
# 输出
nan

  • 产生inf的原因
    非0数字除以0
# 输入
arr = np.arange(4)
arr
# 输出
array([0, 1, 2, 3])

# 输入
arr / 0
# 输出
array([nan, inf, inf, inf])

从最后的输出可以看出,numpy数组中 0除以0=nan非0除以0=inf

因为nan容易产生的一个小error

arr = np.arange(4)
arr[0] = nan

关于nan的error

这是因为nan是float类型,需要将arr变为float类型后,再操作。

# 输出
arr = np.arange(4).astype('float')
arr[0] = nan
arr
# 输出
array([nan,  1.,  2.,  3.])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值