数组:numpy.shape 与numpy.reshape函数

adarray:可以是多维数组,但元素类型必须相同。

常用属性:

T         数组的转置(对高维数组而言)

dtype    数组元素的,数据类型

size      数组元素的个数

ndim    数组的维数

shape   数据的维度大小(以元组形式)

dtype:

bool_,   int(8,16,32,64),   unit(8,16,32,64),   float(16,32,64)

类型转换:astype()

创建adarray:

array()          将列表转换为数组,可选择显式指定dtype

arange()         range的numpy版,支持浮点数

linspace()       类似arange(),第三个参数为数组长度

zeros()       根据指定形状和dtype,创建全0数组

ones()       根据指定形状和dtype,创建全i数组

empty()      根据指定形状和dtype,创建空数组(随机值)

eye()          根据指定边长dtype,创建单元矩阵

 

实例:

linspace(0,10,15)    将0--10之间的数字,分成15份

 

 导入numpy模块

import numpy as np

Array(数组)

a = np.array([1,2,3])
#a
#array([1,2,3])

type(a)
#nympy.ndarray

a.shape
#(3,) #一纬数据 看大小

a= a.reshape((1,-1) ) #明确行列,-1=3
a.shape
#(1,3) #1行3列
a = np.array([1,2,3,4,5,6])

a.shape

#(6,)

a= a.reshape((2,-1))
a.shape
#(2,3)  

a
#array(
    [[1,2,3],
    []4,5,6]
]
)

###
a= a.reshape((-1,2))
a
array([
[1,2],
[3,4],
[5,6]
])

##取5
a[2,0]
## 将5换成55
a[2,0]= 55

zeros

a = zeros((3,3))

a

array([
      [0.,0.,0.],
      [0.,0.,0.],
      [0.,0.,0.],
])

ones

a = np.ones((2,3))

a

##
array([
    [1.,1.,1.],
    [1.,1.,1.],
])

full

a = np.full((3,3),0)  #3行3列,所有数据都是0
a = np.full((2,3),1) #2行3列,所有数据都是1

eye :单位矩阵

a = np.eye((3))  #左上右下为1,3行3列

array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

random.random:创建随机数组,取值在0--1之间

a=np.random.random((3,4))  #3行4列,0-1之间数字组成的

array([[0.31970217, 0.52454361, 0.93528294, 0.59955502],
       [0.47355245, 0.7775892 , 0.8112688 , 0.58033926],
       [0.20438656, 0.37185309, 0.89225405, 0.61406772]])

 

posted @ 2018-04-25 20:23  老王的农场  阅读(7576)  评论(0编辑  收藏  举报