最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

從入門(mén)到實(shí)戰(zhàn)詳解Python數(shù)據(jù)統(tǒng)計(jì)的完全指南

 更新時(shí)間:2025年10月10日 09:50:42   作者:百錦再@新空間  
這篇文章主要為大家詳細(xì)介紹了Python中數(shù)據(jù)統(tǒng)計(jì)的相關(guān)知識(shí),包括數(shù)據(jù)統(tǒng)計(jì),數(shù)據(jù)獲取與加載,數(shù)據(jù)清洗與預(yù)處理等內(nèi)容,有需要的小伙伴可以了解下

1. 數(shù)據(jù)統(tǒng)計(jì)基礎(chǔ)與環(huán)境配置

1.1 Python數(shù)據(jù)科學(xué)生態(tài)系統(tǒng)

Python在數(shù)據(jù)統(tǒng)計(jì)領(lǐng)域的強(qiáng)大主要得益于其豐富的庫(kù)生態(tài)系統(tǒng):

# 核心數(shù)據(jù)分析庫(kù)
import pandas as pd
import numpy as np

# 數(shù)據(jù)可視化庫(kù)
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px

# 統(tǒng)計(jì)分析庫(kù)
import scipy.stats as stats
from scipy import stats
import statsmodels.api as sm
from statsmodels.formula.api import ols

# 機(jī)器學(xué)習(xí)庫(kù)
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression

# 其他實(shí)用庫(kù)
import warnings
warnings.filterwarnings('ignore')

1.2 環(huán)境配置與安裝

# 推薦使用conda或pip安裝必要包
"""
pip install pandas numpy matplotlib seaborn plotly
pip install scipy statsmodels scikit-learn
pip install jupyter notebook  # 交互式環(huán)境
"""

# 設(shè)置中文字體顯示
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用來(lái)正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus'] = False  # 用來(lái)正常顯示負(fù)號(hào)

# 設(shè)置繪圖樣式
plt.style.use('seaborn-v0_8')
sns.set_palette("husl")

2. 數(shù)據(jù)獲取與加載

2.1 從不同數(shù)據(jù)源加載數(shù)據(jù)

import pandas as pd
import numpy as np
import sqlite3
import requests
import json

class DataLoader:
    def __init__(self):
        self.data_sources = {}
    
    def load_csv(self, file_path, **kwargs):
        """加載CSV文件"""
        try:
            df = pd.read_csv(file_path, **kwargs)
            self.data_sources['csv'] = df
            print(f"成功加載CSV文件,數(shù)據(jù)形狀: {df.shape}")
            return df
        except Exception as e:
            print(f"加載CSV文件失敗: {e}")
            return None
    
    def load_excel(self, file_path, sheet_name=0):
        """加載Excel文件"""
        try:
            df = pd.read_excel(file_path, sheet_name=sheet_name)
            self.data_sources['excel'] = df
            print(f"成功加載Excel文件,數(shù)據(jù)形狀: {df.shape}")
            return df
        except Exception as e:
            print(f"加載Excel文件失敗: {e}")
            return None
    
    def load_sql(self, query, db_path):
        """從SQL數(shù)據(jù)庫(kù)加載數(shù)據(jù)"""
        try:
            conn = sqlite3.connect(db_path)
            df = pd.read_sql_query(query, conn)
            conn.close()
            self.data_sources['sql'] = df
            print(f"成功從SQL加載數(shù)據(jù),數(shù)據(jù)形狀: {df.shape}")
            return df
        except Exception as e:
            print(f"從SQL加載數(shù)據(jù)失敗: {e}")
            return None
    
    def load_api(self, url, params=None):
        """從API接口加載數(shù)據(jù)"""
        try:
            response = requests.get(url, params=params)
            if response.status_code == 200:
                data = response.json()
                df = pd.DataFrame(data)
                self.data_sources['api'] = df
                print(f"成功從API加載數(shù)據(jù),數(shù)據(jù)形狀: {df.shape}")
                return df
            else:
                print(f"API請(qǐng)求失敗,狀態(tài)碼: {response.status_code}")
                return None
        except Exception as e:
            print(f"從API加載數(shù)據(jù)失敗: {e}")
            return None

# 使用示例
loader = DataLoader()

# 加載示例數(shù)據(jù)集
from sklearn.datasets import load_iris, load_boston
iris = load_iris()
iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_df['target'] = iris.target

2.2 數(shù)據(jù)基本信息查看

def explore_data(df, sample_size=5):
    """
    全面探索數(shù)據(jù)集基本信息
    """
    print("=" * 50)
    print("數(shù)據(jù)集基本信息探索")
    print("=" * 50)
    
    # 基本形狀信息
    print(f"數(shù)據(jù)形狀: {df.shape}")
    print(f"行數(shù): {df.shape[0]}")
    print(f"列數(shù): {df.shape[1]}")
    
    # 數(shù)據(jù)類型信息
    print("\n數(shù)據(jù)類型信息:")
    print(df.dtypes)
    
    # 數(shù)據(jù)預(yù)覽
    print(f"\n前{sample_size}行數(shù)據(jù):")
    print(df.head(sample_size))
    
    print(f"\n后{sample_size}行數(shù)據(jù):")
    print(df.tail(sample_size))
    
    # 統(tǒng)計(jì)摘要
    print("\n數(shù)值列統(tǒng)計(jì)摘要:")
    print(df.describe())
    
    # 缺失值信息
    print("\n缺失值統(tǒng)計(jì):")
    missing_info = pd.DataFrame({
        '缺失數(shù)量': df.isnull().sum(),
        '缺失比例': df.isnull().sum() / len(df) * 100
    })
    print(missing_info)
    
    # 唯一值信息
    print("\n分類變量唯一值統(tǒng)計(jì):")
    categorical_cols = df.select_dtypes(include=['object']).columns
    for col in categorical_cols:
        print(f"{col}: {df[col].nunique()} 個(gè)唯一值")
    
    return {
        'shape': df.shape,
        'dtypes': df.dtypes,
        'missing_info': missing_info
    }

# 在iris數(shù)據(jù)集上應(yīng)用
info = explore_data(iris_df)

3. 數(shù)據(jù)清洗與預(yù)處理

3.1 缺失值處理

class DataCleaner:
    def __init__(self, df):
        self.df = df.copy()
        self.cleaning_log = []
    
    def detect_missing_values(self):
        """檢測(cè)缺失值"""
        missing_stats = pd.DataFrame({
            'missing_count': self.df.isnull().sum(),
            'missing_percentage': (self.df.isnull().sum() / len(self.df)) * 100,
            'data_type': self.df.dtypes
        })
        
        # 高缺失率列
        high_missing_cols = missing_stats[missing_stats['missing_percentage'] > 50].index.tolist()
        
        self.cleaning_log.append({
            'step': '缺失值檢測(cè)',
            'details': f"發(fā)現(xiàn) {len(high_missing_cols)} 個(gè)高缺失率列(>50%)"
        })
        
        return missing_stats, high_missing_cols
    
    def handle_missing_values(self, strategy='auto', custom_strategy=None):
        """處理缺失值"""
        df_clean = self.df.copy()
        missing_stats, high_missing_cols = self.detect_missing_values()
        
        # 刪除高缺失率列
        if high_missing_cols:
            df_clean = df_clean.drop(columns=high_missing_cols)
            self.cleaning_log.append({
                'step': '刪除高缺失率列',
                'details': f"刪除列: {high_missing_cols}"
            })
        
        # 處理剩余缺失值
        for col in df_clean.columns:
            if df_clean[col].isnull().sum() > 0:
                if strategy == 'auto':
                    # 自動(dòng)選擇策略
                    if df_clean[col].dtype in ['float64', 'int64']:
                        # 數(shù)值列用中位數(shù)填充
                        fill_value = df_clean[col].median()
                        df_clean[col].fillna(fill_value, inplace=True)
                        method = f"中位數(shù)填充 ({fill_value})"
                    else:
                        # 分類列用眾數(shù)填充
                        fill_value = df_clean[col].mode()[0] if not df_clean[col].mode().empty else 'Unknown'
                        df_clean[col].fillna(fill_value, inplace=True)
                        method = f"眾數(shù)填充 ({fill_value})"
                elif strategy == 'custom' and custom_strategy:
                    # 自定義策略
                    if col in custom_strategy:
                        fill_value = custom_strategy[col]
                        df_clean[col].fillna(fill_value, inplace=True)
                        method = f"自定義填充 ({fill_value})"
                
                self.cleaning_log.append({
                    'step': '缺失值填充',
                    'column': col,
                    'method': method,
                    'filled_count': self.df[col].isnull().sum()
                })
        
        self.df = df_clean
        return df_clean
    
    def remove_duplicates(self):
        """刪除重復(fù)行"""
        initial_count = len(self.df)
        self.df = self.df.drop_duplicates()
        removed_count = initial_count - len(self.df)
        
        self.cleaning_log.append({
            'step': '刪除重復(fù)行',
            'removed_count': removed_count,
            'remaining_count': len(self.df)
        })
        
        return self.df
    
    def handle_outliers(self, method='iqr', threshold=3):
        """處理異常值"""
        df_clean = self.df.copy()
        numeric_cols = df_clean.select_dtypes(include=[np.number]).columns
        
        outliers_info = {}
        
        for col in numeric_cols:
            if method == 'iqr':
                # IQR方法
                Q1 = df_clean[col].quantile(0.25)
                Q3 = df_clean[col].quantile(0.75)
                IQR = Q3 - Q1
                lower_bound = Q1 - 1.5 * IQR
                upper_bound = Q3 + 1.5 * IQR
                
                outliers = df_clean[(df_clean[col] < lower_bound) | (df_clean[col] > upper_bound)]
                outlier_count = len(outliers)
                
                # 縮尾處理
                df_clean[col] = np.where(df_clean[col] < lower_bound, lower_bound, df_clean[col])
                df_clean[col] = np.where(df_clean[col] > upper_bound, upper_bound, df_clean[col])
                
            elif method == 'zscore':
                # Z-score方法
                z_scores = np.abs(stats.zscore(df_clean[col]))
                outlier_count = len(df_clean[z_scores > threshold])
                
                # 使用中位數(shù)和標(biāo)準(zhǔn)差進(jìn)行穩(wěn)健的異常值處理
                median = df_clean[col].median()
                mad = stats.median_abs_deviation(df_clean[col])
                df_clean[col] = np.where(z_scores > threshold, median, df_clean[col])
            
            outliers_info[col] = outlier_count
        
        self.cleaning_log.append({
            'step': '異常值處理',
            'method': method,
            'outliers_info': outliers_info
        })
        
        self.df = df_clean
        return df_clean
    
    def get_cleaning_report(self):
        """生成清洗報(bào)告"""
        print("數(shù)據(jù)清洗報(bào)告")
        print("=" * 30)
        for log in self.cleaning_log:
            print(f"{log['step']}:")
            for key, value in log.items():
                if key != 'step':
                    print(f"  {key}: {value}")
            print()

# 使用示例
# 創(chuàng)建有缺失值和異常值的測(cè)試數(shù)據(jù)
np.random.seed(42)
test_data = pd.DataFrame({
    'A': np.random.normal(0, 1, 100),
    'B': np.random.normal(10, 2, 100),
    'C': np.random.choice(['X', 'Y', 'Z'], 100),
    'D': np.random.exponential(2, 100)
})

# 人為添加缺失值和異常值
test_data.loc[10:15, 'A'] = np.nan
test_data.loc[20:25, 'B'] = np.nan
test_data.loc[5, 'A'] = 100  # 異常值
test_data.loc[6, 'B'] = 100  # 異常值

cleaner = DataCleaner(test_data)
cleaned_data = cleaner.handle_missing_values()
cleaned_data = cleaner.remove_duplicates()
cleaned_data = cleaner.handle_outliers()
cleaner.get_cleaning_report()

3.2 數(shù)據(jù)轉(zhuǎn)換與編碼

class DataTransformer:
    def __init__(self, df):
        self.df = df.copy()
        self.transformation_log = []
    
    def encode_categorical(self, columns=None, method='onehot'):
        """分類變量編碼"""
        df_encoded = self.df.copy()
        
        if columns is None:
            categorical_cols = df_encoded.select_dtypes(include=['object']).columns
        else:
            categorical_cols = columns
        
        for col in categorical_cols:
            if method == 'onehot':
                # One-Hot編碼
                dummies = pd.get_dummies(df_encoded[col], prefix=col)
                df_encoded = pd.concat([df_encoded, dummies], axis=1)
                df_encoded.drop(col, axis=1, inplace=True)
                encoding_type = "One-Hot編碼"
                
            elif method == 'label':
                # 標(biāo)簽編碼
                from sklearn.preprocessing import LabelEncoder
                le = LabelEncoder()
                df_encoded[col] = le.fit_transform(df_encoded[col])
                encoding_type = "標(biāo)簽編碼"
            
            elif method == 'target':
                # 目標(biāo)編碼(需要目標(biāo)變量)
                if 'target' in df_encoded.columns:
                    target_mean = df_encoded.groupby(col)['target'].mean()
                    df_encoded[col] = df_encoded[col].map(target_mean)
                    encoding_type = "目標(biāo)編碼"
            
            self.transformation_log.append({
                'step': '分類變量編碼',
                'column': col,
                'method': encoding_type
            })
        
        self.df = df_encoded
        return df_encoded
    
    def scale_numerical(self, columns=None, method='standard'):
        """數(shù)值變量標(biāo)準(zhǔn)化"""
        from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
        
        df_scaled = self.df.copy()
        
        if columns is None:
            numerical_cols = df_scaled.select_dtypes(include=[np.number]).columns
        else:
            numerical_cols = columns
        
        scaler = None
        if method == 'standard':
            scaler = StandardScaler()
            scaling_type = "標(biāo)準(zhǔn)化(Z-score)"
        elif method == 'minmax':
            scaler = MinMaxScaler()
            scaling_type = "最小最大縮放"
        elif method == 'robust':
            scaler = RobustScaler()
            scaling_type = "穩(wěn)健縮放"
        
        if scaler:
            df_scaled[numerical_cols] = scaler.fit_transform(df_scaled[numerical_cols])
            
            self.transformation_log.append({
                'step': '數(shù)值變量縮放',
                'columns': list(numerical_cols),
                'method': scaling_type
            })
        
        self.df = df_scaled
        return df_scaled, scaler
    
    def create_features(self):
        """特征工程"""
        df_featured = self.df.copy()
        numerical_cols = df_featured.select_dtypes(include=[np.number]).columns
        
        # 創(chuàng)建多項(xiàng)式特征
        from sklearn.preprocessing import PolynomialFeatures
        
        if len(numerical_cols) >= 2:
            poly = PolynomialFeatures(degree=2, include_bias=False, interaction_only=True)
            poly_features = poly.fit_transform(df_featured[numerical_cols[:2]])  # 取前兩個(gè)數(shù)值列
            poly_feature_names = poly.get_feature_names_out(numerical_cols[:2])
            
            poly_df = pd.DataFrame(poly_features, columns=poly_feature_names)
            df_featured = pd.concat([df_featured, poly_df], axis=1)
            
            self.transformation_log.append({
                'step': '特征工程',
                'type': '多項(xiàng)式特征',
                'features_created': list(poly_feature_names)
            })
        
        # 創(chuàng)建統(tǒng)計(jì)特征
        for col in numerical_cols:
            df_featured[f'{col}_zscore'] = stats.zscore(df_featured[col])
            df_featured[f'{col}_rank'] = df_featured[col].rank()
        
        self.transformation_log.append({
            'step': '特征工程',
            'type': '統(tǒng)計(jì)特征',
            'features_created': [f'{col}_zscore' for col in numerical_cols] + 
                              [f'{col}_rank' for col in numerical_cols]
        })
        
        self.df = df_featured
        return df_featured

# 使用示例
transformer = DataTransformer(iris_df)
transformed_data, scaler = transformer.scale_numerical(method='standard')
transformer.create_features()

4. 描述性統(tǒng)計(jì)分析

4.1 基本統(tǒng)計(jì)量計(jì)算

class DescriptiveStatistics:
    def __init__(self, df):
        self.df = df
        self.numerical_cols = df.select_dtypes(include=[np.number]).columns
        self.categorical_cols = df.select_dtypes(include=['object']).columns
    
    def basic_stats(self):
        """計(jì)算基本統(tǒng)計(jì)量"""
        stats_summary = {}
        
        for col in self.numerical_cols:
            data = self.df[col].dropna()
            stats_summary[col] = {
                'count': len(data),
                'mean': np.mean(data),
                'median': np.median(data),
                'std': np.std(data),
                'variance': np.var(data),
                'min': np.min(data),
                'max': np.max(data),
                'range': np.max(data) - np.min(data),
                'q1': np.percentile(data, 25),
                'q3': np.percentile(data, 75),
                'iqr': np.percentile(data, 75) - np.percentile(data, 25),
                'skewness': stats.skew(data),
                'kurtosis': stats.kurtosis(data),
                'cv': (np.std(data) / np.mean(data)) * 100 if np.mean(data) != 0 else np.inf
            }
        
        return pd.DataFrame(stats_summary).T
    
    def categorical_stats(self):
        """分類變量統(tǒng)計(jì)"""
        cat_stats = {}
        
        for col in self.categorical_cols:
            data = self.df[col].dropna()
            value_counts = data.value_counts()
            cat_stats[col] = {
                'count': len(data),
                'unique_count': len(value_counts),
                'mode': value_counts.index[0] if len(value_counts) > 0 else None,
                'mode_frequency': value_counts.iloc[0] if len(value_counts) > 0 else 0,
                'mode_percentage': (value_counts.iloc[0] / len(data)) * 100 if len(value_counts) > 0 else 0,
                'entropy': stats.entropy(value_counts)  # 信息熵
            }
        
        return pd.DataFrame(cat_stats).T
    
    def distribution_test(self):
        """分布檢驗(yàn)"""
        distribution_results = {}
        
        for col in self.numerical_cols:
            data = self.df[col].dropna()
            
            # 正態(tài)性檢驗(yàn)
            shapiro_stat, shapiro_p = stats.shapiro(data) if len(data) < 5000 else (np.nan, np.nan)
            normaltest_stat, normaltest_p = stats.normaltest(data)
            
            distribution_results[col] = {
                'shapiro_stat': shapiro_stat,
                'shapiro_p': shapiro_p,
                'normaltest_stat': normaltest_stat,
                'normaltest_p': normaltest_p,
                'is_normal_shapiro': shapiro_p > 0.05 if not np.isnan(shapiro_p) else None,
                'is_normal_normaltest': normaltest_p > 0.05
            }
        
        return pd.DataFrame(distribution_results).T
    
    def correlation_analysis(self):
        """相關(guān)性分析"""
        corr_matrix = self.df[self.numerical_cols].corr()
        
        # 三種相關(guān)系數(shù)
        pearson_corr = self.df[self.numerical_cols].corr(method='pearson')
        spearman_corr = self.df[self.numerical_cols].corr(method='spearman')
        kendall_corr = self.df[self.numerical_cols].corr(method='kendall')
        
        return {
            'pearson': pearson_corr,
            'spearman': spearman_corr,
            'kendall': kendall_corr
        }
    
    def generate_report(self):
        """生成完整的描述性統(tǒng)計(jì)報(bào)告"""
        print("描述性統(tǒng)計(jì)分析報(bào)告")
        print("=" * 50)
        
        # 基本統(tǒng)計(jì)量
        print("\n1. 數(shù)值變量基本統(tǒng)計(jì)量:")
        basic_stats_df = self.basic_stats()
        print(basic_stats_df.round(4))
        
        # 分類變量統(tǒng)計(jì)
        if len(self.categorical_cols) > 0:
            print("\n2. 分類變量統(tǒng)計(jì):")
            cat_stats_df = self.categorical_stats()
            print(cat_stats_df.round(4))
        
        # 分布檢驗(yàn)
        print("\n3. 分布檢驗(yàn)結(jié)果:")
        dist_test_df = self.distribution_test()
        print(dist_test_df.round(4))
        
        # 相關(guān)性分析
        print("\n4. Pearson相關(guān)系數(shù)矩陣:")
        corr_results = self.correlation_analysis()
        print(corr_results['pearson'].round(4))
        
        return {
            'basic_stats': basic_stats_df,
            'categorical_stats': cat_stats_df if len(self.categorical_cols) > 0 else None,
            'distribution_test': dist_test_df,
            'correlation': corr_results
        }

# 使用示例
desc_stats = DescriptiveStatistics(iris_df)
report = desc_stats.generate_report()

4.2 高級(jí)統(tǒng)計(jì)分析

class AdvancedStatistics:
    def __init__(self, df):
        self.df = df
        self.numerical_cols = df.select_dtypes(include=[np.number]).columns
    
    def outlier_detection(self, method='multiple'):
        """異常值檢測(cè)"""
        outlier_results = {}
        
        for col in self.numerical_cols:
            data = self.df[col].dropna()
            outliers = {}
            
            # IQR方法
            Q1 = np.percentile(data, 25)
            Q3 = np.percentile(data, 75)
            IQR = Q3 - Q1
            lower_bound = Q1 - 1.5 * IQR
            upper_bound = Q3 + 1.5 * IQR
            
            iqr_outliers = data[(data < lower_bound) | (data > upper_bound)]
            outliers['iqr'] = {
                'count': len(iqr_outliers),
                'percentage': (len(iqr_outliers) / len(data)) * 100,
                'values': iqr_outliers.tolist()
            }
            
            # Z-score方法
            z_scores = np.abs(stats.zscore(data))
            zscore_outliers = data[z_scores > 3]
            outliers['zscore'] = {
                'count': len(zscore_outliers),
                'percentage': (len(zscore_outliers) / len(data)) * 100,
                'values': zscore_outliers.tolist()
            }
            
            # 修正Z-score方法(對(duì)異常值更穩(wěn)?。?
            median = np.median(data)
            mad = stats.median_abs_deviation(data)
            modified_z_scores = 0.6745 * (data - median) / mad
            mod_z_outliers = data[np.abs(modified_z_scores) > 3.5]
            outliers['modified_zscore'] = {
                'count': len(mod_z_outliers),
                'percentage': (len(mod_z_outliers) / len(data)) * 100,
                'values': mod_z_outliers.tolist()
            }
            
            outlier_results[col] = outliers
        
        return outlier_results
    
    def normality_tests(self):
        """正態(tài)性檢驗(yàn)綜合"""
        normality_results = {}
        
        for col in self.numerical_cols:
            data = self.df[col].dropna()
            tests = {}
            
            # Shapiro-Wilk檢驗(yàn)(適合小樣本)
            if len(data) < 5000:
                shapiro_stat, shapiro_p = stats.shapiro(data)
                tests['shapiro_wilk'] = {
                    'statistic': shapiro_stat,
                    'p_value': shapiro_p,
                    'is_normal': shapiro_p > 0.05
                }
            
            # D'Agostino's K^2檢驗(yàn)
            k2_stat, k2_p = stats.normaltest(data)
            tests['dagostino'] = {
                'statistic': k2_stat,
                'p_value': k2_p,
                'is_normal': k2_p > 0.05
            }
            
            # Anderson-Darling檢驗(yàn)
            anderson_result = stats.anderson(data, dist='norm')
            tests['anderson_darling'] = {
                'statistic': anderson_result.statistic,
                'critical_values': anderson_result.critical_values,
                'significance_level': anderson_result.significance_level,
                'is_normal': anderson_result.statistic < anderson_result.critical_values[2]  # 5%顯著性水平
            }
            
            # Kolmogorov-Smirnov檢驗(yàn)
            ks_stat, ks_p = stats.kstest(data, 'norm', args=(np.mean(data), np.std(data)))
            tests['kolmogorov_smirnov'] = {
                'statistic': ks_stat,
                'p_value': ks_p,
                'is_normal': ks_p > 0.05
            }
            
            normality_results[col] = tests
        
        return normality_results
    
    def confidence_intervals(self, confidence=0.95):
        """置信區(qū)間計(jì)算"""
        ci_results = {}
        
        for col in self.numerical_cols:
            data = self.df[col].dropna()
            n = len(data)
            mean = np.mean(data)
            std_err = stats.sem(data)
            
            # t分布的置信區(qū)間
            ci = stats.t.interval(confidence, n-1, loc=mean, scale=std_err)
            
            # 使用bootstrap計(jì)算置信區(qū)間
            bootstrap_ci = self._bootstrap_ci(data, confidence=confidence)
            
            ci_results[col] = {
                'sample_size': n,
                'mean': mean,
                'std_error': std_err,
                f'ci_{confidence}': ci,
                'bootstrap_ci': bootstrap_ci,
                'ci_width': ci[1] - ci[0]
            }
        
        return ci_results
    
    def _bootstrap_ci(self, data, n_bootstrap=1000, confidence=0.95):
        """Bootstrap置信區(qū)間"""
        bootstrap_means = []
        
        for _ in range(n_bootstrap):
            bootstrap_sample = np.random.choice(data, size=len(data), replace=True)
            bootstrap_means.append(np.mean(bootstrap_sample))
        
        alpha = (1 - confidence) / 2
        lower = np.percentile(bootstrap_means, alpha * 100)
        upper = np.percentile(bootstrap_means, (1 - alpha) * 100)
        
        return (lower, upper)
    
    def generate_advanced_report(self):
        """生成高級(jí)統(tǒng)計(jì)報(bào)告"""
        print("高級(jí)統(tǒng)計(jì)分析報(bào)告")
        print("=" * 50)
        
        # 異常值檢測(cè)
        print("\n1. 異常值檢測(cè)結(jié)果:")
        outlier_results = self.outlier_detection()
        for col, methods in outlier_results.items():
            print(f"\n{col}:")
            for method, result in methods.items():
                print(f"  {method}: {result['count']} 個(gè)異常值 ({result['percentage']:.2f}%)")
        
        # 正態(tài)性檢驗(yàn)
        print("\n2. 正態(tài)性檢驗(yàn)綜合結(jié)果:")
        normality_results = self.normality_tests()
        for col, tests in normality_results.items():
            print(f"\n{col}:")
            for test_name, result in tests.items():
                is_normal = result.get('is_normal', False)
                status = "正態(tài)" if is_normal else "非正態(tài)"
                print(f"  {test_name}: p={result.get('p_value', 0):.4f} ({status})")
        
        # 置信區(qū)間
        print("\n3. 置信區(qū)間分析:")
        ci_results = self.confidence_intervals()
        for col, result in ci_results.items():
            print(f"\n{col}:")
            print(f"  均值: {result['mean']:.4f}")
            print(f"  95%置信區(qū)間: [{result['ci_0.95'][0]:.4f}, {result['ci_0.95'][1]:.4f}]")
            print(f"  Bootstrap CI: [{result['bootstrap_ci'][0]:.4f}, {result['bootstrap_ci'][1]:.4f}]")
        
        return {
            'outliers': outlier_results,
            'normality': normality_results,
            'confidence_intervals': ci_results
        }

# 使用示例
advanced_stats = AdvancedStatistics(iris_df)
advanced_report = advanced_stats.generate_advanced_report()

以上就是從入門(mén)到實(shí)戰(zhàn)詳解Python數(shù)據(jù)統(tǒng)計(jì)的完全指南的詳細(xì)內(nèi)容,更多關(guān)于Python數(shù)據(jù)統(tǒng)計(jì)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 深入探討Python中弱引用機(jī)制與應(yīng)用

    深入探討Python中弱引用機(jī)制與應(yīng)用

    在Python編程中,弱引用(Weak Reference)是一種特殊的引用方式,本文將深入探討Python的弱引用機(jī)制,介紹其用法、原理及實(shí)際應(yīng)用場(chǎng)景,希望對(duì)大家有所幫助
    2024-03-03
  • python實(shí)現(xiàn)逆濾波與維納濾波示例

    python實(shí)現(xiàn)逆濾波與維納濾波示例

    今天小編就為大家分享一篇python實(shí)現(xiàn)逆濾波與維納濾波示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • 實(shí)例解析Python的Twisted框架中Deferred對(duì)象的用法

    實(shí)例解析Python的Twisted框架中Deferred對(duì)象的用法

    Deferred對(duì)象在Twsited框架中用于處理回調(diào),這對(duì)于依靠異步的Twisted來(lái)說(shuō)十分重要,接下來(lái)我們就以實(shí)例解析Python的Twisted框架中Deferred對(duì)象的用法
    2016-05-05
  • Python實(shí)現(xiàn)畫(huà)圖軟件功能方法詳解

    Python實(shí)現(xiàn)畫(huà)圖軟件功能方法詳解

    這篇文章主要介紹了 Python實(shí)現(xiàn)畫(huà)圖軟件功能方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • python基于Kivy寫(xiě)一個(gè)圖形桌面時(shí)鐘程序

    python基于Kivy寫(xiě)一個(gè)圖形桌面時(shí)鐘程序

    這篇文章主要介紹了python如何基于Kivy 寫(xiě)一個(gè)桌面時(shí)鐘程序,幫助大家更好的利用python開(kāi)發(fā)圖形程序,感興趣的朋友可以了解下
    2021-01-01
  • Python入門(mén)之模塊和包用法詳解

    Python入門(mén)之模塊和包用法詳解

    這篇文章主要為大家詳細(xì)介紹一下Python中的包與模塊的使用,文中的示例講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,感興趣的小伙伴可以學(xué)習(xí)一下
    2022-07-07
  • python scp 批量同步文件的實(shí)現(xiàn)方法

    python scp 批量同步文件的實(shí)現(xiàn)方法

    今天小編就為大家分享一篇python scp 批量同步文件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Matplotlib多子圖使用一個(gè)圖例的實(shí)現(xiàn)

    Matplotlib多子圖使用一個(gè)圖例的實(shí)現(xiàn)

    多子圖是Matplotlib中的一個(gè)功能,可以在同一圖形中創(chuàng)建多個(gè)子圖,本文主要介紹了Matplotlib多子圖使用一個(gè)圖例的實(shí)現(xiàn),感興趣的可以了解一下
    2023-08-08
  • python中數(shù)組和矩陣乘法及使用總結(jié)(推薦)

    python中數(shù)組和矩陣乘法及使用總結(jié)(推薦)

    這篇文章主要介紹了python中數(shù)組和矩陣乘法及使用總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 在Heroku云平臺(tái)上部署Python的Django框架的教程

    在Heroku云平臺(tái)上部署Python的Django框架的教程

    這篇文章主要介紹了在Heroku云平臺(tái)上部署Python的Django框架的教程,Heroku云平臺(tái)使用了Git版本控制系統(tǒng),所以本教程主要提供了配置所需要的Git腳本,需要的朋友可以參考下
    2015-04-04

最新評(píng)論

鄯善县| 班玛县| 明光市| 甘南县| 孟津县| 金昌市| 通渭县| 新乡县| 油尖旺区| 方正县| 云和县| 苗栗县| 佛山市| 许昌县| 闻喜县| 黎川县| 桃园县| 慈溪市| 且末县| 尤溪县| 信宜市| 文安县| 乌兰察布市| 明光市| 吴旗县| 临夏市| 西安市| 长阳| 新闻| 东乡族自治县| 疏附县| 金塔县| 桃园市| 博湖县| 中山市| 克东县| 日喀则市| 嘉义市| 临桂县| 晋中市| 阿图什市|