计划执行与检查节点
executing-plans
按既定实施计划完成工作,保持过程检查和反馈。
ds-linear-regression【效率与协作】"Implements linear regression including OLS, ridge regression, lasso elastic net, and other regularized linear models for prediction"
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: ds-linear-regression compatibility: opencode completeness: 95 content-types:
Comprehensive guide to linear regression in machine learning and data science workflows.
Linear Regression is a critical component of the machine learning workflow. This skill covers:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# Generate synthetic regression data for demonstration
X, y = np.random.randn(500, 5), np.random.randn(500)
df = pd.DataFrame(X, columns=[f'feature_{i}' for i in range(5)])
df['target'] = y
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
df.drop('target', axis=1), df['target'], test_size=0.2, random_state=42
)
# Initialize and train the OLS model
model = LinearRegression()
model.fit(X_train, y_train)
# Generate predictions and compute evaluation metrics
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Mean Squared Error: {mse:.4f}")
print(f"R² Score: {r2:.4f}")
print(f"Coefficients: {model.coef_}")
print(f"Intercept: {model.intercept_:.4f}")
import logging
import pandas as pd
import numpy as np
from typing import Any, Dict
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_score
from sklearn.preprocessing import StandardScaler
logger = logging.getLogger(__name__)
class LinearRegressionPipeline:
"""Production-ready linear regression with validation and metrics."""
def __init__(self, random_state: int = 42) -> None:
self.random_state = random_state
self.model = LinearRegression()
self.scaler = StandardScaler()
self.is_fitted: bool = False
def _validate_input(self, data: pd.DataFrame) -> pd.DataFrame:
"""Validate input DataFrame for numeric compatibility."""
if data.empty:
raise ValueError("Input DataFrame cannot be empty.")
numeric_cols = data.select_dtypes(include='number')
if numeric_cols.shape[1] == 0:
raise ValueError("All columns must be numeric for linear regression.")
return data
def execute(self, data: pd.DataFrame, target_col: str = 'target') -> Dict[str, Any]:
"""Execute linear regression pipeline on provided data."""
try:
df = self._validate_input(data)
X = df.drop(columns=[target_col])
y = df[target_col]
# Scale features for numerical stability and convergence
X_scaled = self.scaler.fit_transform(X)
# Train model on scaled data
self.model.fit(X_scaled, y)
self.is_fitted = True
# Predict and evaluate performance
y_pred = self.model.predict(X_scaled)
mae = mean_absolute_error(y, y_pred)
r2 = r2_score(y, y_pred)
return {
'status': 'success'
'mae': float(mae)
'r2_score': float(r2)
'coefficients': self.model.coef_.tolist()
'intercept': float(self.model.intercept_)
'feature_names': X.columns.tolist()
}
except Exception as e:
logger.error(f"Regression pipeline failed: {e}")
return {'status': 'error', 'message': str(e)}
# BAD: Hardcoded values, no validation, bypasses error handling, violates DRY
def bad_regression(df):
X = df.iloc[:, :5]
y = df.iloc[:, 5]
model = LinearRegression()
model.fit(X, y)
return model.predict(X)
# GOOD: Type hints, validation, modular design, follows SOLID/DRY principles
def good_regression(df: pd.DataFrame, target: str = 'target') -> Dict[str, Any]:
if df.empty:
raise ValueError("DataFrame cannot be empty")
X = df.drop(columns=[target])
y = df[target]
model = LinearRegression()
model.fit(X, y)
return {'predictions': model.predict(X), 'coefficients': model.coef_}
| Pitfall | Problem | Solution | |
Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content.