Page 225 - AI Ver 1.0 Class 10
P. 225

OR

                 >>> from numpy import array as ary                 # this will import ONLY
                                                                    # arrays and referred as ary
                 Arrays are a collection of values of the same data types that can be arranged in one or more dimensions. They
                 can be numbers, characters, Booleans, etc. An array of one dimension is called a Vector, an array having two
                 dimensions is called a Matrix and an array with multiple dimensions is called as n-dimensional array.

                 In NumPy we can create n-dimensional arrays and are considered as an alternative to Python lists because they
                 allow faster access in reading and writing items effectively and efficiently.
                 So, if we compare NumPy-Arrays and Python-List then:

                    • Array is a collection of homogeneous values whereas list is a collection of heterogeneous values.
                    • In arrays data of one type does not support data of another type whereas in list it works perfectly by using data
                   of one type by converting into another data type.
                    • Arrays can be accessed only through package NumPy and occupies less memory space whereas list occupies
                   more memory space and can be accessed directly in Python without any package support.

                    • In arrays the mathematical operators can be directly used whereas in list the mathematical operators cannot be
                   used directly on it instead the operator needs to be used separately on individual elements.
                    • Arrays are mainly used for mathematical operations where lists are mainly used for data management.
                    • Syntax of creating an array is:

                     import numpy

                     marks = numpy.array([34,23,41,42])
                     Syntax of creating a list is:

                     marks = [34,23,41,42]

                 Creating an Array using NumPy

                 We can create different arrays using NumPy. Let us discuss about some of them.
                    • Creating a one-dimensional array:

                      import numpy

                    rollno = numpy.array([1, 2, 3])
                    print(rollno)
                     Output will be:

                     [1 2 3]
                    • Create a sequential 1 D array with values as multiples of 10 from 10 to 100:

                     import numpy as np
                     a = np.arange(10,101,10)

                     print(a)
                     Output will be:
                     [10 20 30 40 50 60 70 80 90 100]



                                                                                              Data Science  223
   220   221   222   223   224   225   226   227   228   229   230