numpy TASK3 数组的操作(变形)

1 时间日期和时间增量

1.1 datetime64

dateime64 是常见数据类型中的日期时间类型,datetime64 的常见日期单位如下:
在这里插入图片描述

1.2 timedelta

timedelta 是常见数据类型中表示两个时间之间的间隔。timedelta64 表示两个 datetime64 之间的差。timedelta64 也是带单位的。并且在生成timedelta 时,要注意年月这两个单位不能和天时分秒这些单位进行换算,因为存在闰年和闰月的情况,一年具体多少天不确定,一个月具体多少个小时也不确定。

import numpy as np
a = np.timedelta64(1, 'Y')
b = np.timedelta64(a, 'M')
print(a) # 1 years
print(b) # 12 months
c = np.timedelta64(1, 'h')
d = np.timedelta64(c, 'm')
print(c) # 1 hours
print(d) # 60 minutes
print(np.timedelta64(a, 'D'))
# TypeError: Cannot cast NumPy timedelta64 scalar from metadata [Y] to [D] according to the rule 'same_kind'
print(np.timedelta64(b, 'D'))
# TypeError: Cannot cast NumPy timedelta64 scalar from metadata [M] to [D] according to the rule 'same_kind'

numpy.busday_offset(dates, offsets, roll=‘raise’, weekmask=‘1111100’, holidays=None, busdaycal=None, out=None)函数的目的是查看日期是否为有效的工作日。 首先根据滚动规则将日期调整到某个有效日期,然后将偏移量应用到以有效日期计算的给定日期。

  • forward表示向前取一个工作日
  • backward 表示向后取一个工作日
#查看当前日期是否为有效的工作日,若不是则通过参数设置偏移量offsets表示的是此日期之后的第几个工作日
a=np.busday_offset('2020-07-10', offsets=0)
print(a)
b=np.busday_offset('2020-07-13', offsets=1)
print(b)
c=np.busday_offset('2020-07-14', offsets=1)
print(c)
d=np.busday_offset('2020-07-10', offsets=3)
print(d)
e=np.busday_offset('2020-07-11', offsets=0,roll='forward') #roll 参数表示方向
f=np.busday_offset('2020-07-11', offsets=0,roll='backward')
print(e)
print(f)
2020-07-10
2020-07-14
2020-07-15
2020-07-15
2020-07-13
2020-07-10

numpy.busday_count(begindates, enddates, weekmask=‘1111100’, holidays=[], busdaycal=None, out=None) 。函数的目的是计算计算开始日期和结束日期之间的有效天数(工作日),不包括结束日期。

#查看两个日期之间的工作日数量
begindates=np.datetime64('2020-07-10')
enddates=np.datetime64('2020-07-20')
g=np.busday_count(begindates, enddates)
h=np.busday_count(enddates, begindates)
print(g)
print(h)
6
-6

2 数组操作

2.1 更改形状

  • numpy.ndarray.shape 表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
  • numpy.ndarray.flat 将数组转换为一维的迭代器,可以用for访问数组每一个元素。
  • numpy.ndarray.flatten([order=‘C’]) 将数组的副本转换为一维数组,并返回。注意两者的区别,返回的性质不同! order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘k’ – 元素在内存中的出现顺序。(简记)
  • numpy.ravel(a, order=‘C’) Return a contiguous flattened array. ravel() 返回的是视图
  • numpy.reshape(a, newshape[, order=‘C’]) 在不更改数据的情况下为数组赋予新的形状。reshape() 函数当参数newshape = [rows,-1] 时,将根据行数自动确定列数,当参数newshape = -1 时,表示将数组降为一维。
#numpy.ndarray.flat
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = x.flat
print(y)
for i in y:
print(i, end=' ')
<numpy.flatiter object at 0x0000000006CE3C00>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 [11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 35]
 
 #numpy.ndarray.flatten() 
y=x.flatten(order='C')
print(y)
y=x.flatten(order='F')   
print(y)

[11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30 35]

y = np.ravel(x)
print(y)
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35]

#numpy.reshape
x = np.arange(12)
y = np.reshape(x, [3, 4])
print(y.dtype) # int32
print(y)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

2.2数组转置

y = x.T
y = np.transpose(x)

  • numpy.transpose(a, axes=None) Permute the dimensions of an array.(转置数组)
  • numpy.ndarray.T Same as self.transpose() , except that self is returned if self.ndim < 2 .
import numpy as np
x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)
y = x.T
print(y)
y1=np.transpose(x)
print(y1)
[[5.25 4.3  8.9  1.15 6.53]
 [8.73 8.21 6.29 3.02 7.37]
 [9.76 6.82 1.22 0.69 7.71]
 [1.5  4.81 0.11 9.15 9.66]
 [8.82 9.2  8.86 1.05 0.07]]
[[5.25 8.73 9.76 1.5  8.82]
 [4.3  8.21 6.82 4.81 9.2 ]
 [8.9  6.29 1.22 0.11 8.86]
 [1.15 3.02 0.69 9.15 1.05]
 [6.53 7.37 7.71 9.66 0.07]]
[[5.25 8.73 9.76 1.5  8.82]
 [4.3  8.21 6.82 4.81 9.2 ]
 [8.9  6.29 1.22 0.11 8.86]
 [1.15 3.02 0.69 9.15 1.05]
 [6.53 7.37 7.71 9.66 0.07]]

2.3更改维度

  • numpy.newaxis = None None 的别名,对索引数组很有用,用于创建一个数组后用于增加一个维度。
  • numpy.squeeze(a, axis=None) 从数组的形状中删除单维度条目,即把shape中为1的维度去掉。a 表示输入的数组;axis 用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
import numpy as np
x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape)  # (8,)
print(x) # [1 2 9 4 5 6 7 8]
y = x[np.newaxis, :]
print(y.shape) # (1, 8)
print(y) # [[1 2 9 4 5 6 7 8]]
y = x[:, np.newaxis]
print(y.shape) # (8, 1)
print(y)
[[1]
[2]
[9]
[4]
[5]
[6]
[7]
[8]]

import numpy as np
import matplotlib.pyplot as plt
x = np.array([[1, 4, 9, 16, 25]])
x = np.squeeze(x)
print(x.shape) # (5, )
plt.plot(x)
plt.show()

在这里插入图片描述

2.4 数组组合拆分

组合

  • numpy.concatenate((a1, a2, …), axis=0, out=None) 连接沿现有轴的数组序列
  • numpy.stack(arrays, axis=0, out=None)沿着新的轴加入一系列数组(stack为增加维度的拼接)
  • hstack(),vstack() 分别表示水平和竖直的拼接方式。在数据维度等于1时,比较特殊。而当维度大于或等于2时,它们的作用相当于concatenate ,用于在已有轴上进行操作。

拆分

  • numpy.split(ary, indices_or_sections, axis=0)
  • numpy.vsplit(ary, indices_or_sections)垂直切分是把数组按照高度切分
  • numpy.hsplit(ary, indices_or_sections)水平切分是把数组按照宽度切分。

y = np.split(x, [1, 3])

2.5 数组平铺

  • numpy.tile(A, reps)将原矩阵横向、纵向地复制。
  • numpy.repeat(a, repeats, axis=None) Repeat elements of an array.
    a. axis=0 ,沿着y轴复制,实际上增加了行数。
    b. axis=1 ,沿着x轴复制,实际上增加了列数。
    c. repeats ,可以为一个数,也可以为一个矩阵。
    d. axis=None 时就会flatten当前矩阵,实际上就是变成了一个行向量。
    查找数组的唯一元素:
  • numpy.unique(ar, return_index=False, return_inverse=False,return_counts=False, axis=None)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值