Spaces:
Paused
Paused
File size: 10,812 Bytes
e538e84 f3d41f0 e538e84 f3d41f0 e538e84 f3d41f0 e538e84 |
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 |
# �� Copilot �ͦ�
import pandas as pd
import numpy as np
from typing import Dict, List
import json
from transformers import pipeline
from datasets import Dataset
class RentalAnalyzer:
"""���θ�Ƥ��R�� - Hugging Face Spaces����"""
def __init__(self, df: pd.DataFrame, use_hf_models: bool = True):
"""
��l�Ƥ��R��
Args:
df: ����DataFrame
use_hf_models: �O�_�ϥ�Hugging Face�ҫ�
"""
self.df = df.copy()
self.use_hf_models = use_hf_models
self.analysis_results = {}
# ��l��Hugging Face�ҫ�
self.sentiment_analyzer = None
if use_hf_models:
try:
# �ϥθ��p���^�屡�P���R�ҫ��A�קK���J���D
self.sentiment_analyzer = pipeline(
"sentiment-analysis",
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
return_all_scores=False
)
except Exception as e:
print(f"Warning: Could not load Hugging Face model: {e}")
# ���ըϥιw�]�ҫ�
try:
self.sentiment_analyzer = pipeline("sentiment-analysis")
except Exception as e2:
print(f"Warning: Could not load any sentiment model: {e2}")
self.use_hf_models = False
def clean_data(self) -> pd.DataFrame:
"""�M�~���"""
# �������Ƹ��
original_count = len(self.df)
self.df = self.df.drop_duplicates(subset=['title', 'address', 'price'])
# �B�z�������
self.df['price'] = pd.to_numeric(self.df['price'], errors='coerce')
self.df = self.df[self.df['price'] > 0]
# �B�z�W�Ƹ��
self.df['area'] = pd.to_numeric(self.df['area'], errors='coerce')
self.df = self.df[self.df['area'] > 0]
# �p��C�W����
self.df['price_per_ping'] = self.df['price'] / self.df['area']
# �������`��
self.df = self.remove_outliers(self.df, 'price')
# �K�[�������
self.add_categorical_columns()
return self.df
def remove_outliers(self, df: pd.DataFrame, column: str) -> pd.DataFrame:
"""�������`�ȡ]�ϥ�IQR��k�^"""
Q1 = df[column].quantile(0.25)
Q3 = df[column].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
return df[(df[column] >= lower_bound) & (df[column] <= upper_bound)]
def add_categorical_columns(self):
"""�K�[�������"""
# �����϶�
self.df['price_range'] = pd.cut(
self.df['price'],
bins=[0, 20000, 25000, 30000, 35000, float('inf')],
labels=['<20K', '20-25K', '25-30K', '30-35K', '>35K']
)
# �W�ư϶�
self.df['area_range'] = pd.cut(
self.df['area'],
bins=[0, 25, 30, 35, 40, float('inf')],
labels=['<25�W', '25-30�W', '30-35�W', '35-40�W', '>40�W']
)
def basic_statistics(self) -> Dict:
"""�έp���R"""
stats = {
'total_properties': len(self.df),
'price_stats': {
'mean': round(self.df['price'].mean(), 2),
'median': round(self.df['price'].median(), 2),
'std': round(self.df['price'].std(), 2),
'min': int(self.df['price'].min()),
'max': int(self.df['price'].max()),
'q25': round(self.df['price'].quantile(0.25), 2),
'q75': round(self.df['price'].quantile(0.75), 2)
},
'area_stats': {
'mean': round(self.df['area'].mean(), 2),
'median': round(self.df['area'].median(), 2),
'min': round(self.df['area'].min(), 1),
'max': round(self.df['area'].max(), 1)
},
'price_per_ping_stats': {
'mean': round(self.df['price_per_ping'].mean(), 2),
'median': round(self.df['price_per_ping'].median(), 2),
'min': round(self.df['price_per_ping'].min(), 2),
'max': round(self.df['price_per_ping'].max(), 2)
}
}
return stats
def price_distribution_analysis(self) -> Dict:
"""�����������R"""
distribution = self.df['price_range'].value_counts().sort_index()
return distribution.to_dict()
def area_distribution_analysis(self) -> Dict:
"""�W�Ƥ������R"""
distribution = self.df['area_range'].value_counts().sort_index()
return distribution.to_dict()
def keywords_analysis(self) -> Dict:
"""����r���R"""
# �w�q�Ыά�������r
keywords = [
'�B', '��', '�q��', '���x', '������', '�z�O',
'�ĥ�', '�q��', '�w�R', '�K�Q', '�ͬ�����', '�ǰ�',
'���s', '���C', '�a��', '�a�q', '�N��', '�~���',
'���N�]', '�R�e', '��G', '��l�W', '���s', '�����P'
]
keyword_counts = {keyword: 0 for keyword in keywords}
descriptions = self.df['raw_info'].dropna().tolist()
for desc in descriptions:
for keyword in keywords:
if keyword in str(desc):
keyword_counts[keyword] += 1
# �ƧǨè��e10��
sorted_keywords = dict(
sorted(keyword_counts.items(), key=lambda x: x[1], reverse=True)[:10]
)
return sorted_keywords
def huggingface_analysis(self) -> Dict:
"""�ϥ�Hugging Face�ҫ��i����R"""
if not self.use_hf_models or self.sentiment_analyzer is None:
return {}
try:
descriptions = self.df['raw_info'].dropna().tolist()[:10] # ���e10���קK�W��
if not descriptions:
return {}
# ���P���R
sentiments = []
for desc in descriptions:
try:
result = self.sentiment_analyzer(desc[:100]) # �������
sentiments.append(result[0]['label'] if result else 'NEUTRAL')
except:
sentiments.append('NEUTRAL')
# �έp���P����
sentiment_counts = {}
for sentiment in sentiments:
sentiment_counts[sentiment] = sentiment_counts.get(sentiment, 0) + 1
# �Ы�Dataset
hf_dataset = Dataset.from_dict({
'text': descriptions,
'price': self.df['price'].head(len(descriptions)).tolist(),
'area': self.df['area'].head(len(descriptions)).tolist(),
'sentiment': sentiments
})
return {
'sentiment_distribution': sentiment_counts,
'dataset_size': len(hf_dataset),
'sample_analysis': True
}
except Exception as e:
print(f"Hugging Face analysis error: {e}")
return {}
def correlation_analysis(self) -> Dict:
"""�����ʤ��R"""
numeric_columns = ['price', 'area', 'price_per_ping']
available_columns = [
col for col in numeric_columns
if col in self.df.columns and not self.df[col].isna().all()
]
if len(available_columns) < 2:
return {}
correlation_matrix = self.df[available_columns].corr()
correlations = {}
for i, col1 in enumerate(available_columns):
for j, col2 in enumerate(available_columns):
if i < j: # �קK����
correlations[f"{col1}_vs_{col2}"] = round(
correlation_matrix.loc[col1, col2], 3
)
return correlations
def generate_insights(self) -> List[str]:
"""�ͦ����R�}��"""
insights = []
# �έp�}��
if 'basic_stats' in self.analysis_results:
stats = self.analysis_results['basic_stats']
insights.append(f"�@��� {stats['total_properties']} ���ŦX�����Ϊ���")
insights.append(f"���������� {stats['price_stats']['mean']:,} ��")
insights.append(f"��������Ƭ� {stats['price_stats']['median']:,} ��")
if stats['price_stats']['mean'] > stats['price_stats']['median']:
insights.append("���������V�k���סA�s�b�����������������")
# �������R�}��
if 'price_distribution' in self.analysis_results:
dist = self.analysis_results['price_distribution']
if dist:
most_common_range = max(dist, key=dist.get)
count = dist[most_common_range]
percentage = (count / self.analysis_results['basic_stats']['total_properties']) * 100
insights.append(f"�̱`���������϶��O {most_common_range}�A�� {percentage:.1f}%")
# Hugging Face���R�}��
if 'hf_analysis' in self.analysis_results and self.analysis_results['hf_analysis']:
hf_results = self.analysis_results['hf_analysis']
if 'sentiment_distribution' in hf_results:
insights.append("�w�ϥ�Hugging Face�ҫ��i�污�P���R")
return insights
def run_analysis(self) -> Dict:
"""���槹����R"""
# �M�~���
self.clean_data()
# �έp
self.analysis_results['basic_stats'] = self.basic_statistics()
# �������R
self.analysis_results['price_distribution'] = self.price_distribution_analysis()
self.analysis_results['area_distribution'] = self.area_distribution_analysis()
# ����r���R
self.analysis_results['keywords_analysis'] = self.keywords_analysis()
# �����ʤ��R
self.analysis_results['correlation'] = self.correlation_analysis()
# Hugging Face���R
if self.use_hf_models:
self.analysis_results['hf_analysis'] = self.huggingface_analysis()
# �ͦ��}��
self.analysis_results['insights'] = self.generate_insights()
return self.analysis_results |