numpy使用fromstring创建矩阵的实例

yipeiwu_com5年前Python基础

使用字符串创建矩阵是一个很实用的功能,之前自己尝试了很多次的小功能使用这个方法就能够简单实现。

创建长度为16的字符串,是为了方便能够在各种数据类型之间转换。

>>> s = "mytestfromstring"
>>> len(s)
16

这个功能其实是比较让我兴奋的一个小功能,因为这个简单的转换实现了ASCII码的转换

>>> np.fromstring(s,dtype=np.int8)
array([109, 121, 116, 101, 115, 116, 102, 114, 111, 109, 115, 116, 114,
    105, 110, 103], dtype=int8)
 
>>> np.fromstring(s,dtype=np.int16)
array([31085, 25972, 29811, 29286, 28015, 29811, 26994, 26478], dtype=int16)
>>> np.fromstring(s,dtype=np.int32)
array([1702132077, 1919317107, 1953721711, 1735289202], dtype=int32)
>>> np.fromstring(s,dtype=np.int64)
array([8243404206920464749, 7453010373645659503])
>>> np.fromstring(s,dtype=np.single)
array([ 7.21560833e+22,  4.56462917e+30,  7.71452462e+31,
     1.12586835e+24], dtype=float32)
>>> np.fromstring(s,dtype=np.float32)
array([ 7.21560833e+22,  4.56462917e+30,  7.71452462e+31,
     1.12586835e+24], dtype=float32)
>>> np.fromstring(s,dtype=np.float64)
array([ 1.19783602e+243,  1.69375610e+190])

float默认的数据宽度是64,我使用的是Mac,本身是64位的,倒不知是不是跟操作系统有关?

>>> np.fromstring(s,dtype=np.float)
array([ 1.19783602e+243,  1.69375610e+190])

以上这篇numpy使用fromstring创建矩阵的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Windows下安装python2.7及科学计算套装

Windows下安装python2.7及科学计算套装

安装环境及说明 操作系统:64位win7 以下所有安装包已经被我打包至网盘,请移步到 http://www.colafile.com/file/4591550进行下载 因为在64位win...

python实现机器人行走效果

本文实例为大家分享了python实现机器人行走效果的具体代码,供大家参考,具体内容如下 #! /usr/bin/env python3 # -*- coding: utf-8 -*...

pytorch 常用线性函数详解

Pytorch的线性函数主要封装了Blas和Lapack,其用法和接口都与之类似。 常用的线性函数如下: 函数 功能...

解决Tensorflow使用pip安装后没有model目录的问题

解决Tensorflow使用pip安装后没有model目录的问题

在使用pip安装Tensorflow后,在其目录中没有找到model目录,重复安装了两遍依然没有,原因未知。 于是,使用源码安装的方法: (1)收下,使用git clone源码工程: g...

Python 3.6打包成EXE可执行程序的实现

Python 3.6打包成EXE可执行程序的实现

1、下载pyinstaller python 3.6 已经自己安装了pip,所以只需要执行 pip install pyinstaller就可以了 2、打包程序 进入到你你需要打包的目...