Spaces:
Running
Running
File size: 25,617 Bytes
226ac39 227cb22 226ac39 227cb22 226ac39 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
"""
Advanced Feature Engineering Tools
Tools for creating interaction features, aggregations, text features, and auto feature engineering.
"""
import polars as pl
import numpy as np
from typing import Dict, Any, List, Optional, Tuple
from pathlib import Path
import sys
import os
import json
import warnings
from itertools import combinations
warnings.filterwarnings('ignore')
# Add parent directory to path for imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from sklearn.preprocessing import PolynomialFeatures
from sklearn.decomposition import PCA, TruncatedSVD
from sklearn.feature_selection import mutual_info_classif, mutual_info_regression, SelectKBest, f_classif, f_regression
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from textblob import TextBlob
import re
from ..utils.polars_helpers import (
load_dataframe, save_dataframe, get_numeric_columns,
get_categorical_columns, get_datetime_columns
)
from ..utils.validation import (
validate_file_exists, validate_file_format, validate_dataframe,
validate_column_exists
)
def create_interaction_features(
file_path: str,
method: str = "polynomial",
degree: int = 2,
n_components: Optional[int] = None,
columns: Optional[List[str]] = None,
max_features: int = 50,
output_path: Optional[str] = None
) -> Dict[str, Any]:
"""
Create interaction features using polynomial features, PCA, or feature crossing.
Args:
file_path: Path to dataset
method: Feature interaction method:
- 'polynomial': Polynomial features (degree 2 or 3)
- 'pca': Principal Component Analysis
- 'cross': Manual feature crossing (multiply pairs)
- 'mutual_info': Select best features by mutual information
degree: Polynomial degree (for polynomial method)
n_components: Number of components (for PCA, None = auto)
columns: Columns to use (None = all numeric)
max_features: Maximum number of new features to create
output_path: Path to save dataset with new features
Returns:
Dictionary with feature engineering results
"""
# Validation
validate_file_exists(file_path)
validate_file_format(file_path)
# Load data
df = load_dataframe(file_path)
validate_dataframe(df)
# Get numeric columns if not specified
if columns is None:
columns = get_numeric_columns(df)
print(f"π’ Auto-detected {len(columns)} numeric columns")
else:
for col in columns:
validate_column_exists(df, col)
if not columns:
return {
'status': 'skipped',
'message': 'No numeric columns found for interaction features'
}
# Limit columns if too many
if len(columns) > 20:
print(f"β οΈ Too many columns ({len(columns)}). Using top 20 by variance.")
variances = df[columns].select([
(pl.col(col).var().alias(col)) for col in columns
]).to_dicts()[0]
columns = sorted(variances.keys(), key=lambda x: variances[x], reverse=True)[:20]
X = df[columns].to_numpy()
original_features = len(columns)
# Create interaction features based on method
if method == "polynomial":
print(f"π Creating polynomial features (degree={degree})...")
poly = PolynomialFeatures(degree=degree, include_bias=False, interaction_only=False)
X_poly = poly.fit_transform(X)
# Get feature names
feature_names = poly.get_feature_names_out(columns)
# Limit features
if X_poly.shape[1] > max_features + original_features:
# Keep original + top max_features new ones by variance
variances = np.var(X_poly[:, original_features:], axis=0)
top_indices = np.argsort(variances)[::-1][:max_features]
X_new = np.hstack([X, X_poly[:, original_features:][:, top_indices]])
new_feature_names = [feature_names[i + original_features] for i in top_indices]
else:
X_new = X_poly
new_feature_names = feature_names[original_features:].tolist()
# Create new dataframe
df_new = df.clone()
for i, name in enumerate(new_feature_names):
clean_name = name.replace(' ', '_').replace('^', '_pow_')
df_new = df_new.with_columns(
pl.Series(f"poly_{clean_name}", X_new[:, original_features + i])
)
created_features = new_feature_names
elif method == "pca":
print(f"π Creating PCA features...")
if n_components is None:
n_components = min(len(columns), max_features)
pca = PCA(n_components=n_components)
X_pca = pca.fit_transform(X)
# Create new dataframe
df_new = df.clone()
for i in range(n_components):
df_new = df_new.with_columns(
pl.Series(f"pca_{i+1}", X_pca[:, i])
)
created_features = [f"pca_{i+1}" for i in range(n_components)]
explained_variance = pca.explained_variance_ratio_
cumulative_variance = np.cumsum(explained_variance)
elif method == "cross":
print(f"π Creating feature crosses...")
# Create pairwise interactions
pairs = list(combinations(columns, 2))
# Limit number of pairs
if len(pairs) > max_features:
pairs = pairs[:max_features]
df_new = df.clone()
created_features = []
for col1, col2 in pairs:
new_name = f"{col1}_x_{col2}"
df_new = df_new.with_columns(
(pl.col(col1) * pl.col(col2)).alias(new_name)
)
created_features.append(new_name)
elif method == "mutual_info":
print(f"π Selecting features by mutual information...")
# This requires a target column - for now, create interaction features
# and let the user select based on their target
return {
'status': 'error',
'message': 'mutual_info method requires a target column. Use polynomial or cross instead, then use feature selection.'
}
else:
raise ValueError(f"Unsupported method: {method}")
# Save if output path provided
if output_path:
save_dataframe(df_new, output_path)
print(f"πΎ Dataset with interaction features saved to: {output_path}")
result = {
'status': 'success',
'method': method,
'original_features': original_features,
'new_features_created': len(created_features),
'total_features': len(df_new.columns),
'feature_names': created_features[:20], # Show first 20
'output_path': output_path
}
if method == "pca":
result['explained_variance_ratio'] = explained_variance.tolist()
result['cumulative_variance'] = cumulative_variance.tolist()
result['variance_explained_by_top_5'] = float(cumulative_variance[min(4, len(cumulative_variance)-1)])
return result
def create_aggregation_features(
file_path: str,
group_col: str,
agg_columns: Optional[List[str]] = None,
agg_functions: Optional[List[str]] = None,
rolling_window: Optional[int] = None,
time_col: Optional[str] = None,
lag_periods: Optional[List[int]] = None,
output_path: Optional[str] = None
) -> Dict[str, Any]:
"""
Create aggregation features including group-by aggregations, rolling windows, and lags.
Args:
file_path: Path to dataset
group_col: Column to group by (e.g., 'customer_id', 'category')
agg_columns: Columns to aggregate (None = all numeric)
agg_functions: Aggregation functions ('mean', 'sum', 'std', 'min', 'max', 'count')
rolling_window: Window size for rolling aggregations (requires sorted data)
time_col: Time column for sorting (required for rolling/lag features)
lag_periods: Lag periods to create (e.g., [1, 7, 30] for 1-day, 7-day, 30-day lags)
output_path: Path to save dataset with new features
Returns:
Dictionary with aggregation results
"""
# Validation
validate_file_exists(file_path)
validate_file_format(file_path)
# Load data
df = load_dataframe(file_path)
validate_dataframe(df)
validate_column_exists(df, group_col)
if time_col:
validate_column_exists(df, time_col)
df = df.sort(time_col)
# Get numeric columns if not specified
if agg_columns is None:
agg_columns = [col for col in get_numeric_columns(df) if col != group_col]
print(f"π’ Auto-detected {len(agg_columns)} numeric columns for aggregation")
else:
for col in agg_columns:
validate_column_exists(df, col)
if not agg_columns:
return {
'status': 'skipped',
'message': 'No numeric columns found for aggregation'
}
# Default aggregation functions
if agg_functions is None:
agg_functions = ['mean', 'sum', 'std', 'min', 'max', 'count']
df_new = df.clone()
created_features = []
# Group-by aggregations
print(f"π Creating group-by aggregations for {group_col}...")
for agg_col in agg_columns:
for agg_func in agg_functions:
try:
if agg_func == 'mean':
agg_df = df.group_by(group_col).agg(
pl.col(agg_col).mean().alias(f"{agg_col}_{group_col}_mean")
)
elif agg_func == 'sum':
agg_df = df.group_by(group_col).agg(
pl.col(agg_col).sum().alias(f"{agg_col}_{group_col}_sum")
)
elif agg_func == 'std':
agg_df = df.group_by(group_col).agg(
pl.col(agg_col).std().alias(f"{agg_col}_{group_col}_std")
)
elif agg_func == 'min':
agg_df = df.group_by(group_col).agg(
pl.col(agg_col).min().alias(f"{agg_col}_{group_col}_min")
)
elif agg_func == 'max':
agg_df = df.group_by(group_col).agg(
pl.col(agg_col).max().alias(f"{agg_col}_{group_col}_max")
)
elif agg_func == 'count':
agg_df = df.group_by(group_col).agg(
pl.col(agg_col).count().alias(f"{agg_col}_{group_col}_count")
)
else:
continue
# Join back to original dataframe
df_new = df_new.join(agg_df, on=group_col, how='left')
created_features.append(f"{agg_col}_{group_col}_{agg_func}")
except Exception as e:
print(f"β οΈ Skipping {agg_col}_{agg_func}: {str(e)}")
# Rolling window features
if rolling_window and time_col:
print(f"π Creating rolling window features (window={rolling_window})...")
for agg_col in agg_columns[:5]: # Limit to first 5 columns to avoid explosion
try:
# Rolling mean
df_new = df_new.with_columns(
pl.col(agg_col).rolling_mean(window_size=rolling_window)
.over(group_col)
.alias(f"{agg_col}_rolling_{rolling_window}_mean")
)
created_features.append(f"{agg_col}_rolling_{rolling_window}_mean")
# Rolling std
df_new = df_new.with_columns(
pl.col(agg_col).rolling_std(window_size=rolling_window)
.over(group_col)
.alias(f"{agg_col}_rolling_{rolling_window}_std")
)
created_features.append(f"{agg_col}_rolling_{rolling_window}_std")
except Exception as e:
print(f"β οΈ Skipping rolling for {agg_col}: {str(e)}")
# Lag features
if lag_periods and time_col:
print(f"β° Creating lag features (periods={lag_periods})...")
for agg_col in agg_columns[:5]: # Limit to avoid explosion
for lag in lag_periods:
try:
df_new = df_new.with_columns(
pl.col(agg_col).shift(lag)
.over(group_col)
.alias(f"{agg_col}_lag_{lag}")
)
created_features.append(f"{agg_col}_lag_{lag}")
except Exception as e:
print(f"β οΈ Skipping lag {lag} for {agg_col}: {str(e)}")
# Save if output path provided
if output_path:
save_dataframe(df_new, output_path)
print(f"πΎ Dataset with aggregation features saved to: {output_path}")
return {
'status': 'success',
'group_column': group_col,
'aggregated_columns': agg_columns,
'aggregation_functions': agg_functions,
'new_features_created': len(created_features),
'total_features': len(df_new.columns),
'feature_names': created_features[:30], # Show first 30
'rolling_window': rolling_window,
'lag_periods': lag_periods,
'output_path': output_path
}
def engineer_text_features(
file_path: str,
text_column: str,
methods: Optional[List[str]] = None,
max_features: int = 100,
ngram_range: Tuple[int, int] = (1, 2),
output_path: Optional[str] = None
) -> Dict[str, Any]:
"""
Extract features from text columns using TF-IDF, n-grams, and text statistics.
Args:
file_path: Path to dataset
text_column: Name of text column
methods: List of methods to apply:
- 'tfidf': TF-IDF vectorization
- 'count': Count vectorization (bag of words)
- 'sentiment': Sentiment analysis
- 'stats': Text statistics (length, word count, etc.)
- 'ngrams': N-gram features
max_features: Maximum number of TF-IDF/count features
ngram_range: N-gram range (e.g., (1, 2) for unigrams and bigrams)
output_path: Path to save dataset with new features
Returns:
Dictionary with text feature engineering results
"""
# Validation
validate_file_exists(file_path)
validate_file_format(file_path)
# Load data
df = load_dataframe(file_path)
validate_dataframe(df)
validate_column_exists(df, text_column)
# Default methods
if methods is None:
methods = ['stats', 'sentiment', 'tfidf']
df_new = df.clone()
created_features = []
# Get text data
texts = df[text_column].fill_null("").to_list()
# Text statistics
if 'stats' in methods:
print("π Extracting text statistics...")
char_counts = [len(str(text)) for text in texts]
word_counts = [len(str(text).split()) for text in texts]
avg_word_lengths = [np.mean([len(word) for word in str(text).split()]) if text else 0 for text in texts]
special_char_counts = [len(re.findall(r'[^a-zA-Z0-9\s]', str(text))) for text in texts]
digit_counts = [len(re.findall(r'\d', str(text))) for text in texts]
uppercase_counts = [len(re.findall(r'[A-Z]', str(text))) for text in texts]
df_new = df_new.with_columns([
pl.Series(f"{text_column}_char_count", char_counts),
pl.Series(f"{text_column}_word_count", word_counts),
pl.Series(f"{text_column}_avg_word_length", avg_word_lengths),
pl.Series(f"{text_column}_special_char_count", special_char_counts),
pl.Series(f"{text_column}_digit_count", digit_counts),
pl.Series(f"{text_column}_uppercase_count", uppercase_counts)
])
created_features.extend([
f"{text_column}_char_count",
f"{text_column}_word_count",
f"{text_column}_avg_word_length",
f"{text_column}_special_char_count",
f"{text_column}_digit_count",
f"{text_column}_uppercase_count"
])
# Sentiment analysis
if 'sentiment' in methods:
print("π Performing sentiment analysis...")
sentiments = []
subjectivities = []
for text in texts:
try:
blob = TextBlob(str(text))
sentiments.append(blob.sentiment.polarity)
subjectivities.append(blob.sentiment.subjectivity)
except:
sentiments.append(0.0)
subjectivities.append(0.0)
df_new = df_new.with_columns([
pl.Series(f"{text_column}_sentiment", sentiments),
pl.Series(f"{text_column}_subjectivity", subjectivities)
])
created_features.extend([
f"{text_column}_sentiment",
f"{text_column}_subjectivity"
])
# TF-IDF features
if 'tfidf' in methods:
print(f"π€ Creating TF-IDF features (max_features={max_features})...")
tfidf = TfidfVectorizer(
max_features=max_features,
ngram_range=ngram_range,
stop_words='english',
min_df=2
)
try:
tfidf_matrix = tfidf.fit_transform([str(text) for text in texts])
feature_names = tfidf.get_feature_names_out()
# Add TF-IDF features to dataframe
for i, feature_name in enumerate(feature_names):
clean_name = re.sub(r'[^a-zA-Z0-9_]', '_', feature_name)[:30]
df_new = df_new.with_columns(
pl.Series(f"tfidf_{clean_name}", tfidf_matrix[:, i].toarray().flatten())
)
created_features.append(f"tfidf_{clean_name}")
except Exception as e:
print(f"β οΈ TF-IDF failed: {str(e)}")
# Count vectorization
if 'count' in methods:
print(f"π’ Creating count features (max_features={max_features})...")
count_vec = CountVectorizer(
max_features=max_features,
ngram_range=ngram_range,
stop_words='english',
min_df=2
)
try:
count_matrix = count_vec.fit_transform([str(text) for text in texts])
feature_names = count_vec.get_feature_names_out()
# Add count features to dataframe
for i, feature_name in enumerate(feature_names[:50]): # Limit to 50
clean_name = re.sub(r'[^a-zA-Z0-9_]', '_', feature_name)[:30]
df_new = df_new.with_columns(
pl.Series(f"count_{clean_name}", count_matrix[:, i].toarray().flatten())
)
created_features.append(f"count_{clean_name}")
except Exception as e:
print(f"β οΈ Count vectorization failed: {str(e)}")
# Save if output path provided
if output_path:
save_dataframe(df_new, output_path)
print(f"πΎ Dataset with text features saved to: {output_path}")
return {
'status': 'success',
'text_column': text_column,
'methods_applied': methods,
'new_features_created': len(created_features),
'total_features': len(df_new.columns),
'feature_names': created_features[:30], # Show first 30
'output_path': output_path
}
def auto_feature_engineering(
file_path: str,
target_col: str,
groq_api_key: Optional[str] = None,
max_suggestions: int = 10,
implement_top_k: int = 5,
output_path: Optional[str] = None
) -> Dict[str, Any]:
"""
Use LLM (Groq or Gemini) to automatically generate and implement feature engineering ideas.
Args:
file_path: Path to dataset
target_col: Target column name
groq_api_key: Groq API key (optional - will try to use environment variable or Gemini)
max_suggestions: Maximum number of feature suggestions to generate
implement_top_k: Number of top suggestions to implement
output_path: Path to save dataset with new features
Returns:
Dictionary with feature suggestions and implementation results
"""
import os
# Validation
validate_file_exists(file_path)
validate_file_format(file_path)
# Load data
df = load_dataframe(file_path)
validate_dataframe(df)
validate_column_exists(df, target_col)
# Get dataset summary
numeric_cols = get_numeric_columns(df)
categorical_cols = get_categorical_columns(df)
# Sample data for analysis
sample_df = df.head(5)
# Create prompt for LLM
prompt = f"""You are a data science expert. Analyze this dataset and suggest {max_suggestions} creative feature engineering ideas.
Dataset Overview:
- Target column: {target_col}
- Numeric columns ({len(numeric_cols)}): {', '.join(numeric_cols[:10])}
- Categorical columns ({len(categorical_cols)}): {', '.join(categorical_cols[:5])}
- Rows: {len(df)}
Sample data (first 5 rows):
{sample_df.head(5)}
Suggest {max_suggestions} feature engineering ideas that could improve model performance. For each idea:
1. Describe the feature clearly
2. Provide Python code using Polars to create it
3. Explain why it might be valuable
Format your response as JSON:
{{
"suggestions": [
{{
"name": "feature_name",
"description": "what it does",
"code": "pl.col('a') * pl.col('b')",
"reasoning": "why it helps"
}}
]
}}
"""
print("π€ Asking LLM for feature engineering suggestions...")
# Try multiple LLM providers in order of preference
llm_response = None
# Try Groq first if API key provided
if groq_api_key or os.getenv("GROQ_API_KEY"):
try:
from groq import Groq
api_key = groq_api_key or os.getenv("GROQ_API_KEY")
client = Groq(api_key=api_key)
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": prompt}],
temperature=0.7,
max_tokens=2000
)
llm_response = response.choices[0].message.content
print(" β Using Groq LLM")
except Exception as e:
print(f" β οΈ Groq failed: {str(e)}, trying Gemini...")
# Try Gemini if Groq failed or not available
if not llm_response and os.getenv("GEMINI_API_KEY"):
try:
import google.generativeai as genai
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
model = genai.GenerativeModel('gemini-2.0-flash-exp')
response = model.generate_content(prompt)
llm_response = response.text
print(" β Using Gemini LLM")
except Exception as e:
print(f" β οΈ Gemini failed: {str(e)}")
if not llm_response:
return {
"status": "error",
"message": "No LLM API key available. Set GROQ_API_KEY or GEMINI_API_KEY environment variable."
}
try:
# Parse JSON response
import json
# Extract JSON from response (might be wrapped in markdown code blocks)
if "```json" in llm_response:
llm_response = llm_response.split("```json")[1].split("```")[0].strip()
elif "```" in llm_response:
llm_response = llm_response.split("```")[1].split("```")[0].strip()
suggestions = json.loads(llm_response)
# Implement top K suggestions
df_new = df.clone()
implemented = []
for i, suggestion in enumerate(suggestions['suggestions'][:implement_top_k]):
try:
# Execute feature creation code
feature_name = suggestion['name']
code = suggestion['code']
# Create new column using eval (be careful in production!)
df_new = df_new.with_columns(
eval(code).alias(feature_name)
)
implemented.append({
'name': feature_name,
'description': suggestion['description'],
'reasoning': suggestion['reasoning']
})
print(f"β
Implemented: {feature_name}")
except Exception as e:
print(f"β οΈ Failed to implement {suggestion.get('name', 'unknown')}: {str(e)}")
# Save if output path provided
if output_path:
save_dataframe(df_new, output_path)
print(f"πΎ Dataset with auto-generated features saved to: {output_path}")
return {
'status': 'success',
'total_suggestions': len(suggestions['suggestions']),
'suggestions': suggestions['suggestions'],
'implemented': implemented,
'new_features_created': len(implemented),
'output_path': output_path
}
except Exception as e:
return {
'status': 'error',
'message': f"Auto feature engineering failed: {str(e)}"
}
|