Spaces:
Sleeping
Sleeping
| import numpy as np | |
| from typing import Dict, Tuple | |
| def analyze_drift(X: np.ndarray, y: np.ndarray, drift_points: np.ndarray, drift_type: str) -> Dict[str, any]: | |
| """๋๋ฆฌํํธ ๋ฐ์ดํฐ ๋ถ์""" | |
| analysis = { | |
| "drift_type": drift_type, | |
| "total_samples": len(X), | |
| "num_drift_points": len(drift_points), | |
| "drift_locations": drift_points.tolist() if len(drift_points) > 0 else [], | |
| } | |
| # ์ ์ฒด ํต๊ณ (๋ชจ๋ drift type์ ์ฐ์ ๊ฐ์ผ๋ก ์ฒ๋ฆฌ) | |
| analysis["mean_y"] = float(np.mean(y)) | |
| analysis["std_y"] = float(np.std(y)) | |
| analysis["min_y"] = float(np.min(y)) | |
| analysis["max_y"] = float(np.max(y)) | |
| # ์ธ๊ทธ๋จผํธ๋ณ ๋ถ์ | |
| segments = [] | |
| segment_boundaries = [0] + drift_points.tolist() + [len(X)] | |
| for i in range(len(segment_boundaries) - 1): | |
| start = segment_boundaries[i] | |
| end = segment_boundaries[i + 1] | |
| segment_y = y[start:end] | |
| segment_X = X[start:end] | |
| # ์ ํ ํ๊ท ๊ณ์ ๊ณ์ฐ | |
| if len(segment_X) > 1: | |
| coeffs = np.polyfit(segment_X, segment_y, 1) | |
| slope = float(coeffs[0]) | |
| intercept = float(coeffs[1]) | |
| else: | |
| slope = 0.0 | |
| intercept = float(segment_y[0]) if len(segment_y) > 0 else 0.0 | |
| segments.append({ | |
| "segment_id": i, | |
| "start_idx": int(start), | |
| "end_idx": int(end), | |
| "mean": float(np.mean(segment_y)), | |
| "std": float(np.std(segment_y)), | |
| "slope": slope, | |
| "intercept": intercept | |
| }) | |
| analysis["segments"] = segments | |
| return analysis | |
| def format_analysis_summary(analysis: Dict) -> str: | |
| """๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ์ฌ๋์ด ์ฝ๊ธฐ ์ฌ์ด ํ์์ผ๋ก ํฌ๋งท""" | |
| drift_type = analysis['drift_type'] | |
| summary = f""" | |
| ## ๋๋ฆฌํํธ ๋ถ์ ๊ฒฐ๊ณผ | |
| **๋๋ฆฌํํธ ์ ํ:** {drift_type.upper()} | |
| **์ ์ฒด ๋ฐ์ดํฐ:** | |
| - ์ด ์ํ ์: {analysis['total_samples']} | |
| - ๋๋ฆฌํํธ ๋ฐ์ ํ์: {analysis['num_drift_points']} | |
| - ํ๊ท ๊ฐ: {analysis['mean_y']:.2f} | |
| - ํ์คํธ์ฐจ: {analysis['std_y']:.2f} | |
| - ๋ฒ์: [{analysis['min_y']:.2f}, {analysis['max_y']:.2f}] | |
| **์ธ๊ทธ๋จผํธ๋ณ ๋ถ์:** | |
| """ | |
| for seg in analysis['segments']: | |
| summary += f""" | |
| **์ธ๊ทธ๋จผํธ {seg['segment_id'] + 1}** (์ํ {seg['start_idx']}-{seg['end_idx']}) | |
| - ํ๊ท : {seg['mean']:.2f} | |
| - ํ์คํธ์ฐจ: {seg['std']:.2f} | |
| - ํธ๋ ๋: y = {seg['slope']:.4f}x + {seg['intercept']:.2f} | |
| """ | |
| return summary | |