numpy之-快速创建ndarray

接上篇文章,本章主要说明ndarray的快速创建对象
创建ndarray对象除了使用np.array还有一下几种方式快速创建。

1. 创建空的nadrray对象,因为没有赋值,所以会随机生成一些值。
np.empty((4,4))
array([[ 0.00000000e+000,  0.00000000e+000, -4.94065646e-323,
         0.00000000e+000],
       [ 2.12199579e-314,  0.00000000e+000,  0.00000000e+000,
         0.00000000e+000],
       [ 1.77229088e-310,  3.50977866e+064,  0.00000000e+000,
         0.00000000e+000],
       [             nan,              nan,  3.50977942e+064,
         0.00000000e+000]])
>>> np.empty((4,))
array([ 0.00000000e+000, -1.73059404e-077,  9.88131292e-324,
        2.78134232e-309])
  • 指定类型: dtype='int'或者'uint'等
>>> np.empty((4,4),dtype='int')
array([[                   0,                    0, -9223372036854775798,
                           0],
       [          4294967296,                    0,                    0,
                           0],
       [      35871566856192,  5572452859464646656,                    0,
                           0],
       [                  -1,     -140187915007369,  5572452860762084442,
                           0]])
>>> np.empty((4,4),dtype='uint')
array([[                   0,                    0,   180366274849603603,
                  4402738160],
       [          4390252648, 17045276415608740984,           4402742864,
                  4390152352],
       [                   0,                    0,                    0,
                           0],
       [                   0,                    0,                    0,
                           0]], dtype=uint64)

2. 生成全为0的ndarray对象(类似全为0的行列式):
>>> np.zeros((4,4),dtype='uint')
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=uint64)
>>> np.zeros((4,4),dtype='int')
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
3. 全为1的ndarray对象,(类似全为0的行列式):
>>> np.ones((4,4),dtype='int')
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])
>>> np.ones((4,4),dtype='uint')
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]], dtype=uint64)
4. 生成对角线上有值的ndarray对象:
>>> np.eye(4)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
>>> np.eye(4,dtype='int')
array([[1, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 1]])
5. 通过已有数组列表创建ndarray对象,类似于np.array()
  • 使用np.asarray(),创建普通ndarray对象
>>> list = [1,2,3,4,5]
>>> dt = np.asarray(list)
>>> print(dt)
[1 2 3 4 5]
>>> dt = np.asarray(list,dtype='float')
>>> print(dt)
[1. 2. 3. 4. 5.]
6. 通过已有数据通过流的范式读取,转化为ndarray对象
  • 使用np.frombuffer(),创建ndarray对象
>>> strings = b'this is a string'
>>> dt = np.frombuffer(strings,dtype='S1')
>>> print(dt)
[b't' b'h' b'i' b's' b' ' b'i' b's' b' ' b'a' b' ' b's' b't' b'r' b'i'
 b'n' b'g']
7. 通过可迭代对象中读取,转化为ndarray对象
  • 使用np.forminter(),创建ndarray对象
>>> a = range(4)
>>> dt = np.fromiter(iter(a),dtype='float')
>>> print(dt)
[0. 1. 2. 3.]
8. 从取值范围中生成ndarray对象
  • 使用arrange创建ndarray对象
参数的默认值如下:
start:开始,stop:结束,step:步长,dtype:数据类型
np.arange(start,stop,step=1,dtype=None)
>>> dt = np.arange(1,10)
>>> print(dt)
[1 2 3 4 5 6 7 8 9]
  • 使用linspace创建等差数列ndarray对象
参数的默认值如下:
start:开始,stop:结束,num:默认个数,endpoint:是否显示最后一个,retstep:显示步长,dtype:数据类型
np.linspace(start,stop,num=50,endpoint=False,retstep,dtype=None)
>>> dt = np.linspace(1,10)
>>> print(dt)
[ 1.          1.18367347  1.36734694  1.55102041  1.73469388  1.91836735
  2.10204082  2.28571429  2.46938776  2.65306122  2.83673469  3.02040816
  3.20408163  3.3877551   3.57142857  3.75510204  3.93877551  4.12244898
  4.30612245  4.48979592  4.67346939  4.85714286  5.04081633  5.2244898
  5.40816327  5.59183673  5.7755102   5.95918367  6.14285714  6.32653061
  6.51020408  6.69387755  6.87755102  7.06122449  7.24489796  7.42857143
  7.6122449   7.79591837  7.97959184  8.16326531  8.34693878  8.53061224
  8.71428571  8.89795918  9.08163265  9.26530612  9.44897959  9.63265306
  9.81632653 10.        ]
>>> dt = np.linspace(start=1,stop=10,num=10)
>>> print(dt)
[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]

  • 使用logspace创建等比数列ndarray对象
参数的默认值如下:
start:开始,stop:结束,num:默认个数,endpoint:是否显示最后一个,retstep:显示步长,dtype:数据类型
np.logspace(start,stop,num=50,endpoint=False,retstep,dtype=None)

>>> print(dt)
[1.00000000e+01 1.52641797e+01 2.32995181e+01 3.55648031e+01
 5.42867544e+01 8.28642773e+01 1.26485522e+02 1.93069773e+02
 2.94705170e+02 4.49843267e+02 6.86648845e+02 1.04811313e+03
 1.59985872e+03 2.44205309e+03 3.72759372e+03 5.68986603e+03
 8.68511374e+03 1.32571137e+04 2.02358965e+04 3.08884360e+04
 4.71486636e+04 7.19685673e+04 1.09854114e+05 1.67683294e+05
 2.55954792e+05 3.90693994e+05 5.96362332e+05 9.10298178e+05
 1.38949549e+06 2.12095089e+06 3.23745754e+06 4.94171336e+06
 7.54312006e+06 1.15139540e+07 1.75751062e+07 2.68269580e+07
 4.09491506e+07 6.25055193e+07 9.54095476e+07 1.45634848e+08
 2.22299648e+08 3.39322177e+08 5.17947468e+08 7.90604321e+08
 1.20679264e+09 1.84206997e+09 2.81176870e+09 4.29193426e+09
 6.55128557e+09 1.00000000e+10]
 >>> dt = np.logspace(1,10,num=10)
>>> print(dt)
[1.e+01 1.e+02 1.e+03 1.e+04 1.e+05 1.e+06 1.e+07 1.e+08 1.e+09 1.e+10]

  • 创建使用np数组类型一致的对象:
    1,创建和a一致的全0全1数组,创建和a一样的其他数字类型的数组
>>> np.zeros_like(a)
>>> np.ones_like(a)
>>> np.full_like(a,1)
  • 修改数据类型,一种类型转为其他类型
>>> np.astype(np.int)

>>> np.astype(np.float)
  • 数组转为列表
>>> dt.tolist()

....待续

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 使用内置函数创建 ndarray NumPy 的一个非常节省时间的功能是使用内置函数创建 ndarray。借助这些...
    IntoTheVoid阅读 1,198评论 0 0
  • Numpy的组成与功能 Numpy(Numeric Python)可以被理解为一个用python实现的科学计算包,...
    不做大哥好多年阅读 4,555评论 0 10
  • 基础篇NumPy的主要对象是同种元素的多维数组。这是一个所有的元素都是一种类型、通过一个正整数元组索引的元素表格(...
    oyan99阅读 5,286评论 0 18
  • 介绍 NumPy 是一个 Python 包。 它代表 “Numeric Python”。 它是一个由多维数组对象和...
    喔蕾喔蕾喔蕾蕾蕾阅读 1,864评论 0 5
  • 寒云堆皱,冰栏冷岸, 鸠远冻枝头。 叶乱空廊,菊黄倾路, 红豆倚风楼。 千山旧、一江飞霁, 闲渡问横舟。 信马归来...
    牡丹员外阅读 218评论 0 2

友情链接更多精彩内容