3.numpy基础使用
来源:互联网 发布:绿色傲剑金蛇数据 编辑:程序博客网 时间:2024/06/05 14:30
python3.5版本下demo实例
import sysimport numpyprint(sys.version)'''3.5.3 |Continuum Analytics, Inc.| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)]'''print ('使用列表生成一维数组')data = [1,2,3,4,5,6]x = numpy.array(data)print (x )#打印数组print (x.dtype) #打印数组元素的类型'''[1 2 3 4 5 6]int32'''print ('使用列表生成二维数组')data = [[1,2,2],[3,4,2],[5,6,2],[5,6,2]]x = numpy.array(data)print (x) #打印数组print (x.ndim) #打印数组的维度print (x.shape) #打印数组各个维度的长度。shape是一个元组'''[[1 2 2] [3 4 2] [5 6 2] [5 6 2]]2(4, 3)'''print ('使用zero/ones/empty创建数组:根据shape来创建')x = numpy.zeros(6) #创建一维长度为6的,元素都是0一维数组print (x)x = numpy.zeros((2,3)) #创建一维长度为2,二维长度为3的二维0数组print (x)x = numpy.ones((2,3)) #创建一维长度为2,二维长度为3的二维1数组print (x)x = numpy.empty((3,3)) #创建一维长度为2,二维长度为3,未初始化的二维数组print (x)'''[ 0. 0. 0. 0. 0. 0.][[ 0. 0. 0.] [ 0. 0. 0.]][[ 1. 1. 1.] [ 1. 1. 1.]][[ 2.45757305e-315 2.45761635e-315 2.45761635e-315] [ 2.45761635e-315 2.45761635e-315 2.45761635e-315] [ 0.00000000e+000 0.00000000e+000 2.71797014e-316]]'''print ('使用arrange生成连续元素')print (numpy.arange(6) )# [0,1,2,3,4,5,] 开区间print (numpy.arange(0,6,2) ) # [0, 2,4]'''[0 1 2 3 4 5][0 2 4]'''print ('生成指定元素类型的数组:设置dtype属性')x = numpy.array([1,2.6,3],dtype = numpy.int64)print (x )# 元素类型为int64print (x.dtype)x = numpy.array([1,2,3],dtype = numpy.float64)print (x) # 元素类型为float64print (x.dtype)'''[1 2 3]int64[ 1. 2. 3.]float64'''print ('使用astype复制数组,并转换类型')x = numpy.array([1,2.6,3],dtype = numpy.float64)y = x.astype(numpy.int32)print (y) # [1 2 3]print (x) # [ 1. 2.6 3. ]z = y.astype(numpy.float64)print (z) # [ 1. 2. 3.]'''[1 2 3][ 1. 2.6 3. ][ 1. 2. 3.]'''print ('将字符串元素转换为数值元素')x = numpy.array(['1','2','3'],dtype = numpy.string_)y = x.astype(numpy.int32)print (x )# ['1' '2' '3']print (y )# [1 2 3] 若转换失败会抛出异常'''[b'1' b'2' b'3'][1 2 3]'''print ('使用其他数组的数据类型作为参数')x = numpy.array([ 1., 2.6,3. ],dtype = numpy.float32);y = numpy.arange(3,dtype=numpy.int32);print (y) # [0 1 2]print (y.astype(x.dtype)) # [ 0. 1. 2.]'''[0 1 2][ 0. 1. 2.]'''
numpy基本类型
阅读全文
0 0
- 3.numpy基础使用
- Python基础-numpy使用
- NumPy基础 -- 3. 基本数据处理
- python numpy基础(二)random基本使用
- Numpy 基础
- Numpy 基础
- Numpy:基础
- numpy基础
- Numpy基础
- numpy基础
- NumPy 基础
- Numpy基础
- numpy 基础
- NumPy基础
- Numpy基础
- numpy 基础
- Numpy基础笔记(包括基础函数使用)
- numpy使用
- Oil Deposits(DFS深搜)
- JVM内存区
- 可变参数和泛型--->堆污染
- 操作系统之进程与线程3——内核级线程及切换(未完成)
- Balala Power! HDU
- 3.numpy基础使用
- 求最大公约数-辗转相除法-更相减损术
- Java对象的内存分配过程
- 17暑假多校联赛1.11 HDU 6043 KazaQ's Socks
- springboot+mybatis+gradle在idea和oracle使用
- Android获取路由ip
- CentOS配置日志集中管理
- 从源码角度解析View的绘制过程
- JVM垃圾收集算法