Search this site
Embedded Files
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
  • More
    • Home
    • Courses
    • Contact

日本語  ❯

Lesson 5    ❮    Lesson List    ❮    Top Page

❯  5.1  NumPy Arrays      

5.2 Index and Reshape

5.3 Universal Functions     

5.4 Join, Split, and Transpose

5.5 Search and Filter      

⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳  Video   7m 22s
☷  Interactive readings   5m
✑  Practice 5.1 (G Colab)   25m

Making a NumPy Array

NumPy (Numerical Python) is a library used for working with arrays and math operations. It is usually imported under the np alias. 

The array object in NumPy is called ndarray, it is up to 50x faster than lists and provides a lot of support functions. 

np.array()
converts a list/tuples into an ndarray.

Checking the Dimension and Shape of ndarray

A dimension is one level of nested arrays.

array.ndim
returns the dimension of array:
0-D array: A number/scalar
1-D array: a list with numbers.
2-D array: a list containing lists, a matrix.

array.shape
return the shape of an array.
(as a tuple, like (2, 6))

array.size
return the number of elements in the array.

Generating Arrays with Number Array

Similar to range, NumPy has some functions that make it easy to generate numbers:

np.arange(num)
returns integers from 0 to num-1. 

np.arange(minnum, maxnum)
returns integers from minnum to maxnum-1. 

np.full(shape, fill_value)
returns a new array of the given shape (e.g., (2,3) or 2) filled with fill_value.

np.empty(shape)
returns a new array of the given shape with no value in it.

You can use round(num) to round a number to num places decimal.

Generating a Random Array

NumPy offers the random module to work with random numbers.  

np.random.rand()
returns random float from 0 to 1.

np.random.randint(num)
returns random integers from 0 to num-1.

np.random.randint(minnum, maxnum)
returns random integers from minnum to maxnum-1. 

Adding an extra argument size=shape will return a random array of the given shape.

©2023. All rights reserved.  Samy Baladram,
Graduate Program in Data Science - GSIS - Tohoku University
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse