What is the difference between ndarray and array in numpy?

前端 未结 5 1740
一个人的身影
一个人的身影 2020-11-28 01:27

What is the difference between ndarray and array in Numpy? And where can I find the implementations in the numpy source code?

5条回答
  •  醉话见心
    2020-11-28 01:58

    Just a few lines of example code to show the difference between numpy.array and numpy.ndarray

    Warm up step: Construct a list

    a = [1,2,3]
    

    Check the type

    print(type(a))
    

    You will get

    
    

    Construct an array (from a list) using np.array

    a = np.array(a)
    

    Or, you can skip the warm up step, directly have

    a = np.array([1,2,3])
    

    Check the type

    print(type(a))
    

    You will get

    
    

    which tells you the type of the numpy array is numpy.ndarray

    You can also check the type by

    isinstance(a, (np.ndarray))
    

    and you will get

    True
    

    Either of the following two lines will give you an error message

    np.ndarray(a)                # should be np.array(a)
    isinstance(a, (np.array))    # should be isinstance(a, (np.ndarray))
    

提交回复
热议问题