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根据京东商品url获取产品价格
Aug 09 Python
Python数据类型详解(一)字符串
May 08 Python
python处理csv中的空值方法
Jun 22 Python
python flask实现分页的示例代码
Aug 02 Python
python合并已经存在的sheet数据到新sheet的方法
Dec 11 Python
解决python中画图时x,y轴名称出现中文乱码的问题
Jan 29 Python
python中的协程深入理解
Jun 10 Python
PyCharm安装Markdown插件的两种方法
Jun 24 Python
基于python代码批量处理图片resize
Jun 04 Python
keras 指定程序在某块卡上训练实例
Jun 22 Python
Python爬虫设置Cookie解决网站拦截并爬取蚂蚁短租的问题
Feb 22 Python
python 爬取天气网卫星图片
Jun 07 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
兼容PHP5的PHP目录管理函数库
2008/07/10 PHP
PHP文件操作实现代码分享
2011/09/01 PHP
PHP仿qq空间或朋友圈发布动态、评论动态、回复评论、删除动态或评论的功能(上)
2017/05/26 PHP
laravel框架之数据库查出来的对象实现转化为数组
2019/10/23 PHP
javascript 读取xml,写入xml 实现代码
2009/07/10 Javascript
基于jquery的跨域调用文件
2010/11/19 Javascript
关于锚点跳转及jQuery下相关操作与插件
2012/10/01 Javascript
jQuery图片播放8款精美插件分享
2013/02/17 Javascript
jQuery的$.proxy()应用示例介绍
2014/04/03 Javascript
javascript实现checkBox的全选,反选与赋值
2015/03/12 Javascript
详解js中构造流程图的核心技术JsPlumb(2)
2015/12/08 Javascript
JavaScript实现翻页功能(附效果图)
2017/02/16 Javascript
vue-cli 使用vue-bus来全局控制的实例讲解
2018/09/15 Javascript
Vue实现push数组并删除的例子
2019/11/01 Javascript
HTML+JS实现“代码雨”效果源码(黑客帝国文字下落效果)
2020/03/17 Javascript
jQuery+ajax实现用户登录验证
2020/09/13 jQuery
[22:59]VGJ.S vs VG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
[01:14]DOTA2 7.22版本新增神杖效果展示(智力英雄篇)
2019/05/29 DOTA
python模拟新浪微博登陆功能(新浪微博爬虫)
2013/12/24 Python
Python实现数据可视化看如何监控你的爬虫状态【推荐】
2018/08/10 Python
python使用knn实现特征向量分类
2018/12/26 Python
python实现QQ空间自动点赞功能
2019/04/09 Python
python多线程实现TCP服务端
2019/09/03 Python
python base64库给用户名或密码加密的流程
2020/01/02 Python
Python读取Excel一列并计算所有对象出现次数的方法
2020/09/04 Python
pycharm 如何取消连按两下shift出现的全局搜索
2021/01/15 Python
CSS3制作Dropdown下拉菜单的方法
2015/07/18 HTML / CSS
路政管理毕业自荐书范文
2014/02/10 职场文书
我的大学生活演讲稿
2014/04/25 职场文书
银行委托书范本
2014/09/28 职场文书
会议开幕致辞怎么写
2016/03/03 职场文书
委托书范本格式
2019/04/18 职场文书
甜美蛋糕店的创业计划书模板,拿来即用!
2019/08/21 职场文书
通过Qt连接OpenGauss数据库的详细教程
2021/06/23 PostgreSQL
java如何实现socket连接方法封装
2021/09/25 Java/Android
Tomcat安装使用及部署Web项目的3种方法汇总
2022/08/14 Servers