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

yipeiwu_com6年前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实时得到鼠标位置的示例讲解

对python实时得到鼠标位置的示例讲解

如下所示: #先下载pyautogui库,pip install pyautogui import os,time import pyautogui as pag try: wh...

python3读取MySQL-Front的MYSQL密码

前言 同样的套路又来了,继续尝试从配置文件中读取敏感的信息,这次轮到的是MySQL-Front。 MySQL-Front就一款开源的mysql管理工具,官方网站http://www.my...

利用pyshp包给shapefile文件添加字段的实例

在已有的shapefile文件的基础上增加字段: # -*- coding:gb2312 -*- import shapefile r=shapefile.Reader(r"C:...

python 获取文件列表(或是目录例表)

import os os.os.listdir(path) 然后再一个一个的分析文件和目录 通过和dos命令dir的巧妙结合,可以很轻松的做到这点,看示例 获取目录下所有文件方法 cmd...

Sanic框架配置操作分析

本文实例讲述了Sanic框架配置操作。分享给大家供大家参考,具体如下: 简介 Sanic是一个类似Flask的Python 3.5+ Web服务器,它的写入速度非常快。除了Flask之外...