0% found this document useful (0 votes)
3 views5 pages

Numpy Tutorial Notes (Important Functions in Numpy)

The document provides an overview of various NumPy functions related to random number generation, aggregation, and conditional logic. Key functions include np.random.random() for generating random floats, np.random.seed() for reproducibility, and np.where() for conditional array modifications. It also summarizes functions for finding maximum/minimum values, sorting arrays, and calculating percentiles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Numpy Tutorial Notes (Important Functions in Numpy)

The document provides an overview of various NumPy functions related to random number generation, aggregation, and conditional logic. Key functions include np.random.random() for generating random floats, np.random.seed() for reproducibility, and np.where() for conditional array modifications. It also summarizes functions for finding maximum/minimum values, sorting arrays, and calculating percentiles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Video X: Random Functions, Aggregation, and

Conditional Logic in NumPy

🔹 1. Importing NumPy
import numpy as np

● Common alias used is np for convenience.

🔹 2. np.random.random()
np.random.random()

● Returns a random float in the range [0.0, 1.0).

● Each time it's called, it gives a new value.

Example:

np.random.random() # Might return 0.558...

🔹 3. np.random.seed()
np.random.seed(1)
np.random.random()

● Sets the random seed for reproducibility.

● Ensures that the same random number is generated each time after setting the
seed.

Example:

np.random.seed(1)
np.random.random() # Always gives 0.417022004702574

🔹 4. np.random.uniform(low, high, size)


np.random.uniform(1, 100, 10).reshape(2, 5)

● Returns a random float array between low and high.

● size defines how many values to generate.

Example:

np.random.uniform(1, 100, 10).reshape(2,5)


# Returns a 2x5 array with random floats between 1 and 100

🔹 5. np.random.randint(low, high, size)


np.random.randint(1, 100, 10).reshape(2,5)

● Generates random integers between low and high (exclusive).

● Can produce multi-dimensional arrays using .reshape().

Example:

np.random.randint(1, 100, 10)


# Returns: [19, 85, 12, 29, ...]

🔹 6. np.max() & np.min()


a = np.random.randint(1, 100, 10)
np.max(a)
np.min(a)

● Returns maximum and minimum values in an array.


Example:

np.max(a) # e.g., 97
np.min(a) # e.g., 8

🔹 7. np.argmax() & np.argmin()


np.argmax(a)
np.argmin(a)

● Returns the index of the max/min value in the array.

Example:

np.argmax(a) # e.g., 1 → max value is at index 1

🔹 8. Modifying Array Elements Conditionally (In-Place)


a[a % 2 != 0] = -1

● Changes odd values in array a to -1.

Original a:

[95, 97, 87, 14, 10, 8, 64, 62, 23, 58]

Modified a:

[-1, -1, -1, 14, 10, 8, 64, 62, -1, 58]

🔹 9. np.where(condition, true_value, false_value)


out = np.where(a % 2 != 0, -1, a)

● Creates a new array based on a condition.


● Does not modify the original array.

Example:

np.where(a % 2 != 0, -1, a)
# Replaces odd numbers with -1

🔹 10. np.random.randint() + Sorting


b = np.random.randint(1, 50, 10)
b = np.sort(b)

● Creates and sorts an array of random integers.

Before sorting:

[8, 4, 7, 22, 44, 13, 27, ...]

After sorting:

[4, 5, 7, 8, 13, 22, 25, ...]

🔹 11. np.percentile(array, q)
np.percentile(b, 25)

● Returns the value below which q% of data lies.

Example:

np.percentile(b, 25) # Returns value such that 25% of elements are


smaller than it

🔹 12. Type Conversion Examples


np.int64(1) # Converts to 64-bit integer
np.float64(5.5) # Converts to 64-bit float

Summary of Key Functions


Function Purpose

random() Random float [0,1)

seed() Fixes random output

uniform() Random floats in a given range

randint() Random integers in a range

max() / min() Maximum / Minimum value

argmax() / argmin() Index of max/min value

where() Conditional value setting (returns new array)

sort() Returns sorted array

percentile() Value below which a percentage of data lies

You might also like