详解python中Numpy的属性与创建矩阵

yipeiwu_com5年前Python基础

ndarray.ndim:维度

ndarray.shape:形状

ndarray.size:元素个数

ndarray.dtype:元素数据类型

ndarray.itemsize:字节大小

创建数组:

a = np.array([2,23,4]) 
# list 1d
print(a)
# [2 23 4]

指定数据类型:

a = np.array([2,23,4],dtype=np.int)
print(a.dtype)
# int 64

dtype可以指定的类型有int32,float,float32,后面不跟数字默认64

a = np.zeros((3,4)) # 数据全为0,3行4列
"""

 

a = np.ones((3,4),dtype = np.int)  # 数据为1,3行4列
a = np.empty((3,4)) # 数据为empty,3行4列

empty类型:初始内容随机,取决于内存的状态

a = np.arange(10,20,2) # 10-19 的数据,2步长
a = np.arange(12).reshape((3,4))  # 3行4列,0到11

reshape修改数据形状,如3行4列

a = np.linspace(1,10,20)  # 开始端1,结束端10,且分割成20个数据,生成线段

linspace可以确定数据的数量,而arrage不能确定数据的数量,同时,linspace也可以使用reshape定义结构。

相关文章

Python中创建字典的几种方法总结(推荐)

1、传统的文字表达式: >>> d={'name':'Allen','age':21,'gender':'male'} >>> d {'age':...

Python随机函数random()使用方法小结

1. random.random()   random.random()方法返回一个随机数,其在0至1的范围之内,以下是其具体用法:   import random   print...

布同自制Python函数帮助查询小工具

比如在学习list、tuple、dict、str、os、sys等模组的时候,利用Python的自带文档可以很快速的全面的学到那些处理的函数。所以这个自带文档功能能够给出学者带来很大的方便...

django重新生成数据库中的某张表方法

今天有碰到这种情况,数据库中有张表没办法通过migration来更改, migrate时报 django.db.utils.OperationalError: (1050, “Table...

Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str

在python的Beautiful Soup 4 扩展库的使用过程中出现了 TypeError: list indices must be integers or slices, no...