1.2 NumPy Array Creation

来源:互联网 发布:java中数组怎么输入 编辑:程序博客网 时间:2024/05/16 06:14

Array Creation


There are several ways to create arrays.

For example, you can create an array from a regular Python list or tuple using the array function. The type of the resulting array is deduced(推断) from the type of the elements in the sequences.

>>> import numpy as np>>> a=np.array([2,3,4])>>> aarray([2, 3, 4])>>> a.dtypedtype('int32')>>> b=np.array([1.2,3.5,5.1])>>> b.dtypedtype('float64')

A frequent error consists in calling array with multiple numeric arguments, rather than providing a single list of numbers as an argument.

>>> a=np.array(1,2,3,4) # wrongTraceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: only 2 non-keyword arguments accepted>>> a=np.array([1,2,3,4]) # right

array transforms(转换) sequences of sequences into two-dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on.

>>> b=np.array([(1.5,2,3),(4,5,6)])>>> barray([[ 1.5,  2. ,  3. ],       [ 4. ,  5. ,  6. ]])>>>>>> c=np.array([[1,2,3],[7,8,9]])>>> carray([[1, 2, 3],       [7, 8, 9]])

The type of the array can also be explicitly specified at creation time:

>>> c=np.array([[1.5,2,3],[4,5,6]],dtype=complex)>>> carray([[ 1.5+0.j,  2.0+0.j,  3.0+0.j],       [ 4.0+0.j,  5.0+0.j,  6.0+0.j]])>>> c.dtypedtype('complex128')

Often, the elements of an array are originally unknown, but its size is known. Hence(因此), NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive(昂贵的) operation.

The function zeros creates an array full of zeros, the function ones creates an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.

>>> np.zeros((3,4))array([[ 0.,  0.,  0.,  0.],       [ 0.,  0.,  0.,  0.],       [ 0.,  0.,  0.,  0.]])>>> np.ones((2,3,4),dtype=np.int16)array([[[1, 1, 1, 1],        [1, 1, 1, 1],        [1, 1, 1, 1]],       [[1, 1, 1, 1],        [1, 1, 1, 1],        [1, 1, 1, 1]]], dtype=int16)>>> np.empty((2,3))array([[ 1.5,  2. ,  3. ],       [ 4. ,  5. ,  6. ]])

To create sequences of numbers, NumPy provides a function analogous(相似的) to range that returns arrays instead of lists.

>>> np.arange(10,30,5)array([10, 15, 20, 25])>>> np.arange(0,2,0.3)array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])

When arange is used with floating point arguments, it is generally not possible to predict(预测) the number of elements obtained, due to the finite(有限的) floating point precision. For this reason, it is usually better to use the function linspace that receives as an argument the number of elements that we want, instead of the step:

>>> from numpy import pi>>> np.linspace(0,2,9) # 9 numbers from 0 to 2array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])>>> x=np.linspace(0,2*pi,100) 100 numbers from 0 to 2*pi>>> f=np.sin(x)

提示:np.linspace函数返回一个等差数列。

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 男的尿尿疼是什么原因 会阴部疼痛 女生小便疼 尿液结晶 马眼疼 尿道疼是什么原因 胳膊疼痛是怎么回事 小便胀痛怎么办 上火尿频 疼是怎么回事 脚后跟疼痛怎么回事 为什么性过后第二天尿频尿痛 尿白色尿 无症状性菌尿 尿发白是怎么回事 尿发白浑浊 尿里有白色粉末 尿出白色颗粒 尿微量蛋白 尿发白 尿白色的 黑色的尿 纯白色的尿 白色的尿 尿微量白蛋白 尿微量白蛋白偏高 尿微量白蛋白偏 尿微量白蛋白高怎么回事 尿微量白蛋白怎么回事 尿微量白蛋白正常值 年轻人尿滴白能自愈吗 尿微量白蛋白能自愈吗 尿白蛋白肌酐比值 尿微量白蛋白肌酐比值 尿微量白蛋白100严重吗 尿末滴白可以自愈吗 尿滴白什么样子 早上尿滴白 尿滴白会不会影响生育 尿口滴白是什么原因 尿滴白能自愈吗我才十六