-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcode
85 lines (65 loc) · 2.66 KB
/
code
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
import pandas as pd
import numpy as np
import random
# Machine Learning Packages
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
# Load Url Data
urls_data = pd.read_csv("url_features.csv")
type(urls_data)
urls_data.head()
def makeTokens(f):
tkns_BySlash = str(f.encode('utf-8')).split('/') # make tokens after splitting by slash
total_Tokens = []
for i in tkns_BySlash:
tokens = str(i).split('-') # make tokens after splitting by dash
tkns_ByDot = []
for j in range(0,len(tokens)):
temp_Tokens = str(tokens[j]).split('.') # make tokens after splitting by dot
tkns_ByDot = tkns_ByDot + temp_Tokens
total_Tokens = total_Tokens + tokens + tkns_ByDot
total_Tokens = list(set(total_Tokens)) #remove redundant tokens
if 'com' in total_Tokens:
total_Tokens.remove('com') #removing .com since it occurs a lot of times and it should not be included in our features
return total_Tokens
y = urls_data["label"]
url_list = urls_data["url"]
#Using Default Tokenizer
#vectorizer = TfidfVectorizer()
# Using Custom Tokenizer
vectorizer = TfidfVectorizer(tokenizer=makeTokens)
X = vectorizer.fit_transform(url_list)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Model Building
#using logistic regression
print("TRAINING PHASE")
logit = LogisticRegression()
logit.fit(X_train, y_train)
print("Accuracy ",logit.score(X_test, y_test))
#print("coefficient :\n",logit.coef_)
#print("Intercept:\n",logit.intercept_)
PRINT("TESTING PHASE")
X_predict = ["google.com/search=jcharistech",
"google.com/search=faizanahmad",
"pakistanifacebookforever.com/getpassword.php/",
"www.radsport-voggel.de/wp-admin/includes/log.exe",
"ahrenhei.without-transfer.ru/nethost.exe ",
"www.itidea.it/centroesteticosothys/img/_notes/gum.exe"]
X_predict = vectorizer.transform(X_predict)
New_predict = logit.predict(X_predict)
print("THE GIVEN URLS ARE : ",New_predict)
"""X_predict1 = ["www.buyfakebillsonlinee.blogspot.com",
"www.unitedairlineslogistics.com",
"www.stonehousedelivery.com",
"www.silkroadmeds-onlinepharmacy.com" ]
X_predict1 = vectorizer.transform(X_predict1)
New_predict1 = logit.predict(X_predict1)
print(New_predict1)
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(url_list)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
logit = LogisticRegression() #using logistic regression
logit.fit(X_train, y_train)
print("Accuracy ",logit.score(X_test, y_test))"""