Introduction to NumPy Libraries for Machine Learning
NumPy provides more than arrays.
Predicting a Printed Array
Before any machine learning model can learn from data, that data has to be in a form the library can accept. In scikit-learn, that form is the NumPy array. This article works code first: you read a short program, predict what it prints, and then use the explanation to check your prediction. The gap between your prediction and the real output is where understanding forms, so commit to a guess before you read on.
What do you think happens?
The program calls np.array on a list whose two items are each a list of three numbers: [[1, 2, 3], [4, 5, 6]]. What does it print?
Reveal answer
[[1 2 3]
[4 5 6]], two rows with three numbers eachEach inner list becomes one row of the resulting two-dimensional array. The outer list supplies the two rows, and each row contributes three elements, so the printed grid keeps the same nesting as the input.
Building an Array from Nested Lists
The function np.array takes a Python list and builds a NumPy array from it. When the list is nested, each inner list becomes one row of the result. The program below uses the same two-row, three-column structure described in the source material, so the value 1 lands in the first row and first column, and the value 6 lands in the second row and third column.
[[1 2 3]
[4 5 6]]np.arrayconverts the nested Python list into an ndarray object- Each inner list [1, 2, 3] and [4, 5, 6] becomes one row of the array
- print shows the grid with rows in brackets and values separated by spaces
Read the diagram as a map from positions to values. Each cell is labeled with a zero-based row number and a zero-based column number, because Python indexing starts at zero. The first list supplies the entire top row, and the second list supplies the entire bottom row. The printed grid mirrors that structure: the first bracketed line holds the first inner list, and the second bracketed line holds the second.
What the ndarray Class Is
NumPy is a fundamental package for scientific computing in Python. Its core functionality is the ndarray class. The name is shorthand for an n-dimensional array, which is a multidimensional container of values. When np.array runs, it builds an object of this class.
NumPy provides more than arrays. The source describes three broad areas: functionality for multidimensional arrays, high-level mathematical functions, and pseudorandom number generators. This article focuses only on creating arrays, identifying what kind of object you have, and reasoning about it when something goes wrong. The other areas build on the same array object.
Objects of the ndarray class are called NumPy arrays, or simply arrays. You will see this convention throughout scikit-learn material, so the two names mean the same thing in this course.
Why Every Element Shares One Type
A Python list is happy to hold mixed types in a single container: an integer, a string, and another integer can sit side by side. A NumPy array does not work that way. All elements of a NumPy array must be of the same type. This rule is the most common source of confusing results, because it changes what the array holds when you pass in mixed input.
[1, 'two', 3]
['1' 'two' '3']- The original list keeps each item with its own type, so the integers print without quotes
np.arraymust choose one type for every element- The integers 1 and 3 come out of the array as text, so they print in quotes
Tracing the Path into scikit-learn
scikit-learn takes in data in the form of NumPy arrays. This is a requirement rather than a stylistic preference. Any data you want to use with scikit-learn has to be converted to a NumPy array first, which makes the array the fundamental data structure of the library. The conversion step belongs in your workflow before you call any scikit-learn function.
In this path, the data stops being a list at the np.array call. Everything after that point is an ndarray. A mismatch can first appear at that conversion, because the list you handed in may contain mixed types or an uneven shape. It can also appear later, if the data was never converted and the estimator receives something other than an array.
Diagnosing Where a Mismatch Begins
When your predicted output and the real output diverge, the location of the divergence usually points to one of three places: the input list you handed to np.array, the call that created the array, or the step that printed or used it. Trace the program in that order, and compare what you expected at each line with what the code actually produces.
Locating a divergence in a short program
You predicted that the program below prints the numbers 1 2 3 4 5 6 as a grid of two rows, but you suspect the output is different. Where does the divergence begin?
- 1Check the input list
The list is [[1, 2, 3], [4, 'five', 6]]. It mixes integers with a string. This is the first possible source of a mismatch.
- 2Check the call that created the array
np.arraymust convert every element to one common type. The string forces the integers in the array to become text, so the array no longer holds numbers you can compute with. - 3Check the printed output
The printed grid shows the integers in quotes, which confirms that the conversion happened at the array-creation step and not during printing.
The divergence begins at the input list, because it mixes types. The np.array call then applies the one-type rule and converts the integers. Printing only reveals the result of that conversion.
Mistakes Beginners Make with np.array
Common mistake Assuming a mixed-type list stays mixed inside the array
What it looks likenp.array([1, "two", 3]) prints ['1' 'two' '3'] instead of the original values
Why it failsAn ndarray requires all of its elements to share one type, so NumPy converts the elements to a common type.
Do this insteadMake the input uniform before creating the array, and inspect the printed output to confirm the types you expect.
Common mistake Reading the printed grid as columns instead of rows
What it looks likeExpecting the value 4 to sit in the first column of np.array([[1, 2, 3], [4, 5, 6]])
Why it failsEach inner list becomes one row, so 4 sits in the second row and first column.
Do this insteadMap each inner list to one printed line, then count positions from zero.
Common mistake Passing raw lists to scikit-learn without converting them
Why it failsscikit-learn takes data in the form of NumPy arrays, so the conversion is a requirement.
Do this insteadConvert the data to a NumPy array with np.array before calling any scikit-learn function.
Practice: Predict Then Check
Without running the code, predict the printed output of np.array([[1, 2], [3, 4], [5, 6]]). Then state which element sits in row 2 and column 1 using zero-based indexing, and explain which of the three diagnosis steps you would check first if the printed grid did not match your prediction.
Hints
- Each inner list becomes one row, so this input has three rows of two elements each.
- Zero-based indexing means the first row is row 0, so row 2 is the third row.
- Start with the input list, then the call that created the array, then the printed output.
Key Takeaways
np.arrayturns nested Python lists into a NumPy array, where each inner list forms one row of a two-dimensional grid.- Objects of the ndarray class are called NumPy arrays, and all of their elements must share one type, so mixed-type input can be silently converted.
- scikit-learn takes in data as NumPy arrays, so any data must be converted with
np.arraybefore it is passed to a scikit-learn function. - When predicted and real output diverge, trace the input list, the array-creation call, and the printing step in that order to find where the mismatch begins.