data-scientist
You are a data scientist with expertise in statistical analysis, machine learning, data visualization, and experimental design. Use when: statistical analysi...
Documentation & Preview
Data Scientist — Code Examples
Example 1
import pandas as pd
import numpy as np
import scipy.stats as stats
from scipy.stats import ttest_ind, chi2_contingency, mannwhitneyu
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
class StatisticalAnalyzer:
def __init__(self, data):
self.data = data
self.results = {}
def descriptive_statistics(self, columns=None):
"""Generate comprehensive descriptive statistics"""
if columns is None:
columns = self.data.select_dtypes(include=[np.number]).columns
stats_summary = {}
for col in columns:
stats_summary[col] = {
'count': self.data[col].count(),
'mean': self.data[col].mean(),
'median': self.data[col].median(),
'std': self.data[col].std(),
'min': self.data[col].min(),
'max': self.data[col].max(),
'q25': self.data[col].quantile(0.25),
'q75': self.data[col].quantile(0.75),
'skewness': stats.skew(self.data[col].dropna()),
'kurtosis': stats.kurtosis(self.data[col].dropna())
}
return pd.DataFrame(stats_summary).T
def hypothesis_testing(self, group_col, target_col, test_type='auto'):
"""Perform appropriate hypothesis tests"""
groups = self.data[group_col].unique()
if len(groups) != 2:
raise ValueError("Currently supports only two-group comparisons")
group1 = self.data[self.data[group_col] == groups[0]][target_col].dropna()
group2 = self.data[self.data[group_col] == groups[1]][target_col].dropna()
# Normality tests
_, p_norm1 = stats.shapiro(group1.sample(min(5000, len(group1))))
_, p_norm2 = stats.shapiro(group2.sample(min(5000, len(group2))))
# Equal variance test
_, p_var = stats.levene(group1, group2)
results = {
'group1_size': len(group1),
'group2_size': len(group2),
'group1_mean': group1.mean(),
'group2_mean': group2.mean(),
'normality_p1': p_norm1,
'normality_p2': p_norm2,
'equal_variance_p': p_var
}
# Choose appropriate test
if test_type == 'auto':
if p_norm1 > 0.05 and p_norm2 > 0.05:
# Both normal, use t-test
if p_var > 0.05:
# Equal variances
stat, p_value = ttest_ind(group1, group2)
test_used = "Independent t-test (equal variances)"
else:
# Unequal variances
stat, p_value = ttest_ind(group1, group2, equal_var=False)
test_used = "Welch's t-test (unequal variances)"
else:
# Non-normal, use Mann-Whitney U
stat, p_value = mannwhitneyu(group1, group2, alternative='two-sided')
test_used = "Mann-Whitney U test"
results.update({
'test_used': test_used,
'test_statistic': stat,
'p_value': p_value,
'significant': p_value < 0.05,
'effect_size': self._calculate_effect_size(group1, group2)
})
return results
def _calculate_effect_size(self, group1, group2):
"""Calculate Cohen's d for effect size"""
pooled_std = np.sqrt(((len(group1) - 1) * group1.var() +
(len(group2) - 1) * group2.var()) /
(len(group1) + len(group2) - 2))
return (group1.mean() - group2.mean()) / pooled_stdExample 2
from sklearn.model_selection import cross_val_score, GridSearchCV, StratifiedKFold
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.metrics import roc_auc_score, precision_recall_curve
import xgboost as xgb
import lightgbm as lgb
class MLPipeline:
def __init__(self, random_state=42):
self.random_state = random_state
self.models = {}
self.best_model = None
self.feature_importance = None
def feature_engineering(self, X, y=None, numeric_features=None, categorical_features=None):
"""Advanced feature engineering"""
X_engineered = X.copy()
# Numeric feature engineering
if numeric_features:
for col in numeric_features:
# Log transformation for skewed features
if X[col].skew() > 1:
X_engineered[f'{col}_log'] = np.log1p(X[col])
# Polynomial features for important variables
X_engineered[f'{col}_squared'] = X[col] ** 2
X_engineered[f'{col}_sqrt'] = np.sqrt(X[col])
# Binning for non-linear relationships
X_engineered[f'{col}_binned'] = pd.cut(X[col], bins=5, labels=False)
# Categorical feature engineering
if categorical_features:
for col in categorical_features:
# Target encoding (if y is provided)
if y is not None:
target_mean = y.groupby(X[col]).mean()
X_engineered[f'{col}_target_encoded'] = X[col].map(target_mean)
# Frequency encoding
freq_map = X[col].value_counts(normalize=True)
X_engineered[f'{col}_frequency'] = X[col].map(freq_map)
# Interaction features
if len(numeric_features) >= 2:
for i, col1 in enumerate(numeric_features):
for col2 in numeric_features[i+1:]:
X_engineered[f'{col1}_{col2}_interaction'] = X[col1] * X[col2]
X_engineered[f'{col1}_{col2}_ratio'] = X[col1] / (X[col2] + 1e-8)
return X_engineered
def model_comparison(self, X_train, X_test, y_train, y_test):
"""Compare multiple ML algorithms"""
models = {
'Logistic Regression': LogisticRegression(random_state=self.random_state),
'Random Forest': RandomForestClassifier(random_state=self.random_state),
'Gradient Boosting': GradientBoostingClassifier(random_state=self.random_state),
'XGBoost': xgb.XGBClassifier(random_state=self.random_state),
'LightGBM': lgb.LGBMClassifier(random_state=self.random_state)
}
results = {}
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=self.random_state)
for name, model in models.items():
# Cross-validation
cv_scores = cross_val_score(model, X_train, y_train, cv=cv, scoring='roc_auc')
# Fit and predict
model.fit(X_train, y_train)
y_pred = model.predict_proba(X_test)[:, 1]
test_auc = roc_auc_score(y_test, y_pred)
results[name] = {
'cv_mean': cv_scores.mean(),
'cv_std': cv_scores.std(),
'test_auc': test_auc,
'model': model
}
self.models[name] = model
# Select best model
best_model_name = max(results.keys(), key=lambda x: results[x]['test_auc'])
self.best_model = self.models[best_model_name]
return results
def hyperparameter_tuning(self, X_train, y_train, model_type='xgboost'):
"""Advanced hyperparameter tuning"""
if model_type == 'xgboost':
param_grid = {
'n_estimators': [100, 200, 300],
'max_depth': [3, 4, 5, 6],
'learning_rate': [0.01, 0.1, 0.2],
'subsample': [0.8, 0.9, 1.0],
'colsample_bytree': [0.8, 0.9, 1.0]
}
model = xgb.XGBClassifier(random_state=self.random_state)
elif model_type == 'lightgbm':
param_grid = {
'n_estimators': [100, 200, 300],
'max_depth': [3, 4, 5, 6],
'learning_rate': [0.01, 0.1, 0.2],
'feature_fraction': [0.8, 0.9, 1.0],
'bagging_fraction': [0.8, 0.9, 1.0]
}
model = lgb.LGBMClassifier(random_state=self.random_state)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=self.random_state)
grid_search = GridSearchCV(
model, param_grid, cv=cv, scoring='roc_auc',
n_jobs=-1, verbose=1
)
grid_search.fit(X_train, y_train)
self.best_model = grid_search.best_estimator_
return grid_search.best_params_, grid_search.best_score_Example 3
import pandas as pd
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_absolute_error, mean_squared_error
import warnings
warnings.filterwarnings('ignore')
class TimeSeriesAnalyzer:
def __init__(self, data, date_col, value_col):
self.data = data.copy()
self.data[date_col] = pd.to_datetime(self.data[date_col])
self.data = self.data.set_index(date_col).sort_index()
self.ts = self.data[value_col]
self.forecast = None
def exploratory_analysis(self):
"""Comprehensive time series EDA"""
results = {}
# Basic statistics
results['basic_stats'] = {
'start_date': self.ts.index.min(),
'end_date': self.ts.index.max(),
'total_observations': len(self.ts),
'missing_values': self.ts.isnull().sum(),
'mean': self.ts.mean(),
'std': self.ts.std(),
'trend': 'increasing' if self.ts.iloc[-1] > self.ts.iloc[0] else 'decreasing'
}
# Stationarity test
adf_result = adfuller(self.ts.dropna())
results['stationarity'] = {
'adf_statistic': adf_result[0],
'p_value': adf_result[1],
'is_stationary': adf_result[1] < 0.05,
'critical_values': adf_result[4]
}
# Seasonal decomposition
if len(self.ts) >= 24: # Need at least 2 seasons
decomposition = seasonal_decompose(self.ts.dropna(), period=12)
results['seasonality'] = {
'seasonal_strength': np.var(decomposition.seasonal) / np.var(self.ts.dropna()),
'trend_strength': np.var(decomposition.trend.dropna()) / np.var(self.ts.dropna())
}
return results
def arima_modeling(self, max_p=5, max_d=2, max_q=5):
"""Automatic ARIMA model selection"""
best_aic = np.inf
best_params = None
best_model = None
for p in range(max_p + 1):
for d in range(max_d + 1):
for q in range(max_q + 1):
try:
model = ARIMA(self.ts.dropna(), order=(p, d, q))
fitted_model = model.fit()
if fitted_model.aic < best_aic:
best_aic = fitted_model.aic
best_params = (p, d, q)
best_model = fitted_model
except:
continue
return best_model, best_params, best_aic
def forecast_evaluation(self, model, test_size=0.2):
"""Evaluate forecasting performance"""
split_point = int(le