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日志模块logging简介
Apr 13 Python
Python中格式化format()方法详解
Apr 01 Python
解读! Python在人工智能中的作用
Nov 14 Python
python+pandas分析nginx日志的实例
Apr 28 Python
Python切片操作去除字符串首尾的空格
Apr 22 Python
python django 原生sql 获取数据的例子
Aug 14 Python
Series和DataFrame使用简单入门
Nov 13 Python
Python3使用xlrd、xlwt处理Excel方法数据
Feb 28 Python
使用卷积神经网络(CNN)做人脸识别的示例代码
Mar 27 Python
python爬虫数据保存到mongoDB的实例方法
Jul 28 Python
pandas处理csv文件的方法步骤
Oct 16 Python
python更新数据库中某个字段的数据(方法详解)
Nov 18 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
漫威DC御用漫画家去世 他的表情包曾走红网络
2020/04/09 欧美动漫
基于jquery的修改当前TAB显示标题的代码
2010/12/11 Javascript
jquery动态添加元素事件失效问题解决方法
2014/05/23 Javascript
原生JavaScript+LESS实现瀑布流
2014/12/12 Javascript
JavaScript正则表达式的分组匹配详解
2016/02/13 Javascript
js实现select二级联动下拉菜单
2020/04/17 Javascript
IOS中safari下的select下拉菜单文字过长不换行的解决方法
2016/09/26 Javascript
BootStrap Table 后台数据绑定、特殊列处理、排序功能
2017/05/27 Javascript
Vue单文件组件基础模板小结
2017/08/10 Javascript
利用Javascript开发一个二维周视图日历
2017/12/14 Javascript
vue vuex vue-rouert后台项目——权限路由(适合初学)
2017/12/29 Javascript
详解layui中的树形关于取值传值问题
2018/01/16 Javascript
vue实现2048小游戏功能思路详解
2018/05/09 Javascript
layer.js之回调销毁对话框的例子
2019/09/11 Javascript
Vue.js组件使用props传递数据的方法
2019/10/19 Javascript
vue项目中使用vue-layer弹框插件的方法
2020/03/11 Javascript
详解elementUI中input框无法输入的问题
2020/04/27 Javascript
详解element-ui 表单校验 Rules 配置 常用黑科技
2020/07/11 Javascript
vue如何在项目中调用腾讯云的滑动验证码
2020/07/15 Javascript
[44:43]完美世界DOTA2联赛决赛日 FTD vs GXR 第一场 11.08
2020/11/11 DOTA
python获取标准北京时间的方法
2015/03/24 Python
Python实现的概率分布运算操作示例
2017/08/14 Python
详解如何利用Cython为Python代码加速
2018/01/27 Python
使用python将多个excel文件合并到同一个文件的方法
2019/07/09 Python
详解用Python为直方图绘制拟合曲线的两种方法
2019/08/21 Python
解决Python 函数声明先后顺序出现的问题
2020/09/02 Python
Order by的几种用法
2013/06/16 面试题
城管大队整治方案
2014/05/06 职场文书
团队口号大全
2014/06/06 职场文书
班级文化建设标语
2014/06/23 职场文书
教师党员自我评议不足范文
2014/10/19 职场文书
政风行风整改报告
2014/11/06 职场文书
幼儿园亲子活动通知
2015/04/24 职场文书
学习雷锋主题班会
2015/08/14 职场文书
应届生个人的求职(自荐信范文2篇)
2019/08/23 职场文书
Python-OpenCV教程之图像的位运算详解
2021/06/21 Python