We create arrays to store and work with many numbers together easily. Arrays help us do math and analyze data faster.
Array creation (array, arange, linspace) in Data Analysis Python
import numpy as np # Create an array from a list array = np.array([1, 2, 3, 4]) # Create an array with numbers from start to stop (exclusive) with step size arange_array = np.arange(start, stop, step) # Create an array with a specific number of points between start and stop (inclusive) linspace_array = np.linspace(start, stop, num=num_points)
np.array converts a list or tuple into an array.
np.arange creates numbers with a fixed step size but excludes the stop value.
import numpy as np # Empty array from empty list empty_array = np.array([]) print(empty_array)
import numpy as np # Array with one element single_element_array = np.array([10]) print(single_element_array)
import numpy as np # arange from 0 to 5 with step 1 range_array = np.arange(0, 5, 1) print(range_array)
import numpy as np # linspace from 0 to 1 with 5 points linspace_array = np.linspace(0, 1, 5) print(linspace_array)
This program shows how to create arrays using three methods: from a list, with fixed steps, and with a fixed number of points.
import numpy as np # Create an array from a list numbers_list = [2, 4, 6, 8] numbers_array = np.array(numbers_list) print("Array from list:", numbers_array) # Create an array using arange from 1 to 10 with step 2 arange_array = np.arange(1, 10, 2) print("Arange array:", arange_array) # Create an array using linspace from 0 to 5 with 6 points linspace_array = np.linspace(0, 5, 6) print("Linspace array:", linspace_array)
Time complexity: Creating arrays is fast and usually O(n), where n is the number of elements.
Space complexity: Arrays use memory proportional to the number of elements stored.
Common mistake: Using np.arange with floating point steps can cause unexpected results due to rounding errors. Use np.linspace for precise spacing.
Use np.array to convert existing data. Use np.arange when you want a range with a fixed step size. Use np.linspace when you want a fixed number of points between two values.
Arrays store many numbers together for easy math and analysis.
np.array converts lists to arrays.
np.arange creates sequences with fixed steps, excluding the stop value.
np.linspace creates sequences with a fixed number of points including start and stop.
Practice
np.arange(1, 5) produce in Python's NumPy?Solution
Step 1: Understand np.arange parameters
np.arange(start, stop)creates values from start up to but not including stop.Step 2: Apply to np.arange(1, 5)
Values start at 1 and go up to 4 (5 is excluded), so the array is [1, 2, 3, 4].Final Answer:
An array with values [1, 2, 3, 4] -> Option DQuick Check:
arange excludes stop = [1, 2, 3, 4] [OK]
- Including the stop value in the output
- Starting from 0 instead of the given start
- Confusing arange with linspace
[10, 20, 30]?Solution
Step 1: Recall np.array syntax
np.array()takes a single list or iterable as input to create an array.Step 2: Check each option
np.array([10, 20, 30]) correctly passes a list inside brackets. np.array(10, 20, 30) passes separate arguments, which is invalid. np.arange([10, 20, 30]) uses arange incorrectly. np.linspace(10, 20, 30) uses linspace which is for ranges, not lists.Final Answer:
np.array([10, 20, 30]) -> Option AQuick Check:
np.array needs a list inside brackets [OK]
- Passing multiple arguments instead of a list
- Using arange or linspace to convert lists
- Forgetting brackets around the list
import numpy as np arr = np.linspace(0, 1, 5) print(arr)
Solution
Step 1: Understand np.linspace parameters
np.linspace(start, stop, num)creates num evenly spaced points from start to stop inclusive.Step 2: Apply to np.linspace(0, 1, 5)
It creates 5 points between 0 and 1: 0.0, 0.25, 0.5, 0.75, 1.0.Final Answer:
[0. 0.25 0.5 0.75 1. ] -> Option CQuick Check:
linspace includes stop and divides evenly [OK]
- Confusing linspace with arange step behavior
- Expecting stop to be excluded
- Wrong number of points generated
import numpy as np arr = np.arange(0, 10, 3.5) print(arr)
Solution
Step 1: Check np.arange with float steps
NumPy's arange supports float step values, so 3.5 is valid.Step 2: Predict output of np.arange(0, 10, 3.5)
Values start at 0, then 3.5, then 7.0. Next would be 10.5 which exceeds stop 10, so stops.Final Answer:
The code runs correctly and prints [0. 3.5 7.] -> Option BQuick Check:
arange accepts float steps and excludes stop [OK]
- Assuming arange only works with integers
- Expecting stop value to be included
- Forgetting to import numpy
Solution
Step 1: Understand the requirement
We need 4 numbers starting at 2 and ending at 5, including both ends.Step 2: Check np.arange options
np.arange(2, 5, 1) creates [2, 3, 4], only 3 numbers and excludes 5. np.arange(2, 6, 2) creates [2, 4], wrong number of elements and values.Step 3: Check np.linspace options
np.linspace(2, 5, 4) creates 4 evenly spaced numbers including 2 and 5: [2.0, 3.0, 4.0, 5.0]. np.linspace(2, 5, 3) creates only 3 numbers.Final Answer:
np.linspace(2, 5, 4) -> Option AQuick Check:
linspace fixes number of points including ends [OK]
- Using arange which excludes stop and depends on step size
- Choosing wrong number of points in linspace
- Confusing arange and linspace behavior
Ready to prove it?
Pass the test and this pattern moves unknown
