Use attribute and target matrices for TensorFlow Linear Regression Python
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Linear regression in TensorFlow involves two core data structures: the attribute matrix (X) containing input features and the target matrix (Y) containing the values to predict. Properly shaping and preparing these matrices is essential — TensorFlow operations expect specific dimensions, and mismatched shapes are the most common source of errors in regression models.
Understanding the Matrices
- Attribute Matrix (X): Shape
(n_samples, n_features). Each row is one observation, each column is a feature. - Target Matrix (Y): Shape
(n_samples, 1)or(n_samples,). The output variable to predict.
Method 1: TensorFlow Keras (Recommended)
The simplest approach using tf.keras:
Method 2: Low-Level TensorFlow
For understanding the math behind linear regression:
Method 3: Normal Equation (Closed-Form)
For small datasets, solve analytically:
Loading Data from CSV
Feature Normalization
Always normalize features before training — linear regression converges much faster with normalized inputs:
Common Pitfalls
- Shape mismatch: X must be
(n_samples, n_features)and Y must be(n_samples, 1). A common error is Y being(n_samples,)— useY.reshape(-1, 1). - Feature scaling: Without normalization, features with large ranges dominate the loss. Always standardize (zero mean, unit variance) or normalize (0-1 range) before training.
- dtype mismatch: TensorFlow defaults to
float32. Ensure X and Y are bothfloat32, notfloat64(NumPy's default). - Learning rate too high: For gradient descent, a high learning rate causes divergence (loss goes to infinity). Start with
0.001and adjust. - Overfitting with many features: Linear regression with many features relative to samples can overfit. Use L1/L2 regularization:
tf.keras.layers.Dense(1, kernel_regularizer=tf.keras.regularizers.l2(0.01)).
Summary
- The attribute matrix X has shape
(n_samples, n_features), target Y has shape(n_samples, 1) - Use
tf.keras.layers.Dense(1)for the simplest Keras linear regression - Use
tf.GradientTapefor manual gradient descent (educational purposes) - Use the normal equation
W = (X^T X)^(-1) X^T Yfor small datasets - Always normalize features before training for faster convergence
- Ensure both X and Y are
float32and properly shaped
Related reading
- Use attribute and target matrices for TensorFlow Linear Regression Python
- Use keras layer in tensorflow code
- Use of grads_ys parameter in tf.gradients - TensorFlow
- use pre-compiled tensorflow with cmake
- Use Azure Machine learning to detect symbol within an image
- Use Bagging Classifier with a support vector machine model
- Use cases for the 'setdefault' dict method
- Use celery priority queue with broadcast tasks
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.