Python数据分析(3)-numpy中nd数组的创建

  1. ndarray的内存结构

和其他的库一样,每个库都可能有自己独特的数据结构,例如OpenCV,numpy库的多维数组叫做ndarray( N dimensionality array ),它的内存结构如下图:
这里写图片描述
ndarray的内存结构
在这个结构体中有两个对象,一个是用来描述元素类型的头部区域,一个是用来储存数据的数据区域。(事实上大多数数据类型的数据都是这么储存的)。

2 ndarray对象的创建

2.1 ndarray多维数组的创建常规方法
创建一个3*3的数组并在屏幕打印它以及它的类型和维数:

import numpy as np

x = np.array([[0,1,2],[3,4,5],[6,7,8]],dtype = np.int32)
print('这个数组是:',x)
print('这个数组的数据类型是:',x.dtype)
print('这个数组的大小:',x.shape)

屏幕输出结果:
这里写图片描述

我们也可以采用更加直接的办法:

import numpy as np
x = np.arange(0,9).reshape(3,3)
print('这个数组是:',x)
print('这个数组的数据类型是:',x.dtype)
print('这个数组的大小:',x.shape)

屏幕上打印输出的结果和前一种的结果是一样的。

2.2 ndarray多维数组的创建其他方法
除了常规方法,numpy还提供了一些其他的创建方法:
2.2.1 创建全0或者全1的数组

这里写图片描述
例如:
这里写图片描述

import numpy as np

x = np.ones([3,3])
print('这个数组是:',x)
print('这个数组的数据类型是:',x.dtype)
print('这个数组的大小:',x.shape)
>>> x = np.arange(6) 
>>> x = x.reshape(2,3)
>>> x     
array([[0, 1, 2], [3, 4, 5]]) 
>>> np.ones_like(x) 
array([[1, 1, 1], [1, 1, 1]])
>>> y = np.arange(3, dtype=np.float) 
>>> y 
array([ 0., 1., 2.]) 
>>> np.ones_like(y) 
array([ 1., 1., 1.])

当然也可以填充其他的数:

import numpy as np
x = np.full([3,3],np.inf)
print('这个数组是:',x)
print('这个数组的数据类型是:',x.dtype)
print('这个数组的大小:',x.shape)

打印输出:这里写图片描述

2.2.2 从已存在的数据中创建数组
这里写图片描述

>>> np.array([1, 2, 3]) 
array([1, 2, 3])

>>> np.array([1, 2, 3.0]) 
array([ 1., 2., 3.])

>>> np.array([[1, 2], [3, 4]]) 
array([[1, 2], [3, 4]])

>>> np.array([1, 2, 3], ndmin=2) 
array([[1, 2, 3]])

>>> np.array([1, 2, 3], dtype=complex) 
array([ 1.+0.j, 2.+0.j, 3.+0.j])

>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')]) 
>>> x['a'] 
array([1, 3])

2.2.3 创建记录阵列(record array,可能翻译不准):创建一个阵列,将其他数组集中在一起管理,并可以命名:
这里写图片描述
例如:

import numpy as np

x1 = np.arange(0,3)
x2 = np.array(['ff','hh','yy'])
x3 = ([1,2,3])
r = np.core.records.fromarrays([x1,x2,x3],names='a,b,c')
print(r[2])
print(r.a)

2.2.4 创建字符数组
numpy提供了专门的函数创建字符数组:np.chararray()
首先看看它的参数:
Parameters
| ———-
| shape : tuple
| Shape of the array.
| itemsize : int, optional
| Length of each array element, in number of characters. Default is 1.
| unicode : bool, optional
| Are the array elements of type unicode (True) or string (False).
| Default is False.
| buffer : int, optional
| Memory address of the start of the array data. Default is None,
| in which case a new array is created.
| offset : int, optional
| Fixed stride displacement from the beginning of an axis?
| Default is 0. Needs to be >=0.
| strides : array_like of ints, optional
| Strides for the array (see ndarray.strides for full description).
| Default is None.
| order : {‘C’, ‘F’}, optional
| The order in which the array data is stored in memory: ‘C’ ->
| “row major” order (the default), ‘F’ -> “column major”
| (Fortran) order.

 Examples
 |  --------
 |  >>> charar = np.chararray((3, 3))
 |  >>> charar[:] = 'a'
 |  >>> charar
 |  chararray([['a', 'a', 'a'],
 |         ['a', 'a', 'a'],
 |         ['a', 'a', 'a']],
 |        dtype='|S1')
 |  
 |  >>> charar = np.chararray(charar.shape, itemsize=5)
 |  >>> charar[:] = 'abc'
 |  >>> charar
 |  chararray([['abc', 'abc', 'abc'],
 |         ['abc', 'abc', 'abc'],
 |         ['abc', 'abc', 'abc']],
 |        dtype='|S5')

2.2.5 创建数值数组
这里写图片描述

import numpy as np

x1 = np.arange(0,3,1)
x2 = np.linspace(0,3,4)
x3 = np.logspace(1,8,3)
x4 = np.mgrid[0:3,0:3]
x5 = np.ogrid[0:3,0:3]
print(x1,x2,x3,x4,x5)

2.2.6 创建矩阵
这里写图片描述

2.2.7 矩阵类(The matrix class)
这里写图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值