-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_feature_extraction.py
More file actions
64 lines (52 loc) · 2.24 KB
/
Copy path2_feature_extraction.py
File metadata and controls
64 lines (52 loc) · 2.24 KB
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
import pandas as pd
import numpy as np
import feature_utils
import multiprocessing
from tqdm import tqdm
import os
# --- CẤU HÌNH ---
N_JOBS = multiprocessing.cpu_count() # Sử dụng tất cả các cores
FEATURE_COLUMNS = feature_utils.FEATURE_NAMES
def process_chunk(chunk):
"""Xử lý một phần của DataFrame."""
# Quan trọng: Mỗi process con không cần reload feature_utils,
# nhưng global variables trong feature_utils sẽ được copy.
# Đảm bảo dictionary được load
if not feature_utils.COMMON_WORDS_SET:
feature_utils.load_google_10k()
results = []
for domain in chunk:
domain = str(domain)
feat_dict = feature_utils.get_features_dict(domain)
results.append([feat_dict[col] for col in FEATURE_COLUMNS])
return results
if __name__ == '__main__':
print(f"Dang doc du lieu 'dataset_full.csv'...")
try:
df = pd.read_csv('dataset_full.csv')
df['domain'] = df['domain'].astype(str)
except FileNotFoundError:
print("Khong tim thay file dataset! Hay chay 1_load.py truoc.")
exit()
print(f"Bat dau trich xuat dac trung voi {N_JOBS} luong (cores)...")
# Chia dữ liệu thành các chunks
domains = df['domain'].tolist()
chunk_size = len(domains) // N_JOBS + 1
chunks = [domains[i:i + chunk_size] for i in range(0, len(domains), chunk_size)]
# Xử lý song song
with multiprocessing.Pool(processes=N_JOBS) as pool:
# Sử dụng tqdm để hiện progress bar
results = list(tqdm(pool.imap(process_chunk, chunks), total=len(chunks), unit="chunk"))
# Gộp kết quả
flat_results = [item for sublist in results for item in sublist]
print("\nDang luu ket qua...")
df_features = pd.DataFrame(flat_results, columns=FEATURE_COLUMNS)
df_final = pd.concat([df, df_features], axis=1)
cols_to_keep = FEATURE_COLUMNS + ['label']
# Convert label to int if needed (usually it is already)
# Save
output_file = 'dataset_training_ready.csv'
df_final[cols_to_keep].to_csv(output_file, index=False)
print(f"\n[OK] Da luu '{output_file}'")
print(f"Tong so features: {len(FEATURE_COLUMNS)}")
print(f"Kich thuoc du lieu: {df_final.shape}")