-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathno_graph_analysis.py
116 lines (83 loc) · 3.12 KB
/
no_graph_analysis.py
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
import networkx as nx
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from node2vec import Node2Vec
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn import preprocessing, feature_extraction
from sklearn.model_selection import train_test_split
from sklearn.manifold import TSNE
from sklearn.metrics import accuracy_score, f1_score, classification_report, mean_squared_error
from sklearn.model_selection import GridSearchCV
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline
from sklearn.decomposition import TruncatedSVD
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegressionCV
import warnings
import xgboost as xgb
warnings.filterwarnings('ignore')
from sklearn.cluster import KMeans
from utils import *
screen_name = "all_10k"
threshhold = 0.5
graph = nx.read_gml("{}/{}_features.gml".format(dump_location, screen_name))
graph_nodes = list(graph.nodes(data=True))
X_feat = []
node_targets = []
for index, result in enumerate(graph_nodes):
fake = result[1]["fake"]
feats = [result[1]["followers_count"], result[1]["friends_count"], result[1]["listed_count"], result[1]["statuses_count"], result[1]["verified"], result[1]["political"]]
X_feat.append(feats)
if fake >= threshhold:
node_targets.append(1)
else:
node_targets.append(0)
node_embeddings = (
X_feat
)
X = node_embeddings
y = np.array(node_targets)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
scaler = StandardScaler()
scaled_X_train = scaler.fit_transform(X_train)
scaled_X_test = scaler.transform(X_test)
print("\n Logistic Regession: ")
clf = LogisticRegressionCV(Cs=10, cv=10, max_iter=300)
clf.fit(scaled_X_train, y_train)
y_pred = clf.predict(scaled_X_test)
print(classification_report(y_test, y_pred))
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\n Naive Bayes: ")
gnb = GaussianNB()
gnb.fit(scaled_X_train, y_train)
y_pred = gnb.predict(scaled_X_test)
print(classification_report(y_test, y_pred))
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\n SVM: ")
clf = svm.SVC(kernel='rbf', gamma='scale')
clf.fit(scaled_X_train, y_train)
y_pred = clf.predict(scaled_X_test)
print(classification_report(y_test, y_pred))
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\n K Nearest Neighbors: ")
knn = KNeighborsClassifier(n_neighbors=10)
knn.fit(scaled_X_train, y_train)
y_pred = knn.predict(scaled_X_test)
print(classification_report(y_test, y_pred))
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\n Random Forest: ")
forest = RandomForestClassifier()
forest.fit(scaled_X_train, y_train)
y_pred = forest.predict(scaled_X_test)
print(classification_report(y_test, y_pred))
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\n XGBoost: ")
xgb_clf = xgb.XGBClassifier()
xgb_clf.fit(scaled_X_train, y_train)
y_pred = xgb_clf.predict(scaled_X_test)
print(classification_report(y_test, y_pred))
print("Accuracy:", accuracy_score(y_test, y_pred))