Chapter 5: NumPy Creating Arrays

NumPy Creating Arrays — written as if a patient teacher is sitting next to you, showing examples on the screen, explaining why each method exists, when you should prefer one over another, and what beginners usually get wrong.

Let’s go slowly and thoroughly through the most important ways to create NumPy arrays.

Python

1. The most common starting point: np.array()

Python

Important things to notice:

  • All elements must be of compatible types (NumPy will usually convert them)
  • You can mix integers and floats → becomes float
Python

Common beginner mistake:

Python

2. Special creator functions – very frequently used

These are the functions you will type every single day.

np.zeros() – empty starting point, initialization

Python

Integer version (very common):

Python

np.ones()

Python

np.full() – fill with any value

Python

np.eye() – identity matrix (very useful in linear algebra)

Python
text

3. Sequence generators – extremely common

np.arange() – like Python range(), but returns array

Python

Warning students always forget:

Python

Safer alternatives when using floats:

Python

np.linspace() – “linear space” – most used for plotting & testing

Python

Key differences – memorize this table

Feature arange linspace
Includes endpoint? No (like range) Yes
Step / num points You give step size You give number of points
Good for floats? Can be dangerous Designed for floats, very safe
Most common use integer indices, loops plotting, generating test points

4. Random number generation – you will use these constantly

Python

Uniform random numbers [0, 1)

Python

Standard normal (Gaussian) distribution – mean=0, std=1

Python

Random integers

Python

Random choice from existing array (very useful)

Python

5. Quick realistic examples you will actually write

Example 1: Preparing feature matrix (machine learning style)

Python

Example 2: Time axis for plotting

Python

Example 3: Random test data

Python

Example 4: Image placeholder

Python

Summary – Which creation function when?

You want to… Best choice(s)
Create from existing data np.array()
Initialize empty array with zeros np.zeros(), np.zeros(…, dtype=int)
Initialize with ones np.ones()
Fill with same value np.full()
Need identity matrix np.eye()
Want integer sequence (0,1,2,3…) np.arange()
Want evenly spaced points (especially floats) np.linspace()
Need random numbers between 0 and 1 np.random.rand()
Need Gaussian / normal random numbers np.random.randn()
Need random integers np.random.randint()

Would you like to go deeper into any of these topics next?

  • Detailed differences between view vs copy when creating arrays
  • Setting specific dtypes (int8, float32, uint16, bool…)
  • Creating structured arrays (like mini data frames)
  • Very common patterns in machine learning / data science
  • Common mistakes & debugging tips when creating arrays

Just tell me what you want to focus on now! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *