Python梯度下降多元回归成本趋于无穷的问题排查求助
Hey there! Let's break down why your gradient descent is spiraling to infinity—since you’ve already vetted your gradientDescent function, the CSV data parsing is almost certainly the root issue here. Let’s walk through fixing that and verifying your data setup.
1. Fix Your CSV Parsing Logic
Your CSV uses a non-standard format: samples are separated by |, and each sample’s values are space-separated. Default CSV loaders (like np.loadtxt or pd.read_csv with default args) won’t handle this correctly, leading to misaligned features/targets or corrupted data—exactly the kind of issue that makes gradient descent diverge.
Here’s a robust way to parse your data:
import numpy as np # Load and process the CSV file with open('your_dataset.csv', 'r') as file: # Read all content and strip extra whitespace/newlines raw_content = file.read().strip() # Split into individual samples using '|' as the delimiter # Filter out empty strings to avoid issues with leading/trailing '|' sample_strings = [s.strip() for s in raw_content.split('|') if s.strip()] # Convert each sample string into a numeric array dataset = np.array([list(map(float, s.split())) for s in sample_strings]) # Split into features (X) and target (y) X = dataset[:, :3] # First 3 columns = independent variables y = dataset[:, 3:4] # 4th column = dependent variable # Add a bias term (critical for most gradient descent implementations) X = np.hstack((np.ones((X.shape[0], 1)), X))
2. Verify Your Loaded Data
Before re-running gradient descent, confirm your data is correctly loaded:
- Check shapes with
print(X.shape, y.shape): You should see something like(N, 4)for X (N samples + bias term) and(N, 1)for y. - Print the first few rows with
print(X[:3], y[:3])to ensure they match your CSV example (e.g., first X row should be[1, 600, 20, 0.5]with y value0.63). - Scan for extreme outlier values—if any features/targets are way larger than others, you’ll need to normalize your data (e.g., min-max scaling or standardization) to prevent gradient explosion.
3. Quick Gradient Descent Sanity Checks (Just to Be Safe)
Even if you trust your function, these quick checks can rule out other divergence causes:
- Lower your learning rate: A learning rate that’s too large is a common divergence trigger. Try reducing it incrementally (e.g., from
0.001to0.0001or smaller) and see if cost stabilizes. - Validate your cost function: For linear regression, the cost function should be:
Double-check that matrix multiplication and summation are dimensionally consistent.def compute_cost(X, y, theta): m = len(y) predictions = X @ theta cost = (1/(2*m)) * np.sum((predictions - y)**2) return cost
内容的提问来源于stack exchange,提问作者capncook

