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的lambda语句声明匿名函数的用法
Jul 01 Python
Python设计模式之中介模式简单示例
Jan 09 Python
python实现闹钟定时播放音乐功能
Jan 25 Python
django在接受post请求时显示403forbidden实例解析
Jan 25 Python
Python实现对字典分别按键(key)和值(value)进行排序的方法分析
Dec 19 Python
Pythony运维入门之Socket网络编程详解
Apr 15 Python
Python中asyncio模块的深入讲解
Jun 10 Python
Tensorflow中tf.ConfigProto()的用法详解
Feb 06 Python
keras输出预测值和真实值方式
Jun 27 Python
python中plt.imshow与cv2.imshow显示颜色问题
Jul 16 Python
tensorflow与numpy的版本兼容性问题的解决
Jan 08 Python
Python实现制作销售数据可视化看板详解
Nov 27 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
2020最新CPU的性能排名
2020/04/02 数码科技
使用sockets:从新闻组中获取文章(一)
2006/10/09 PHP
Laravel中如何增加自定义全局函数详解
2017/05/09 PHP
javascript下有关dom以及xml节点访问兼容问题
2007/11/26 Javascript
javascript 动态table添加colspan\rowspan 参数的方法
2009/07/25 Javascript
基于jquery的滚动鼠标放大缩小图片效果
2011/10/27 Javascript
js 延迟加载 改变JS的位置加快网页加载速度
2012/12/11 Javascript
JS获取地址栏参数的几种方法小结
2014/02/28 Javascript
jQuery实现瀑布流布局
2014/12/12 Javascript
JavaScript 异常处理 详解
2015/02/06 Javascript
jquery+css实现动感的图片切换效果
2015/11/25 Javascript
Js获取图片原始宽高的实现代码
2016/05/17 Javascript
深究AngularJS中$sce的使用
2017/06/12 Javascript
Webpack打包css后z-index被重新计算的解决方法
2017/06/18 Javascript
Angular 表单控件示例代码
2017/06/26 Javascript
微信小程序switch组件使用详解
2018/01/31 Javascript
vue中如何实现pdf文件预览的方法
2018/07/12 Javascript
基于vue写一个全局Message组件的实现
2019/08/15 Javascript
Node.js HTTP服务器中的文件、图片上传的方法
2019/09/23 Javascript
js函数和this用法实例分析
2020/03/13 Javascript
解决vue-cli输入命令vue ui没效果的问题
2020/11/17 Javascript
[02:57]DOTA2亚洲邀请赛小组赛第四日 赛事回顾
2015/02/02 DOTA
python解决字典中的值是列表问题的方法
2013/03/04 Python
基于Python Numpy的数组array和矩阵matrix详解
2018/04/04 Python
python修改list中所有元素类型的三种方法
2018/04/09 Python
python将字符串以utf-8格式保存在txt文件中的方法
2018/10/30 Python
详解Python 正则表达式模块
2018/11/05 Python
Python检查ping终端的方法
2019/01/26 Python
Python获取时间范围内日期列表和周列表的函数
2019/08/05 Python
AmazeUI 模态窗口的实现代码
2020/08/18 HTML / CSS
美国网上眼镜供应商:LEOTONY(眼镜、RX太阳镜和太阳镜)
2017/10/31 全球购物
英国婚礼商城:Wedding Mall
2019/11/02 全球购物
课程设计心得体会
2013/12/28 职场文书
公司出纳岗位职责
2015/03/31 职场文书
民主生活会意见
2015/06/05 职场文书
Python中tqdm的使用和例子
2022/09/23 Python