• We just launched and are currently in beta. Join us as we build and grow the community.

NumPy Array Creation

srs2010

Platform Insights Analyst
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
79
Likes
196
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial you will learn:

  • NumPy Array
  • Dimensions of NumPy Array
  • Common Functions of NumPy Array

NumPy Array

Array in NumPy is a data structure that is similar to Python lists but it's a lot more powerful since it allows us to manage N number of dimensions which helps us in making different mathematical calculations. We can define same type of elements in a NumPy array. The elements of a NumPy array are actually dtype objects ( data-type objects) and the array object in NumPy itself is called ndarray. We can use simple array function to create an a NumPy ndarray object. Let’s take a look at an example that creates NumPy array object and print the contents.

  1. import

    numpy as

    np
  2. nparr =

    np.array

    (

    [

    "Python"

    ,

    "Test"

    ,

    "Programming"

    ]

    )
  3. print

    (

    "** Numpy Array Creation **"

    )
  4. print

    (

    "Content of NumPy array: "

    ,

    nparr)

Dimensions of NumPy Array

One of the biggest advantages of NumPy array is that it allows us to create arrays that can have N-Dimensions. It means that we can have a very deeply nested array. Let’s have a look at examples in which we will create arrays that are 2 level, 3 level and 4 levels nested.

Example

In this example we are creating an array which is 2-Dimensional. For that we write both arrays in a NumPy array function separated by a comma.

  1. nparr2 =

    np.array

    (

    [

    [

    8

    ,

    9

    ,

    5

    ]

    ,

    [

    5

    ,

    4

    ,

    7

    ]

    ,

    [

    7

    ,

    7

    ,

    9

    ]

    ,

    [

    4

    ,

    4

    ,

    4

    ]

    ]

    )
  2. print

    (

    "\n

    \n

    Contents of 2-D NumPy array: \n

    "

    ,

    nparr2)

In this example we are creating 2-D array by creating an array within an array.

  1. nparr2 =

    np.array

    (

    [

    [

    8

    ,

    9

    ,

    5

    ]

    ,

    [

    5

    ,

    4

    ,

    7

    ]

    ,

    [

    7

    ,

    7

    ,

    9

    ]

    ,

    [

    4

    ,

    4

    ,

    4

    ]

    ]

    )
  2. print

    (

    "\n

    \n

    Contents of 3-D NumPy array: \n

    "

    ,

    nparr2)

Here we are creating 3-D array by creating an array within an array up to three levels.

  1. nparr3 =

    np.array

    (

    [

    [

    [

    8

    ,

    9

    ,

    5

    ,

    5

    ]

    ,

    [

    4

    ,

    4

    ,

    4

    ,

    4

    ]

    ]

    ,

    [

    [

    7

    ,

    6

    ,

    6

    ,

    3

    ]

    ,

    [

    7

    ,

    7

    ,

    9

    ,

    1

    ]

    ]

    ]

    )
  2. print

    (

    "\n

    \n

    Contents of 4-D NumPy array: \n

    "

    ,

    nparr3)

Common Functions of NumPy Array

Now let’s have a look at the usage of some common functions of NumPy. NumPy gives us functions that can fill the array with ones, zeros and random numbers. These functions are simple yet very powerful while developing Machine Learning algorithms in Python.

  1. print

    (

    "\n

    \n

    Array full of 4"

    ,

    np.full

    (

    (

    3

    ,

    3

    )

    ,

    4

    )

    )
  2. print

    (

    "\n

    Random number array 3 rows 4cols"

    ,

    np.random

    .random

    (

    (

    3

    ,

    4

    )

    )

    )
  3. print

    (

    "\n

    Create Array of ones"

    ,

    np.ones

    (

    (

    2

    ,

    4

    )

    )

    )
  4. print

    (

    "\n

    Create Array of zeros"

    ,

    np.zeros

    (

    (

    3

    ,

    3

    )

    ,

    dtype=

    np.int16

    )

    )

Book traversal links for NumPy Array Creation

  • ‹ NumPy Accessing Arrays
  • Up
  • NumPy Iterator ›


Download
You must upgrade your account or reply in the thread to view hidden text.
 

440,010

316,559

316,568

Top