Machine Learning · کاربردی

تحلیل احساسات متن

متن انگلیسی Amazon/IMDb/Yelp را به مثبت (۱) و منفی (۰) دسته‌بندی کن.

۱۰۰ دقیقه · ۲۸۰ XP · Python · Scikit-learn · Evaluation

هدف و راهنمای انجام

جداسازی آموزش و ارزیابی و سنجش تعمیم مدل

ورودی

train: لیست سطرهای دارای label؛ test: لیست ویژگی‌های بدون هدف؛ features: ['text']؛ target: label

خروجی

result.json شامل predictions با یک مقدار به‌ازای هر سطر test؛ عدد برای رگرسیون و برچسب موجود در train برای دسته‌بندی.

  1. request.json شامل train دارای برچسب و test بدون برچسب است؛ فایل CSV محلی برای مطالعه است و در ارزیابی به محیط ارسال نمی‌شود.
  2. train را به ویژگی‌ها و target جدا کن؛ داده ارزیابی بیرونی را هرگز برای fit استفاده نکن.
  3. از TfidfVectorizer و LogisticRegression در یک Pipeline استفاده کن؛ واژگان فقط از train استخراج شوند.
  4. برای تمرین، با train_test_split و random_state=42 بخشی از train را نگه دار و مدل را با MAE یا accuracy/F1 بسنج.
  5. مدل یا Pipeline را فقط روی داده آموزش fit کن؛ سپس برای تمام سطرهای test پیش‌بینی کن.
  6. predictions را به ترتیب test در result.json ذخیره کن؛ امتیاز نهایی را داور روی برچسب‌های خارج محیط محاسبه می‌کند.

تقسیم داده را داور هم مستقل انجام می‌دهد؛ موفقیت صرف اجرای برنامه یا metric چاپ‌شده کافی نیست. random_state=42 را برای بازتولیدپذیری تنظیم کن.

مثال ورودی و خروجی

{
    "input": {
        "train": [
            {
                "text": "The best scene in the movie was when Gerardo is trying to find a song that keeps running through his head.  ",
                "label": "1"
            },
            {
                "text": "first time there and might just be the last.",
                "label": "0"
            },
            {
                "text": "Don't trust their website and don't expect any helpful support.",
                "label": "0"
            },
            {
                "text": "Everything was good and tasty!",
                "label": "1"
            },
            {
                "text": "The battery life is highly unacceptable.",
                "label": "0"
            },
            {
                "text": "Same evening, him and I are both drastically sick.",
                "label": "0"
            },
            {
                "text": "This is the number one best TH game in the series.  ",
                "label": "1"
            },
            {
                "text": "I believe the screenwriter did a good job of tying up the loose ends.  ",
                "label": "1"
            },
            {
                "text": "Disappointing accessory from a good manufacturer.",
                "label": "0"
            },
            {
                "text": "We will not be coming back.",
                "label": "0"
            },
            {
                "text": "Brilliance indeed.  ",
                "label": "1"
            },
            {
                "text": "Excellent short film.  ",
                "label": "1"
            },
            {
                "text": "A world better than 95% of the garbage in the theatres today.  ",
                "label": "1"
            },
            {
                "text": "I've only had my bluetooth for a few weeks, but I really like it.",
                "label": "1"
            },
            {
                "text": "The Macarons here are insanely good.",
                "label": "1"
            },
            {
                "text": "This is a great little item.",
                "label": "1"
            },
            {
                "text": "What the hell kind of crap is that?!  ",
                "label": "0"
            },
            {
                "text": "I love the decor with the Chinese calligraphy wall paper.",
                "label": "1"
            },
            {
                "text": "Things that went wrong: - They burned the saganaki.",
                "label": "0"
            },
            {
                "text": "However, I recently watched the whole thing again on DVD, and I was completely struck by how extremely stupid the storyline was - how it contained holes, inconsistencies and - frankly - a whole lot of crap - and how horrid the dancing was.  ",
                "label": "0"
            },
            {
                "text": "That was done in the second movie.  ",
                "label": "0"
            },
            {
                "text": "Delicious and I will absolutely be back!",
                "label": "1"
            },
            {
                "text": "Pretty cool I would say.",
                "label": "1"
            },
            {
                "text": "Go rent it.  ",
                "label": "1"
            },
            {
                "text": "There is so much good food in Vegas that I feel cheated for wasting an eating opportunity by going to Rice and Company.",
                "label": "0"
            },
            {
                "text": "AVOID THIS ESTABLISHMENT!",
                "label": "0"
            },
            {
                "text": "If there were zero stars I would give it zero stars.",
                "label": "0"
            },
            {
                "text": "It's been my choice headset for years.Great sound; good volume; good noise cancellation.",
                "label": "1"
            },
            {
                "text": "The story line is just awful!  ",
                "label": "0"
            },
            {
                "text": "This second appearance of Mickey Mouse (following the silent PLANE CRAZY earlier that year) is probably his most famous film--mostly because it was so ground-breaking.  ",
                "label": "1"
            },
            {
                "text": "[...] down the drain because of a weak snap!",
                "label": "0"
            },
            {
                "text": "Crust is not good.",
                "label": "0"
            },
            {
                "text": "I own a Jabra Earset and was very happy with it, but the sound quality, especially outgoing, on this is better.",
                "label": "1"
            },
            {
                "text": "it did not work in my cell phone plug i am very up set with the charger!.",
                "label": "0"
            },
            {
                "text": "Appears to actually outperform the original battery from China that came with my V325i.",
                "label": "1"
            },
            {
                "text": "One thing I hate is the mode set button at the side.",
                "label": "0"
            },
            {
                "text": "There are many continuity errors: one other user commented on different cars in the garage, Joe's glasses...the one that got to me the most was the fact Joe's facial hair configuration seemed to change from scene to scene.  ",
                "label": "0"
            },
            {
                "text": "Some may say this buffet is pricey but I think you get what you pay for and this place you are getting quite a lot!",
                "label": "1"
            },
            {
                "text": "I would avoid this place if you are staying in the Mirage.",
                "label": "0"
            },
            {
                "text": "It's just lame.  ",
                "label": "0"
            },
            {
                "text": "This film tries to be a serious and sophisticated thriller\/horror flick and it fails miserably.  ",
                "label": "0"
            },
            {
                "text": "So good I am going to have to review this place twice - once hereas a tribute to the place and once as a tribute to an event held here last night.",
                "label": "1"
            },
            {
                "text": "Overall I was not impressed and would not go back.",
                "label": "0"
            },
            {
                "text": "Great bluetooth!.",
                "label": "1"
            },
            {
                "text": "Great phone!.",
                "label": "1"
            },
            {
                "text": "Finally, after three or four times the spring of the latch broke and I could not use it any longer on the visor.",
                "label": "0"
            },
            {
                "text": "Couldn't ask for a more satisfying meal.",
                "label": "1"
            },
            {
                "text": "The Jabra EarGels fit my ears very well.",
                "label": "1"
            },
            {
                "text": "Also, I feel like the chips are bought, not made in house.",
                "label": "0"
            },
            {
                "text": "This place is horrible and way overpriced.",
                "label": "0"
            },
            {
                "text": "Linked to my phone without effort.",
                "label": "1"
            },
            {
                "text": "Wow, what a bad film.  ",
                "label": "0"
            },
            {
                "text": "Great software for Motorolas.",
                "label": "1"
            },
            {
                "text": "The color is even prettier than I thought it would be, and the graphics are incredibly sharp.",
                "label": "1"
            },
            {
                "text": "On three different occasions I asked for well done or medium well, and all three times I got the bloodiest piece of meat on my plate.",
                "label": "0"
            },
            {
                "text": "Instead, we got a bore fest about a whiny, spoiled brat babysitting.  ",
                "label": "0"
            },
            {
                "text": "The cable looks so thin and flimsy, it is scary.",
                "label": "0"
            },
            {
                "text": "Today is the second time I've been to their lunch buffet and it was pretty good.",
                "label": "1"
            },
            {
                "text": "Mishima is extremely uninteresting.  ",
                "label": "0"
            },
            {
                "text": "I was VERY disappointed!!",
                "label": "0"
            }
        ],
        "test": [
            {
                "text": "Jawbone Era is awesome too!"
            },
            {
                "text": "My sister has one also and she loves it."
            },
            {
                "text": "Call me a nut, but I think this is one of the best movies ever.  "
            }
        ],
        "features": [
            "text"
        ],
        "target": "label"
    },
    "output": {
        "predictions": [
            "برچسب یا عدد برای هر سطر test"
        ]
    }
}

معیارهای موفقیت

  • accuracy و macro-F1 حداقل 0.72
  • شکل و تعداد و نوع پیش‌بینی‌ها مطابق ورودی
  • ارزیابی روی دو تقسیم تصادفی مستقل؛ برچسب test به کد ارسال نمی‌شود.

کتابخانه‌ها: pandas, scikit-learn, numpy

منبع و وضعیت دیتاست‌ها

sentiment.csv · CC BY 4.0 · Kotzias (2015), Sentiment Labelled Sentences, UCI, doi:10.24432/C57604. English Amazon/IMDb/Yelp text, binary labels.

https://archive.ics.uci.edu/static/public/331/sentiment+labelled+sentences.zip

نسخه محلی همراه پروژه؛ ۳۰۰۰ ردیف. هنگام اجرا دانلود خارجی لازم نیست.

برای نوشتن و اجرای کد وارد شوید