-
Notifications
You must be signed in to change notification settings - Fork 1
/
1.py
169 lines (95 loc) · 3.63 KB
/
1.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#{"query":{"bool":{"must":[{"match":{"BNF_Chapter_Code":"2"}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"aggs":{}}
#{"query":{"bool":{"must":[],"must_not":[],"should":[{"match":{"BNF_Chapter_Code":"1"}},{"match":{"BNF_Section_Code":""}},{"match":{"BNF_Paragraph_Code":"10101"}},{"match":{"BNF_Presentation_Code":""}},{"match":{"BNF_Subparagraph_Code":""}},{"match":{"BNF_Chemical_Substance":"Sodium"}}]}},"from":0,"size":10,"sort":[],"aggs":{}}
# coding: utf-8
# In[27]:
import pandas as pd
import numpy as np
import nltk
spam_data = pd.read_csv('spam.csv')
spam_data['target'] = np.where(spam_data['target']=='spam',1,0)
spam_data.head(10)
spam_data.describe()
# In[28]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(spam_data['text'],
spam_data['target'],
random_state=0)
# In[29]:
def answer_one():
total=spam_data.shape[0]
#cnt_spam = len(spam_data[spam_data['target'] == 1])
cnt_spam =spam_data[spam_data['target'] == 1].count()
spam_per=(cnt_spam*100)/total
#print(total_spam)
return total,cnt_spam,spam_per
# In[30]:
answer_one()
# In[32]:
from sklearn.feature_extraction.text import CountVectorizer
def answer_two():
vect = CountVectorizer().fit(X_train)
words=vect.get_feature_names()
token=len(words)
word_dist = nltk.FreqDist(words)
rslt=pd.DataFrame(word_dist.most_common(2))
print(rslt)
length=len(vect.get_feature_names())
#print(rslt)
return length,vect.get_feature_names(),word_dist,rslt,token
# In[6]:
length,name,word_dist,rslt,token=answer_two()
length,token
# In[35]:
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import roc_auc_score
def answer_three():
vect = CountVectorizer().fit(X_train)
x_train_vect =vect.transform(X_train)
x_test_vect = vect.transform(X_test)
clfNaive=MultinomialNB(alpha=0.1)
clfNaive.fit(x_train_vect,y_train)
predictions=clfNaive.predict(x_test_vect)
roc= roc_auc_score(y_test, predictions)
return roc,x_train_vect.shape,predictions,x_train_vect
# In[36]:
a,c,predictions,x_train_vect=answer_three()
a
#pd.DataFrame(X_test,predictions)
# In[9]:
from sklearn.feature_extraction.text import TfidfVectorizer
def answer_four():
tfvect=TfidfVectorizer().fit(X_train)
x_train_tfvect=tfvect.transform(X_train)
x_test_tfvect=tfvect.transform(X_test)
name=tfvect.get_feature_names()
feature_names = np.array(tfvect.get_feature_names())
sorted_tfidf_index = x_train_tfvect.max(0).toarray()[0].argsort()
return feature_names,sorted_tfidf_index
# In[10]:
feature_names,sorted_tfidf_index=answer_four()
print('Smallest tfidf:\n{}\n'.format(feature_names[sorted_tfidf_index[:10]]))
print('Largest tfidf: \n{}'.format(feature_names[sorted_tfidf_index[:-11:-1]]))
# In[11]:
print('3')
# In[12]:
def answer_five():
tfvect=TfidfVectorizer(min_df=5).fit(X_train)
x_train_tfvect=tfvect.transform(X_train)
x_test_tfvect=tfvect.transform(X_test)
clfNaive=MultinomialNB(alpha=0.1)
clfNaive.fit(x_train_tfvect,y_train)
predictions=clfNaive.predict(x_test_tfvect)
roc= roc_auc_score(y_test, predictions)
return roc
# In[13]:
answer_five()
# In[14]:
def answer_six():
cnt_spam =spam_data[spam_data['target'] == 1].count()
spam_data['char_count'] = spam_data['text'].str.len()
return cnt_spam
# In[15]:
answer_six()
print(spam_data.head())
spam_data.groupby(['target'])['char_count'].mean()
#spam_data['char_count'].mean()