ZealPyae commited on
Commit
fdad801
·
verified ·
1 Parent(s): 945d97a

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +54 -0
main.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from sklearn.feature_extraction.text import TfidfVectorizer
3
+ from sklearn.model_selection import train_test_split
4
+ from sklearn.naive_bayes import MultinomialNB
5
+ from sklearn.metrics import accuracy_score
6
+ import nltk
7
+ from nltk.corpus import stopwords
8
+ import re
9
+
10
+ # Download NLTK stopwords
11
+ nltk.download('stopwords')
12
+
13
+ # Load dataset
14
+ # Assuming you have a CSV file with 'url' and 'label' columns
15
+ data = pd.read_csv('malicious_phish.csv')
16
+
17
+ # Preprocess URLs
18
+ def preprocess_url(url):
19
+ url = re.sub(r"http\S+", "", url) # Remove http links
20
+ url = re.sub(r"\d+", "", url) # Remove digits
21
+ url = re.sub(r"\W", " ", url) # Remove non-word characters
22
+ url = url.lower() # Convert to lowercase
23
+ return url
24
+
25
+ data['url'] = data['url'].apply(preprocess_url)
26
+
27
+ # Split data into training and testing sets
28
+ X = data['url']
29
+ y = data['type']
30
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
31
+
32
+ # Vectorize URLs using TF-IDF
33
+ vectorizer = TfidfVectorizer(stop_words=stopwords.words('english'))
34
+ X_train_tfidf = vectorizer.fit_transform(X_train)
35
+ X_test_tfidf = vectorizer.transform(X_test)
36
+
37
+ # Train a Naive Bayes classifier
38
+ model = MultinomialNB()
39
+ model.fit(X_train_tfidf, y_train)
40
+
41
+ # Predict and evaluate
42
+ y_pred = model.predict(X_test_tfidf)
43
+ accuracy = accuracy_score(y_test, y_pred)
44
+ print(f"Accuracy: {accuracy * 100:.2f}%")
45
+
46
+ # Function to predict if a URL is malicious
47
+ def predict_url(url):
48
+ processed_url = preprocess_url(url)
49
+ vectorized_url = vectorizer.transform([processed_url])
50
+ prediction = model.predict(vectorized_url)
51
+ return prediction[0]
52
+
53
+ # Example usage
54
+ print(predict_url("br-icloud.com.br"))