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抓taobao图片爬虫
Oct 26 Python
Python常用的爬虫技巧总结
Mar 28 Python
Python Requests模拟登录实现图书馆座位自动预约
Apr 27 Python
python实现图片文件批量重命名
Mar 23 Python
python实现从pdf文件中提取文本,并自动翻译的方法
Nov 28 Python
在python中对变量判断是否为None的三种方法总结
Jan 23 Python
python+openCV利用摄像头实现人员活动检测
Jun 22 Python
10款最好的Python开发编辑器
Jul 03 Python
YUV转为jpg图像的实现
Dec 09 Python
Python定义一个Actor任务
Jul 29 Python
python如何调用php文件中的函数详解
Dec 29 Python
python基于tkinter制作m3u8视频下载工具
Apr 24 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 __autoload函数(自动载入类文件)的使用方法
2012/02/04 PHP
PHP获取网址的顶级域名函数代码
2012/09/24 PHP
php对mongodb的扩展(初出茅庐)
2012/11/11 PHP
phpphp图片采集后按原路径保存图片示例
2014/02/18 PHP
浅谈php和.net的区别
2014/09/28 PHP
WordPress中邮件的一些修改和自定义技巧
2015/12/15 PHP
注释PHP和html混合代码的小技巧(分享)
2016/11/03 PHP
Redis构建分布式锁
2017/03/28 PHP
Laravel5框架添加自定义辅助函数的方法
2018/08/01 PHP
对YUI扩展的Gird组件 Part-2
2007/03/10 Javascript
JSON.stringify转换JSON时日期时间不准确的解决方法
2014/08/08 Javascript
分享一款基于jQuery的视频播放插件
2014/10/09 Javascript
浅谈js在html中的加载执行顺序,多个jquery ready执行顺序
2016/11/26 Javascript
微信小程序左右滑动切换页面详解及实例代码
2017/02/28 Javascript
Angular.js实现动态加载组件详解
2017/05/28 Javascript
微信小程序之前台循环数据绑定
2017/08/18 Javascript
JS实现图片旋转动画效果封装与使用示例
2018/07/09 Javascript
axios的拦截请求与响应方法
2018/08/11 Javascript
原生js实现贪食蛇小游戏的思路详解
2019/11/26 Javascript
node.js express捕获全局异常的三种方法实例分析
2019/12/27 Javascript
python应用程序在windows下不出现cmd窗口的办法
2014/05/29 Python
python调用Moxa PCOMM Lite通过串口Ymodem协议实现发送文件
2014/08/15 Python
python+pyqt实现右下角弹出框
2017/10/26 Python
python对文件的操作方法汇总
2020/02/28 Python
Python如何实现远程方法调用
2020/08/07 Python
Python实现定时监测网站运行状态的示例代码
2020/09/30 Python
css3实例教程 一款纯css3实现的环形导航菜单
2014/10/20 HTML / CSS
CSS3对背景图片的裁剪及尺寸和位置的设定方法
2016/03/07 HTML / CSS
个性与发展自我评价
2014/02/11 职场文书
教师见习报告范文
2014/11/03 职场文书
一般基层干部群众路线教育实践活动个人对照检查材料
2014/11/04 职场文书
学习雷锋精神倡议书
2015/04/27 职场文书
西游降魔篇观后感
2015/06/15 职场文书
工作简报格式范文
2015/07/21 职场文书
SQL Server表分区删除详情
2021/10/16 SQL Server
深入理解pytorch库的dockerfile
2022/06/10 Python