Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0f16517

Browse files
author
zhunago
committed
携程
1 parent 2234795 commit 0f16517

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

‎nowcoder/KL散度.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
import math
3+
4+
la = sys.stdin.readline().strip().split(" ")
5+
lb = sys.stdin.readline().strip().split(" ")
6+
7+
da = {}
8+
db = {}
9+
for ch in la:
10+
if ch not in da:
11+
da[ch] = 0
12+
da[ch] += 1
13+
for ch in lb:
14+
if ch not in db:
15+
db[ch] = 0
16+
db[ch] += 1
17+
18+
for key in da:
19+
da[key] /= len(la)
20+
for key in db:
21+
db[key] /= len(lb)
22+
23+
kl = 0
24+
for key in da:
25+
kl += da[key] * math.log(da[key]/db[key], 2)
26+
print('%.2f' % kl)

‎nowcoder/信息增益.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
import math
3+
4+
n = int(sys.stdin.readline().strip())
5+
6+
data = []
7+
for i in range(n):
8+
wordlist = sys.stdin.readline().strip().split(",")
9+
data.append(wordlist)
10+
11+
label = [0, 0]
12+
for sample in data:
13+
if sample[1] == "0":
14+
label[0] += 1
15+
else:
16+
label[1] += 1
17+
label[0] /= len(data)
18+
label[1] /= len(data)
19+
20+
if(label[0] == 0 or label[1] == 0):
21+
before = 0
22+
else:
23+
before = -(label[0] * math.log(label[0], 2) + label[1] * math.log(label[1], 2))
24+
25+
dd = {}
26+
for sample in data:
27+
if sample[0] not in dd:
28+
dd[sample[0]] = [0, 0, 0]
29+
if sample[1] == "0":
30+
dd[sample[0]][0] += 1
31+
else:
32+
dd[sample[0]][1] += 1
33+
dd[sample[0]][2] += 1
34+
after = 0
35+
for key in dd:
36+
d = dd[key]
37+
d[0] = d[0] / d[2]
38+
d[1] = d[1] / d[2]
39+
if(d[0] == 0 or d[1] == 0):
40+
temp = 0
41+
else:
42+
temp = -(d[0] * math.log(d[0], 2) + d[1] * math.log(d[1], 2))
43+
after += temp * (d[2] / len(data))
44+
45+
print('%.2f' % (before - after))
46+
47+

‎nowcoder/情感分类.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
import math
3+
4+
good = {"high", "great", "good", "easy", "worth", "cheap", "convenient" ,"fun", "cost-effective", "like", "ok", "fresh", "value", ""}
5+
bad = {"expensive", "inconvenient", "never", "bad", "poor"}
6+
notWord = {"nothing", "don't", "not", "too"}
7+
8+
words = sys.stdin.readline().strip().lower().split(" ")
9+
goodC = 0
10+
badC = 0
11+
notW = False
12+
for word in words:
13+
if word in good:
14+
goodC += 1
15+
if word in bad:
16+
badC += 1
17+
if word in notWord:
18+
notW = True
19+
20+
print(int(not notW and goodC > badC))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /