
import pandas as pd
import numpy as np

class FeatureAnalyzer:
    def __init__(self):
        self.summary = None
        self.drop_cols = []
        
    def analyze(self, df, target=None):
        """
        Analyze dataset and return summary statistics.
        """
        stats = []
        for col in df.columns:
            series = df[col]
            dtype = series.dtype
            
            # Count unique values
            nunique = series.nunique()
            
            # Missing rate
            missing = series.isnull().sum()
            missing_rate = missing / len(df)
            
            # Basic stats
            if np.issubdtype(dtype, np.number):
                min_val = series.min()
                max_val = series.max()
                mean_val = series.mean()
                std_val = series.std()
                zeros = (series == 0).sum()
                zeros_rate = zeros / len(df)
            else:
                min_val = np.nan
                max_val = np.nan
                mean_val = np.nan
                std_val = np.nan
                zeros = 0
                zeros_rate = 0
                
            stats.append({
                'variable': col,
                'dtype': str(dtype),
                'nunique': nunique,
                'missing_rate': missing_rate,
                'min': min_val,
                'max': max_val,
                'mean': mean_val,
                'std': std_val,
                'zeros_rate': zeros_rate
            })
            
        self.summary = pd.DataFrame(stats)
        return self.summary
        
    def get_drop_columns(self, threshold_missing=0.95, threshold_unique=1):
        """
        Identify columns to drop based on missing rate or single value.
        """
        if self.summary is None:
            raise ValueError("Run analyze() first.")
            
        drop_missing = self.summary[self.summary['missing_rate'] > threshold_missing]['variable'].tolist()
        drop_unique = self.summary[self.summary['nunique'] <= threshold_unique]['variable'].tolist()
        
        self.drop_cols = list(set(drop_missing + drop_unique))
        return self.drop_cols
