Python-Numpy多维数组--来自现有数据的数据,来自数值范围的数据

一、NumPy 来自现有数据的数组

1.numpy.asarray此函数类似于numpy.array,除了它有较少的参数。 这个例程对于将 Python 序列转换为ndarray非常有用。

numpy.asarray(a, dtype = None, order = None)

构造器接受下列参数:

序号 参数及描述
1. a 任意形式的输入参数,比如列表、列表的元组、元组、元组的元组、元组的列表
2. dtype 通常,输入数据的类型会应用到返回的ndarray
3. order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组

下面的例子展示了如何使用asarray函数:

DEMO 1

# 将列表转换为 ndarray
import numpy as np
x = [1,2,3]
a = np.asarray(x)
print a
输出如下:[1 2 3]

DEMO 2

# 设置了 dtype
import numpy as np
x = [1,2,3]
a = np.asarray(x, dtype = float)
print a
输出如下:[ 1. 2. 3.]

DEMO 3

# 来自元组的 ndarray
import numpy as np
x = (1,2,3)
a = np.asarray(x)
print a
输出如下:[1 2 3]

DEMO 4

# 来自元组列表的 ndarray
import numpy as np
x = [(1,2,3),(4,5)]
a = np.asarray(x)
print a
输出如下:[(1, 2, 3) (4, 5)]

2.numpy.frombuffer此函数将缓冲区解释为一维数组。 暴露缓冲区接口的任何对象都用作参数来返回ndarray

numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)

构造器接受下列参数:

序号 参数及描述
1. buffer 任何暴露缓冲区借口的对象
2. dtype 返回数组的数据类型,默认为float
3. count 需要读取的数据数量,默认为-1,读取所有数据
4. offset 需要读取的起始位置,默认为0

DEMO

下面的例子展示了frombuffer函数的用法。
import numpy as np
s = 'Hello World'
a = np.frombuffer(s, dtype = 'S1')
print a
输出如下:['H' 'e' 'l' 'l' 'o' ' ' 'W' 'o' 'r' 'l' 'd']

3.numpy.fromiter函数从任何可迭代对象构建一个ndarray对象,返回一个新的一维数组。

numpy.fromiter(iterable, dtype, count = -1)

构造器接受下列参数:

序号 参数及描述
1. iterable 任何可迭代对象
2. dtype 返回数组的数据类型
3. count 需要读取的数据数量,默认为-1,读取所有数据

以下示例展示了如何使用内置的range()函数返回列表对象。 此列表的迭代器用于形成ndarray对象。

DEMO 1

# 使用 range 函数创建列表对象
import numpy as np
list = range(5)
print list
输出如下:[0, 1, 2, 3, 4]

DEMO2

# 从列表中获得迭代器
import numpy as np
list = range(5)
it = iter(list)
# 使用迭代器创建 ndarray
x = np.fromiter(it, dtype = float)
print x
输出如下:[0. 1. 2. 3. 4.]

二、NumPy - 来自数值范围的数组

1.numpy.arange这个函数返回ndarray对象,包含给定范围内的等间隔值。​​​​​​​

numpy.arange(start, stop, step, dtype)

构造器接受下列参数:

序号 参数及描述
1. start 范围的起始值,默认为0
2. stop 范围的终止值(不包含)
3. step 两个值的间隔,默认为1
4. dtype 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型。

下面的例子展示了如何使用该函数:

DEMO 1

import numpy as np
x = np.arange(5)
print x
输出如下:[0 1 2 3 4]

DEMO 2​​​​​​​

import numpy as np
# 设置了 dtype
x = np.arange(5, dtype = float)
print x
输出如下:[0. 1. 2. 3. 4.]

DEMO 3

# 设置了起始值和终止值参数
import numpy as np
x = np.arange(10,20,2)
print x
输出如下:[10 12 14 16 18]

3.numpy.linspace此函数类似于arange()函数。 在此函数中,指定了范围之间的均匀间隔数量,而不是步长。 此函数的用法如下。

numpy.linspace(start, stop, num, endpoint, retstep, dtype)

构造器接受下列参数:

序号 参数及描述
1. start 序列的起始值
2. stop 序列的终止值,如果endpointtrue,该值包含于序列中
3. num 要生成的等间隔样例数量,默认为50
4. endpoint 序列中是否包含stop值,默认为ture
5. retstep 如果为true,返回样例,以及连续数字之间的步长
6. dtype 输出ndarray的数据类型

下面的例子展示了linspace函数的用法。

DEMO1

import numpy as np
x = np.linspace(10,20,5)
print x
输出如下:[10. 12.5 15. 17.5 20.]

DEMO2​​​​​​​

# 将 endpoint 设为 false
import numpy as np
x = np.linspace(10,20, 5, endpoint = False)
print x
输出如下:
[10. 12. 14. 16. 18.]

DEMO 3​​​​​​​

# 输出 retstep 值
import numpy as np
x = np.linspace(1,2,5, retstep = True)
print x
# 这里的 retstep 为 0.25
输出如下:(array([ 1. , 1.25, 1.5 , 1.75, 2. ]), 0.25)

4.numpy.logspace此函数返回一个ndarray对象,其中包含在对数刻度上均匀分布的数字。 刻度的开始和结束端点是某个底数的幂,通常为 10。

numpy.logscale(start, stop, num, endpoint, base, dtype)

logspace函数的输出由以下参数决定:

序号 参数及描述
1. start 起始值是base ** start
2. stop 终止值是base ** stop
3. num 范围内的数值数量,默认为50
4. endpoint 如果为true,终止值包含在输出数组当中
5. base 对数空间的底数,默认为10
6. dtype 输出数组的数据类型,如果没有提供,则取决于其它参数

下面的例子展示了logspace函数的用法。

DEMO 1​​​​​​​

import numpy as np
# 默认底数是 10
a = np.logspace(1.0, 2.0, num = 10)
print a
输出如下:[ 10. 12.91549665 16.68100537 21.5443469 27.82559402, 35.93813664 46.41588834 59.94842503 77.42636827 100. ]

DEMO 2​​​​​​​

# 将对数空间的底数设置为 2
import numpy as np
a = np.logspace(1,10,num = 10, base = 2)
print a
输出如下:[ 2. 4. 8. 16. 32. 64. 128. 256. 512. 1024.]

​​​​​​​

猜你喜欢

转载自blog.csdn.net/Odyssues_lee/article/details/85165271