0%

pandas.to_datetime()

pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, format=None, exact=True, unit=None, infer_datetime_format=False, origin='unix', cache=True)[source]

Parameters

  • arg int, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-like

    The object to convert to a datetime

  • format str, default None

The strftime to parse time, eg “%d/%m/%Y”, note that “%f” will parse all the way up to nanoseconds.


该函数可以接受一个series,可以接受一个dateFrame。如果不确定它是否可以以默认的格式去解析你的时间,format参数可以不传递。

1
2
3
4
5
6
7
df = pd.DataFrame({'year': [2015, 2016],
... 'month': [2, 3],
... 'day': [4, 5]})
>>> pd.to_datetime(df)
0 2015-02-04
1 2016-03-05
dtype: datetime64[ns]

上面的这种产生datetime的方式,在创建dateframe的时候,可以指定缩写或者缩写的复数形式,其他形式不接受:[‘year’, ‘month’, ‘day’, ‘minute’, ‘second’, ‘ms’, ‘us’, ‘ns’])

pandas.to_timedelta()

timedelta是两个时间之间的差值,该函数可以帮助我们求两个timestamp之间的差是多少(单位可以是days,hours,minutes,seconds

Parameters

  • arg str, timedelta, list-like or Series

  • unit str, optional

    Denotes the unit of the arg for numeric arg. Defaults to "ns".

    Possible values:

    • ‘W’
    • ‘D’ / ‘days’ / ‘day’
    • ‘hours’ / ‘hour’ / ‘hr’ / ‘h’
    • ‘m’ / ‘minute’ / ‘min’ / ‘minutes’ / ‘T’
    • ‘S’ / ‘seconds’ / ‘sec’ / ‘second’
    • ‘ms’ / ‘milliseconds’ / ‘millisecond’ / ‘milli’ / ‘millis’ / ‘L’
    • ‘us’ / ‘microseconds’ / ‘microsecond’ / ‘micro’ / ‘micros’ / ‘U’
    • ‘ns’ / ‘nanoseconds’ / ‘nano’ / ‘nanos’ / ‘nanosecond’ / ‘N’

这里如果传入的是str,是不允许再传入unit参数了,不然会报错。

1
2
3
4
5
6
7
8
>> pd.to_timedelta('15days 2hours')
Timedelta('15 days 02:00:00')

>> pd.to_timedelta('1 days 06:05:01.00003')
Timedelta('1 days 06:05:01.000030')

>> pd.to_timedelta(4,unit='days')
Timedelta('4 days 00:00:00')

Series.dt()

1
2
3
4
5
6
7
8
9
10
11
>> seconds_series = pd.Series(pd.date_range("2000-01-01", periods=3, freq="s"))
seconds_series
0 2000-01-01 00:00:00
1 2000-01-01 00:00:01
2 2000-01-01 00:00:02
dtype: datetime64[ns]
>> seconds_series.dt.second
0 0
1 1
2 2
dtype: int64

dt是Series的一个方法,当调用dt时,Series中必须是timestamp的格式。

当调用完dt后可以获取时间的具体年份等信息:

1
2
3
4
5
6
7
8
9
seconds_series.dt.date # 2000-01-01
seconds_series.dt.hour # 00
seconds_series.dt.quarter # 返回第几季度
seconds_series.dt.time # 00:00:00
seconds_series.dt.year # 2000
seconds_series.dt.month # 01
seconds_series.dt.day # 01
seconds_series.dt.weekday # 返回一个0-6的数,0表示周一,6表示周日
seconds_series.dt.dayname() # 会返回星期的名字:Monday

有的时候我们想获取某一天是全年中的第几周,这时候weekday就不管用了,此时采用:

1
dseries.dt.isocalendar()['week'] 

count()

不管是Series还是DateFrame,两个类都有这个方法。

对于DateFrame,返回的是每一列中非空的元素的个数,可以指定是以行去统计还是以列去统计。

对于Series,返回的是Series中非空元素的个数。

对于groupby,返回的是每一组中元素的个数,这里的个数不包含空字段。

value_counts()

对于DateFrame,返回的是unique行的个数,这里的unique行指的是只要其中一个column的值不一样就算不一样的行。

对于Series,返回的是每一个unique元素的个数,也就是每一个unique的元素,它会统计它在Series中出现了多少次。

unique()

对于Series,返回的是所有unique的元素们,如果你想统计所有不一样的元素有多少个,可以统计list中元素的个数,如:

1
len(df['a'].unique().tolist())  

配置云服务器

系统 : ubuntu 18.04

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 更新所有环境
sudo apt update

# 安装开发所需的基本包
sudo apt install build-essential

# 安装cuda.这里会连同显卡驱动一起安装
wget https://developer.download.nvidia.com/compute/cuda/11.5.1/local_installers/cuda_11.5.1_495.29.05_linux.run
sudo sh cuda_11.5.1_495.29.05_linux.run



# 去anaconda官方复制miniconda的下载链接
wget https://repo.anaconda.com/miniconda/Miniconda3-py38_4.10.3-Linux-x86_64.sh

# 进入conda环境
bash

之后就继续用pip安装所需要的包
apt-get install python3.8

这里需要知道,如果我们在云端配置的环境,需要将云端的jupyter notebook的运行端口映射到本地来,可以这样做:

1
ssh -L8888:localhost:8888 ubuntu@100.20.65.33

如果使用aws配置远端服务器,在本地连接时使用:

$ chomd 400 Downloads/d2l.pem

$ ssh -i Downloads/d2l.pem ubuntu@54.245.23.40

以上两条命令中将密钥path和ip地址都替换成自己的。

docker中的tensorflow-gpu配置

首先在宿主机(本机)安装好NVIDIA GPU的驱动程序,然后对于每一个容器都要各自安装对应版本的cudacudnn

下载TensorFlow Docker镜像

官方 TensorFlow Docker 映像位于 tensorflow/tensorflow Docker Hub 代码库中。映像版本按照以下格式进行标记

标记 说明
latest TensorFlow CPU 二进制映像的最新版本。(默认版本)
nightly TensorFlow 映像的每夜版。(不稳定)
version 指定 TensorFlow 二进制映像的版本,例如:2.1.0
devel TensorFlow master 开发环境的每夜版。包含 TensorFlow 源代码。
custom-op 用于开发 TF 自定义操作的特殊实验性映像。详见此处

每个基本标记都有会添加或更改功能的变体:

标记变体 说明
tag-gpu 支持 GPU 的指定标记版本。(详见下文
tag-jupyter 针对 Jupyter 的指定标记版本(包含 TensorFlow 教程笔记本)

您可以一次使用多个变体。例如,以下命令会将 TensorFlow 版本映像下载到计算机上:

1
2
3
docker pull tensorflow/tensorflow                     # latest stable release
docker pull tensorflow/tensorflow:devel-gpu # nightly dev release w/ GPU support
docker pull tensorflow/tensorflow:latest-gpu-jupyter # latest release w/ GPU support and Jupyter

注意如果要用gpu版本的tensorflow,需要pull带有gpu tag的镜像. docker pull tensorflow/tensorflow:latest-gpu。如果不带gpu标签,会默认下载CPU版本的tensorflow。

验证tensorflow gpu

查看是否有GPU:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import tensorflow as tf

gpu_device_name = tf.test.gpu_device_name()
print(gpu_device_name)

# GPU是否可用,返回True或者False
tf.test.is_gpu_available()

from tensorflow.python.client import device_lib

# 列出所有的本地机器设备
local_device_protos = device_lib.list_local_devices()
# 打印
# print(local_device_protos)

# 只打印GPU设备
[print(x) for x in local_device_protos if x.device_type == 'GPU']

docker中安装多种cuda版本并切换

cuda官网下载所需版本,以.run结尾。楼主系统为linux

进入到放置 cuda_9.0.176_384.81_linux.run 的目录:

1
2
sudo chmod +x cuda_9.0.176_384.81_linux.run # 为 cuda_9.0.176_384.81_linux.run 添加可执行权限
./cuda_9.0.176_384.81_linux.run # 安装 cuda_9.0.176_384.81_linux.run

在安装过程中截取其中比较重要的几个选择:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Do you accept the previously read EULA?
accept/decline/quit: accept

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 384.81?
(y)es/(n)o/(q)uit: n # 如果在这之前已经安装好更高版本的显卡驱动就不需要再重复安装,如果需要重复安装就选择 yes,此外还需要关闭图形界面。

Install the CUDA 9.0 Toolkit?
(y)es/(n)o/(q)uit: y

Enter Toolkit Location
[ default is /usr/local/cuda-9.0 ]: # 一般选择默认即可,也可以选择安装在其他目录,在需要用的时候指向该目录或者使用软连接 link 到 /usr/local/cuda。

/usr/local/cuda-9.0 is not writable.
Do you wish to run the installation with 'sudo'?
(y)es/(n)o: y

Please enter your password:
Do you want to install a symbolic link at /usr/local/cuda? # 是否将安装目录通过软连接的方式 link 到 /usr/local/cuda,yes or no 都可以,取决于你是否使用 /usr/local/cuda 为默认的 cuda 目录。
(y)es/(n)o/(q)uit: n

Install the CUDA 9.0 Samples?
(y)es/(n)o/(q)uit: n

选择的汇总:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Driver:   Not Selected
Toolkit: Installed in /usr/local/cuda-9.0
Samples: Not Selected

Please make sure that
- PATH includes /usr/local/cuda-9.0/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-9.0/lib64, or, add /usr/local/cuda-9.0/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-9.0/bin

Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-9.0/doc/pdf for detailed information on setting up CUDA.

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 9.0 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
sudo <CudaInstaller>.run -silent -driver
安装完成后可以在 /usr/local 目录下看到:

1
2
3
cuda-11.1 # 之前安装的cuda-11.1 
cuda-9.0 # 刚刚安装的cuda-9.0
cuda # cuda-10.0 的软连接

多版本切换:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#在切换cuda版本时
rm -rf /usr/local/cuda#删除之前创建的软链接
sudo ln -s /usr/local/cuda-8.0/ /usr/local/cuda/
nvcc --version #查看当前 cuda 版本

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Mon_Jan_23_12:24:11_CST_2017
Cuda compilation tools, release 8.0, V8.0.62

#cuda8.0 切换到 cuda9.0
rm -rf /usr/local/cuda
sudo ln -s /usr/local/cuda-9.0/ /usr/local/cuda/
nvcc --version

上面的前提是linux系统的环境变量中(~./bashrc文件)cuda的路径是/usr/local/cuda

tensorflow_decision_forests使用

我一开始pull了tensorflow-gpu版本的docker环境,想用一下tensorflow的tensorflow_decision_forests库,该库是随机森林的集合库,内有很多算法可以用。官方使用document在[https://www.tensorflow.org/decision_forests/tutorials/beginner_colab?hl=zh_cn]。

我一开始没注意,后来发现我在docker环境中直接用pip安装该库时,帮我又安装了cpu版本的tensorflow,这样就和我的gpu版本冲突了,然后去网上搜了一下,发现该库现在还有很多使用限制:1. 仅仅支持linux,不支持windows和mac 2. 仅支持cpu ,还没有gpu版本,见[https://github.com/tensorflow/decision-forests/issues/38].作者的意思是用gpu训练会更复杂,更详细的我就没看了。

然后我的做法是在该tensorflow-gpu的docker环境中使用virtualenv创建一个tensorfow的cpu虚拟环境,然后再用pip安装TF-DF这个包。避免污染docker主环境中的tensorflow-gpu。

之所以不用miniconda,是因为conda会默认替代掉我容器自带的python以及安装好的tensorflow-gpu。我只是想用一下TFDF这个库,不想太折腾。

此处贴virtualenv的命令:

1
2
3
4
5
6
7
pip install virtualenv

virtualenv venv # 在项目目录中执行,会创建一个名称为venv的虚拟环境,venv文件夹下包含python和pip

source venv/bin/activate # 激活环境

激活完了之后就pip安装tensorflow就行了

本地机器vscode配置使用远程服务器中运行的容器

参考 [https://zhuanlan.zhihu.com/p/80099904]

其中有几个地方需要注意一下的是:

  1. 如果遇到在本地机器vscode中使用插件remote ssh连接不上容器的问题时,需要vim /etc/ssh/sshd_config ,将PermitRootLogin的值改为yes(去掉前面的#号)

  2. 远程容器里面还需要有python插件,才能在本地机器vscode中debug代码

  3. 上面博主服务器端口映射的是容器的22端口,我没有尝试过用其他端口映射。22端口是ssh登陆的默认端口。

cudnn深度学习库的安装和验证

参考[https://blog.csdn.net/caicaiatnbu/article/details/87626491]

注意点:

  1. 下载对应的linux版本的cudnn

conda安装cuda和cudnn

如果使用的是conda的环境,可以单独使用conda install cuda来在虚拟环境中安装不同于本机版本的cuda和cudnn,而不需要使用我上面提到的那种方式(每次换cuda版本需要更换/usr/local/cuda的软链接的方式)。

conda创建了虚拟环境后,激活进入虚拟环境

1
conda install cudatoolkit=10.2

以上命令安装了10.2版本的cuda,然后可以使用cuda search cudnn找一下合适版本的cudnn,然后还是用cuda install cudnn=版本号来安装

注意:这里用conda安装的cuda和cudnn,是无法像在本机使用nvcc -V来检查版本的,所以即使你在虚拟环境激活的情况下使用 nvcc -V的命令会返回无此命令或者是返回的还是本机cuda的版本号!

那可能有人会问,如果想在conda虚拟环境下测试cuda和cudnn是否安装成功怎么办?

目前的办法是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# In[]:
import torch
# cpu
print(torch.__version__)
# gpu
print(torch.cuda.is_available())

# In[]:
import tensorflow as tf
# cpu
print(tf.__version__)
# v1 to test gpu
print(tf.test.is_gpu_available())
# v2 to test gpu
print(tf.config.list_physical_devices('GPU'))

参考[https://blog.csdn.net/qq_37774098/article/details/109895048]

docker拉取的pytorch-gpu版找不到cuda和cudnn的位置,为何?

参考 https://blog.csdn.net/ljp1919/article/details/106209358

tensorflow和pytorch验证GPU是否可用

tensorflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import tensorflow as tf

gpu_device_name = tf.test.gpu_device_name()
print(gpu_device_name)
# 查看是否可用
tf.test.is_gpu_available()

from tensorflow.python.client import device_lib

# 列出所有的本地机器设备
local_device_protos = device_lib.list_local_devices()
# 打印
# print(local_device_protos)

# 只打印GPU设备
[print(x) for x in local_device_protos if x.device_type == 'GPU']

pytorch

1
2
3
4
5
6
7
8
9
10
11
12
13
import torch
flag = torch.cuda.is_available()
if flag:
print("CUDA可使用")
else:
print("CUDA不可用")

ngpu= 1
# Decide which device we want to run on
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
print("驱动为:",device)
print("GPU型号: ",torch.cuda.get_device_name(0))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import 	torch
import time
print(torch.__version__)
print(torch.cuda.is_available())
# print('hello, world.')


a = torch.randn(10000, 1000)
b = torch.randn(1000, 2000)

t0 = time.time()
c = torch.matmul(a, b)
t1 = time.time()
print(a.device, t1 - t0, c.norm(2))

device = torch.device('cuda')
a = a.to(device)
b = b.to(device)

t0 = time.time()
c = torch.matmul(a, b)
t2 = time.time()
print(a.device, t2 - t0, c.norm(2))

t0 = time.time()
c = torch.matmul(a, b)
t2 = time.time()
print(a.device, t2 - t0, c.norm(2))

附录:docker使用

创建容器:

1
2
3
4
5
6
7
8
docker run --gpus all -d -v /:/host -p 3333:22 tensorflow/tensorflow:2.4.0-gpu tail -f /var/log/dpkg.log # 创建容器并启动
docker create --name postgres nginx:latest # 创建容器并不启动

docker start # 启动一个或多个已经被停止的容器

docker stop # 停止一个运行中的容器

docker restart # 重启容器

进入容器:

1
2
3
docker exec -it [container_id] /bin/bash

docker inpect # 获取容器/镜像的元数据

停止容器运行

1
2
docker kill -s KILL [container_id]
或者 docker stop [container_id]

删除一个或多个容器

1
2
3
docker rm -f db01 db02 # 强制删除,容器可以是在运行着的状态
或者 docker rm db01

docker save 与 docker export 区别参考:[https://jingsam.github.io/2017/08/26/docker-save-and-docker-export.html]

1
2
docker save -o images.tar postgres:9.6 # 打包postgres镜像
docker load -9 images.tar # 载入镜像

docker save的应用场景是,如果你的应用是使用docker-compose.yml编排的多个镜像组合,但你要部署的客户服务器并不能连外网。这时,你可以使用docker save将用到的镜像打个包,然后拷贝到客户服务器上使用docker load载入。

1
2
docker export -o postgres-export.tar postgres # 将容器postgres打包成一个tar
docker import postgres-export.tar postgres:latest # 这是将tar包import成一个镜像,镜像和tag名字自定义

docker export的应用场景主要用来制作基础镜像,比如你从一个ubuntu镜像启动一个容器,然后安装一些软件和进行一些设置后,使用docker export保存为一个基础镜像。然后,把这个镜像分发给其他人使用,比如作为基础的开发环境。