|
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. TGIF! Everyone is talking about AI. Most people take courses. You're going to build it instead. Let's create a simple sentiment analyzer that reads customer reviews and classifies them as positive or negative. No machine learning libraries—just Python. 🎯 Define list of positive and negative words
In:
positive_words = ["good", "great", "excellent", "amazing", "love", "best"] negative_words = ["bad", "terrible", "awful", "hate", "worst", "horrible"] 👉🏻 Assign reviews as variables
In:
review1 = "This product is amazing and I love it" review2 = "Terrible quality, worst purchase ever" Lets do review1 first ✂️ Get all words from the review. Convert to lowercase (for easy matching)
In:
words = review1.lower().split() print(words)
Out:
['this', 'product', 'is', 'amazing', 'and', 'i', 'love', 'it']
✅ Count # positive words Count 1 when any word is positive. Add all such 1
In:
pos_count = sum(1 for word in words if word in positive_words) print(f"Positive words found: {pos_count}")
Out:
Positive words found: 2
⛔ Count # negative words Count 1 when any word is negative. Add all such 1
In:
neg_count = sum(1 for word in words if word in negative_words) print(f"Negative words found: {neg_count}")
Out:
Negative words found: 0 🏆 Define final sentiment
In:
if pos_count > neg_count: sentiment = "Positive" elif neg_count > pos_count: sentiment = "Negative" else: sentiment = "Neutral" print(f"Sentiment: {sentiment}")
Out:
Sentiment: Positive
✌ Same method for the 2nd review. Did you also get the same answer?
In:
words2 = review2.lower().split() print(words2)
Out:
['terrible', 'quality,', 'worst', 'purchase', 'ever']
In:
pos_count2 = sum(1 for word in words2 if word in positive_words) neg_count2 = sum(1 for word in words2 if word in negative_words) sentiment2 = "Positive" if pos_count2 > neg_count2 else "Negative" if neg_count2 > pos_count2 else "Neutral" print(f"Review 2: {sentiment2}")
Out:
Review 2: Negative
⭐📣 That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼
|
Beginner to Expert in Python in just 5 minutes
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. You have a spreadsheet with 500 numbers. Your boss asks: "What do you see?" Most will freeze, scroll, guess. Not you! 3 Python functions to turn raw numbers into answers. No calculator, excel sheets. Just code. Statistics isn't boring math. It's how you make decisions with data. Using just the statistics module we find "center" of any dataset. Any data analyst must know the basics‼️ 🎯 Compute simple...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Last week we introduced numpy. Created bunch of arrays, sliced them and did some math with them. To prepare data for machine learning or any analysis, we need to learn to control their shape. Today - Reshape flat lists into grids. Flatten 2D tables back to 1D arrays. Create arrays filled with zeros, ones, or random numbers. ⏪ Recap: See dimensions first In: import numpy as np sales = np.array([120, 340,...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We created arrays on Day 1. Sliced them on Day 2. Now comes the real computational power of NumPy. Doing math on entire datasets. No loops. No iteration. Just one line. Add monthly revenues across regions. Calculate profit margins. Find averages. All in seconds!! Let's dive in.. 🍃 Add/Subtract like a breeze In: import numpy as np jan_sales = np.array([120, 340, 210]) feb_sales = np.array([150, 290, 180])...