جواب «Step 2: Build X and y» چالش 2 از جلسهی 24
سؤال
قدم دوم: از لیست messages (جفتهای متن و برچسب) دو لیست X و y بساز: X ویژگیهای هر پیام و y برچسبها. برنامه X[0]، y[0] و len(X) را چاپ میکند: [4, 5, 1]، spam، 6.
راهنما
for text, label in messages: X.append(features(text)); y.append(label).
یک جواب درست
SPAM_WORDS = ["free", "win", "now", "click", "prize"]
def features(text):
words = text.lower().split()
spam_count = 0
for w in words:
if w.strip("!") in SPAM_WORDS:
spam_count += 1
return [spam_count, len(words), text.count("!")]
messages = [
("Win a FREE prize now!", "spam"),
("free money win now click", "spam"),
("you won click now!!", "spam"),
("hi are you coming to class", "ham"),
("see you at football practice", "ham"),
("mum says dinner is ready", "ham"),
]
X = []
y = []
for text, label in messages:
X.append(features(text))
y.append(label)
print(X[0])
print(y[0])
print(len(X))
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن