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正则表达式修复网站文章字体不统一的解决方法
Feb 21 Python
python del()函数用法
Mar 24 Python
Python编程之属性和方法实例详解
May 19 Python
给Python入门者的一些编程建议
Jun 15 Python
PyQt5图形界面播放音乐的实例
Jun 17 Python
使用python实现回文数的四种方法小结
Nov 24 Python
Python一行代码解决矩阵旋转的问题
Nov 30 Python
python二分法查找算法实现方法【递归与非递归】
Dec 06 Python
使用sklearn的cross_val_score进行交叉验证实例
Feb 28 Python
Python HTTP下载文件并显示下载进度条功能的实现
Apr 02 Python
django正续或者倒序查库实例
May 19 Python
Django haystack实现全文搜索代码示例
Nov 28 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
第九节--绑定
2006/11/16 PHP
PHP中error_log()函数的使用方法
2015/01/20 PHP
在PHP中使用FastCGI解析漏洞及修复方案
2015/11/10 PHP
Zend Framework入门教程之Zend_Db数据库操作详解
2016/12/08 PHP
PHP使用preg_split和explode分割textarea存放内容的方法分析
2017/07/03 PHP
用javascript实现兼容IE7的类库 IE7_0_9.zip提供下载
2007/08/08 Javascript
js每隔5分钟执行一次ajax请求的实现方法
2013/11/27 Javascript
jQuery学习笔记之总体架构
2014/06/03 Javascript
javascript常用的方法分享
2015/07/01 Javascript
js从外部获取图片的实现方法
2016/08/05 Javascript
微信小程序 教程之注册页面
2016/10/17 Javascript
JavaScript中创建对象的7种模式详解
2017/02/21 Javascript
jQuery Json数据格式排版高亮插件json-viewer.js使用方法详解
2017/06/12 jQuery
JS中Safari浏览器中的Date
2017/07/17 Javascript
layui 实现加载动画以及非真实加载进度的方法
2019/09/23 Javascript
vue实现循环滚动列表
2020/06/30 Javascript
python生成器的使用方法
2013/11/21 Python
使用Python生成url短链接的方法
2015/05/04 Python
Python简单生成随机姓名的方法示例
2017/12/27 Python
PyQt4实现下拉菜单可供选择并打印出来
2018/04/20 Python
对pandas进行数据预处理的实例讲解
2018/04/20 Python
Python双向循环链表实现方法分析
2018/07/30 Python
python在回调函数中获取返回值的方法
2019/02/22 Python
Python 窗体(tkinter)按钮 位置实例
2019/06/13 Python
Win10下Python3.7.3安装教程图解
2019/07/08 Python
浅谈优化Django ORM中的性能问题
2020/07/09 Python
python定时截屏实现
2020/11/02 Python
HTML5实现直播间评论滚动效果的代码
2020/05/27 HTML / CSS
美国在线精品家居网站:Burke Decor
2017/04/12 全球购物
英国莱斯特松木橡木家具网上商店:Choice Furniture Superstore
2019/07/05 全球购物
法律专业大学生职业生涯规划书:向目标一步步迈进
2014/09/22 职场文书
酒店管理专业毕业生自我鉴定
2014/09/29 职场文书
抢劫罪辩护词
2015/05/21 职场文书
Vue实现导入Excel功能步骤详解
2021/07/03 Vue.js
Python何绘制带有背景色块的折线图
2022/04/23 Python
nginx rewrite功能使用场景分析
2022/05/30 Servers