且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

类型修改与数组去重| 学习笔记

更新时间:2022-09-06 18:15:19

开发者学堂课程【Python 科学计算库 NumPy 快速入门类型修改与数组去重学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址https://developer.aliyun.com/learning/course/605/detail/8820


类型修改与数组去重


内容简介:

一、类型修改

二、数组的去重


一、类型修改

●ndarray.astype(type)

stock_ change. astype (np. int32)

●ndarray.tostring([order]) 或者 ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array.

。转换成 bytes

arr = np.array([[[1, 2, 3],[4, 5, 6]],[[12, 3,34], [5,6, 7]]])

arr. tostring()

拓展:如果遇到

I0Pub data rate exceeded.

The notebook server Will temporarily stop sending output

to the client in order to avoid crashing it.

To change this limit, set the config variable

、--NotebookApp.iopub_ data_ rate_ limit.

这个问题是在 jupyer 当中对输出的字节数有限制,需要去修改配置文件

创建配置文件

jupyter notebook --generate . config

vi ~/.jupyter/jupyter_ notebook _config.py

取消注释,多增加

## (bytes/sec) Maximum rate at which messages can be sent on iopub before they

#  are limited.

C .NotebookApp. iopub_ data_ rate_ limit = 10000000

但是不建议这样去修改,jupyter 输出太大会 崩溃

(一)ndarray . astype (type )

In[84]:stock_change.astype(“int32”)

Out[84]:array([[ 0,    1,   0,   2,   0,   0,  -1,   0,   1,-1],

[-1, -1, 0, 1, -1, 0, 0, -1, 0,   0] ,

[ 0,   0, 1, 0,  1,2,   0,    0,   0,  0],  

[ 0,   0, -1,  0, 0,   -1,  0,   -1,   0,  0],

[0, -1, -2, -1, 0,  0,   0,    1,   1,  0],

[0,  1,  0, -2, -2, -1,   1,   -2,  1,   1],

[-2,   0,  0, 0,  0,   -1,   0,   0,    1,    0],

[0,   0,   0, 0, -1, -1,  0, 1,    1,   0]],

dtype=int32)  

(二)ndarray 序列化到本地

ndarray. tostring ( )


二、数组的去重

· ndarray.unique

temp = np.array([[l, 2, 3, 4],[3, 4, 5, 6]])

>>> np.unique(temp)

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

例子:

1、In [88]:temp . np.array([[l, 2, 3, 4],[3, 4, 5, 6]])

In [89]:temp

Out(89):array([[l, 2, 3, 4],

           l3, 4, 5, 6J)

In [91]:np· unique( temp)

Out(91):array([l, 2, 3, 4, 5, 6])

2、In [94] :set (temp. flatten()

Out(94):(1,2,3,4,5,6)