NumPy helps organize and process numbers quickly. Machine learning libraries use NumPy arrays to learn from data and make predictions.
0
0
NumPy with machine learning libraries
Introduction
When you want to prepare data for a machine learning model.
When you need to convert data into arrays that machine learning tools understand.
When you want to do math operations on data before training a model.
When you want to check or change data shapes to fit machine learning requirements.
When you want to use popular machine learning libraries like scikit-learn or TensorFlow.
Syntax
NumPy
import numpy as np from sklearn.linear_model import LinearRegression # Create NumPy arrays for features and target X = np.array([[1, 2], [3, 4], [5, 6]]) y = np.array([1, 2, 3]) # Create and train model model = LinearRegression() model.fit(X, y) # Predict new data new_data = np.array([[7, 8]]) prediction = model.predict(new_data)
Machine learning libraries often expect data as NumPy arrays.
NumPy arrays are fast and easy to use for math operations needed in machine learning.
Examples
Using NumPy arrays with KMeans clustering from scikit-learn.
NumPy
import numpy as np from sklearn.cluster import KMeans # Data as NumPy array data = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]) # Create and fit KMeans model kmeans = KMeans(n_clusters=2) kmeans.fit(data) # Get cluster centers centers = kmeans.cluster_centers_
Convert NumPy arrays to TensorFlow tensors for deep learning.
NumPy
import numpy as np import tensorflow as tf # Create NumPy array features = np.array([[1, 2], [3, 4], [5, 6]], dtype=np.float32) # Convert to TensorFlow tensor tensor = tf.convert_to_tensor(features) # Use tensor in a simple TensorFlow operation result = tf.reduce_sum(tensor, axis=1)
Sample Program
This program uses NumPy arrays to train a simple linear regression model and predict a value.
NumPy
import numpy as np from sklearn.linear_model import LinearRegression # Prepare data X = np.array([[1, 1], [2, 2], [3, 3], [4, 4]]) y = np.array([2, 4, 6, 8]) # Create and train model model = LinearRegression() model.fit(X, y) # Predict for new input new_X = np.array([[5, 5]]) predicted = model.predict(new_X) print(f"Prediction for input {new_X.tolist()}: {predicted[0]:.2f}")
OutputSuccess
Important Notes
Always check that your data is in the right shape before using it with machine learning libraries.
NumPy arrays are the common language between many machine learning tools.
Converting between NumPy arrays and other formats (like pandas DataFrames) is easy and often needed.
Summary
NumPy arrays are the main data format for machine learning libraries.
They help prepare, process, and feed data into models.
Using NumPy with machine learning libraries makes your code faster and simpler.