2020-10-20

it2023-10-29  69

Numpy 基础

numpy是python中基于数组对象的科学计算库。 python拥有以下三大特点: 拥有n维数组对象; 拥有广播功能; 拥有各种科学计算API。

时间日期和时间增量 datetime64是带单位的日期时间类型。 timedelta64表示两个datetime64之间的差。timedelta64也是带单位的,并且和相减运算中的两个datetime64中的较小的单位保持一致。

将numpy的datetime64对象转换为datetime的datetime对象。

import numpy as np import datetime dt64 = np.datetime64('2020-10-20 22:11:45') dt = dt64.astype(datetime.datetime) print(dt, type(dt))

填写不规则系列的numpy日期中的缺失日期?

import numpy as np dates = np.arange('2020-10-20','2020-10-30', 2, np.datetime64) out = [] for date, d in zip(dates, np.diff(dates)): out.extend(np.arrange(date, date + d)) fillin = np.array(out) output = np.hstack([fillin, dates[-1]]) print(output)

得到今天、昨天、明天的日期?

yesterday = np.datetime64('today', 'D') - np.timedelta(1, 'D') today = np.datetime64('today', 'D') tomorrow = np.datetime64(1, 'D')

长度为10的numpy数组,从5开始,在连续的数值之间的步长为3.

import numpy as np start = 5 step = 3 length = 10 a = np.arange(start, start + step * length, step)

数组创建: array()和asarray()都可以将结构数据转化为ndarray,但是array()和asarray()主要区别就是当数据源是ndarray时,array()仍会copy出一个副本,占用新的内存,但不改变dtype时asarray不会。

将本地图像导入并将其转换为numpy数组。

import numpy as np from PIL import Image img1 = Image.open('test.jpg') a = np.array(img1) print(a.shape, a.dtype)
最新回复(0)