-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_preprocessing.py
More file actions
45 lines (37 loc) · 1.46 KB
/
data_preprocessing.py
File metadata and controls
45 lines (37 loc) · 1.46 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
import pandas as pd
import re
from urllib.parse import urlparse
from tld import get_tld
# 1. 데이터 로드
df = pd.read_csv("urldata.csv")
# 2. 라벨 이진화
df['label'] = df['label'].apply(lambda x: 0 if x == 'benign' else 1)
# 3. 각 feature 계산
df['hostname_length'] = df['url'].apply(lambda x: len(urlparse(x).netloc))
df['count_dir'] = df['url'].apply(lambda x: urlparse(x).path.count('/'))
df['count-www'] = df['url'].apply(lambda x: x.count('www'))
df['url_length'] = df['url'].apply(lambda x: len(str(x)))
def fd_length(url):
try:
return len(urlparse(url).path.split('/')[1])
except:
return 0
df['fd_length'] = df['url'].apply(fd_length)
df['count-'] = df['url'].apply(lambda x: x.count('-'))
df['count.'] = df['url'].apply(lambda x: x.count('.'))
def get_tld_length(url):
try:
return len(get_tld(url, fail_silently=True))
except:
return -1
df['tld_length'] = df['url'].apply(get_tld_length)
df['count-digits'] = df['url'].apply(lambda x: sum(c.isdigit() for c in x))
df['count='] = df['url'].apply(lambda x: x.count('='))
# 4. 필요한 컬럼만 저장
final_columns = ['url', 'label'] + [
'hostname_length', 'count_dir', 'count-www',
'url_length', 'fd_length', 'count-', 'count.',
'tld_length', 'count-digits', 'count='
]
df[final_columns].to_csv("urldata_features10.csv", index=False)
print("10개 feature 기반 데이터 저장 완료: urldata_features10.csv")