Python list与NumPy array 区分详解


Posted in Python onNovember 06, 2019

1. 数据类型 type()

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

import numpy as np
# import tensorflow as tf
import cv2
import time

print(16 * "++--")
print("current_directory:", current_directory)

PIXEL_MEAN = [123.68, 116.779, 103.939] # R, G, B. In TensorFlow, channel is RGB. In OpenCV, channel is BGR.
print("Python list")
print("PIXEL_MEAN:", PIXEL_MEAN)
print("type(PIXEL_MEAN):", type(PIXEL_MEAN))
print("type(PIXEL_MEAN[0]):", type(PIXEL_MEAN[0]), "\n")

PIXEL_MEAN_array = np.array(PIXEL_MEAN)
print("NumPy array")
print("PIXEL_MEAN_array:", PIXEL_MEAN_array)
print("type(PIXEL_MEAN_array):", type(PIXEL_MEAN_array))
print("type(PIXEL_MEAN_array[0]):", type(PIXEL_MEAN_array[0]))
print("PIXEL_MEAN_array.dtype:", PIXEL_MEAN_array.dtype)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
Python list
PIXEL_MEAN: [123.68, 116.779, 103.939]
type(PIXEL_MEAN): <type 'list'>
type(PIXEL_MEAN[0]): <type 'float'> 

NumPy array
PIXEL_MEAN_array: [123.68 116.779 103.939]
type(PIXEL_MEAN_array): <type 'numpy.ndarray'>
type(PIXEL_MEAN_array[0]): <type 'numpy.float64'>
PIXEL_MEAN_array.dtype: float64

Process finished with exit code 0

2. 数据融合 (data fusion)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))

import numpy as np
# import tensorflow as tf
import cv2
import time

print(16 * "++--")
print("current_directory:", current_directory)

PIXEL_MEAN = [123.68, 116.779, 103.939] # R, G, B. In TensorFlow, channel is RGB. In OpenCV, channel is BGR.
print("Python list")
print("PIXEL_MEAN:", PIXEL_MEAN)
print("type(PIXEL_MEAN):", type(PIXEL_MEAN))
print("type(PIXEL_MEAN[0]):", type(PIXEL_MEAN[0]), "\n")

PIXEL_MEAN_array = np.array(PIXEL_MEAN)
print("NumPy array")
print("PIXEL_MEAN_array:", PIXEL_MEAN_array)
print("type(PIXEL_MEAN_array):", type(PIXEL_MEAN_array))
print("type(PIXEL_MEAN_array[0]):", type(PIXEL_MEAN_array[0]))
print("PIXEL_MEAN_array.dtype:", PIXEL_MEAN_array.dtype, "\n")

image_array = np.array(
  [[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], [[21, 22, 23], [24, 25, 26], [27, 28, 29], [30, 31, 32]]])
print("image_array:", image_array)
print("type(image_array):", type(image_array))
print("type(image_array[0]):", type(image_array[0]))
print("image_array.dtype:", image_array.dtype, "\n")

image_array_fusion = image_array + np.array(PIXEL_MEAN)
print("image_array_fusion:", image_array_fusion)
print("type(image_array_fusion):", type(image_array_fusion))
print("type(image_array_fusion[0]):", type(image_array_fusion[0]))
print("image_array_fusion.dtype:", image_array_fusion.dtype)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
Python list
PIXEL_MEAN: [123.68, 116.779, 103.939]
type(PIXEL_MEAN): <type 'list'>
type(PIXEL_MEAN[0]): <type 'float'> 

NumPy array
PIXEL_MEAN_array: [123.68 116.779 103.939]
type(PIXEL_MEAN_array): <type 'numpy.ndarray'>
type(PIXEL_MEAN_array[0]): <type 'numpy.float64'>
PIXEL_MEAN_array.dtype: float64 

image_array: [[[ 1 2 3]
 [ 4 5 6]
 [ 7 8 9]
 [10 11 12]]

 [[21 22 23]
 [24 25 26]
 [27 28 29]
 [30 31 32]]]
type(image_array): <type 'numpy.ndarray'>
type(image_array[0]): <type 'numpy.ndarray'>
image_array.dtype: int64 

image_array_fusion: [[[124.68 118.779 106.939]
 [127.68 121.779 109.939]
 [130.68 124.779 112.939]
 [133.68 127.779 115.939]]

 [[144.68 138.779 126.939]
 [147.68 141.779 129.939]
 [150.68 144.779 132.939]
 [153.68 147.779 135.939]]]
type(image_array_fusion): <type 'numpy.ndarray'>
type(image_array_fusion[0]): <type 'numpy.ndarray'>
image_array_fusion.dtype: float64

Process finished with exit code 0

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python网络编程学习笔记(六):Web客户端访问
Jun 09 Python
Pyhton中防止SQL注入的方法
Feb 05 Python
Python contextlib模块使用示例
Feb 18 Python
用Python脚本来删除指定容量以上的文件的教程
May 04 Python
基于python 字符编码的理解
Sep 02 Python
python中使用print输出中文的方法
Jul 16 Python
一行python实现树形结构的方法
Aug 09 Python
Python 实用技巧之利用Shell通配符做字符串匹配
Aug 23 Python
春节到了 教你使用python来抢票回家
Jan 06 Python
如何用python插入独创性声明
Mar 31 Python
Python实现byte转integer
Jun 03 Python
matplotlib如何设置坐标轴刻度的个数及标签的方法总结
Jun 11 Python
Django实现WebSSH操作物理机或虚拟机的方法
Nov 06 #Python
django 简单实现登录验证给你
Nov 06 #Python
Python数据可视化:箱线图多种库画法
Nov 06 #Python
使用Python完成15位18位身份证的互转功能
Nov 06 #Python
python3.8 微信发送服务器监控报警消息代码实现
Nov 05 #Python
python SVD压缩图像的实现代码
Nov 05 #Python
Django REST框架创建一个简单的Api实例讲解
Nov 05 #Python
You might like
php操作MongoDB基础教程(连接、新增、修改、删除、查询)
2014/03/25 PHP
基于PHP实现数据分页显示功能
2016/05/26 PHP
PHP全局使用Laravel辅助函数dd
2019/12/26 PHP
php的单例模式及应用场景详解
2021/02/27 PHP
基于JQuery框架的AJAX实例代码
2009/11/03 Javascript
Javascript 面向对象(二)封装代码
2012/05/23 Javascript
jquery ui dialog实现弹窗特效的思路及代码
2013/08/03 Javascript
js 编码转换 gb2312 和 utf8 互转的2种方法
2013/08/07 Javascript
javascript实现字符串反转的方法
2015/02/05 Javascript
flash+jQuery实现可关闭及重复播放的压顶广告
2015/04/15 Javascript
JS实现完全语义化的网页选项卡效果代码
2015/09/15 Javascript
jQuery设置单选按钮radio选中/不可用的实例代码
2016/06/24 Javascript
浅谈Angularjs link和compile的使用区别
2016/10/21 Javascript
JS图片延迟加载插件LazyImgv1.0用法分析【附demo源码下载】
2017/09/04 Javascript
微信小程序 页面跳转事件绑定的实例详解
2017/09/20 Javascript
深入理解Vue生命周期、手动挂载及挂载子组件
2017/09/27 Javascript
JS实现的透明度渐变动画效果示例
2018/04/28 Javascript
Nodejs异步流程框架async的方法
2019/06/07 NodeJs
python使用正则表达式匹配字符串开头并打印示例
2017/01/11 Python
python基础之入门必看操作
2017/07/26 Python
PHP实现发送和接收JSON请求
2018/06/07 Python
python绘制立方体的方法
2018/07/02 Python
python: 自动安装缺失库文件的方法
2018/10/22 Python
Python一行代码解决矩阵旋转的问题
2019/11/30 Python
python创建ArcGIS shape文件的实现
2019/12/06 Python
Python Tornado批量上传图片并显示功能
2020/03/26 Python
canvas生成带二维码海报的踩坑记录
2019/09/11 HTML / CSS
canvas绘制图片drawImage使用方法
2020/09/15 HTML / CSS
英国和世界各地鲜花速递专家:Arena Flowers
2018/02/10 全球购物
探亲假请假条
2014/04/11 职场文书
学校三八妇女节活动总结
2015/02/06 职场文书
幼儿园辞职信范文
2015/02/27 职场文书
学习与创新自我评价
2015/03/09 职场文书
重阳节座谈会主持词
2015/07/03 职场文书
如何理解PHP核心特性命名空间
2021/05/28 PHP
Spring Cloud OpenFeign模版化客户端
2022/06/25 Java/Android