Python 21to30
Python 21to30
Dataset Sample:
Review Sentiment
"Great product, very useful!" Positive
"Worst service ever." Negative
"It's okay, nothing special." Neutral
22.Implement a simple chatbot using Python's NLTK library that can respond to
basic user queries.
# Create chatbot
chatbot = Chat(pairs, reflections)
Example Conversation
Hello! I'm your chatbot. Type 'bye' to exit.
You: hi
Chatbot: Hi there!
You: bye
Chatbot: Goodbye!
23.Write a Python program to analyse sentiment in each text using the TextBlob
library.
# Classifying sentiment
if polarity > 0:
sentiment = "Positive"
elif polarity < 0:
sentiment = "Negative"
else:
sentiment = "Neutral"
# Display results
print(f"\nText: {text}")
print(f"Sentiment Score: {polarity}")
print(f"Overall Sentiment: {sentiment}")
Example Output
Input:
vbnet
Enter a text for sentiment analysis: This product is amazing and works
perfectly!
Output:
24.Create a machine learning model in Python to classify spam emails using the
Naive Bayes algorithm.
df["message"] = df["message"].apply(clean_text)
# Example Test
email = input("\nEnter an email to check if it's spam or not: ")
print("Prediction:", predict_spam(email))
Example Output
vbnet
CopyEdit
Model Accuracy: 0.98
Classification Report:
precision recall f1-score support
0 0.99 0.99 0.99 965
1 0.94 0.95 0.94 150
Enter an email to check if it's spam or not: Congratulations! You've won a free
iPhone! Click here to claim.
Prediction: Spam
# Example Test
area = float(input("\nEnter Area (sqft): "))
bedrooms = int(input("Enter Number of Bedrooms: "))
bathrooms = int(input("Enter Number of Bathrooms: "))
stories = int(input("Enter Number of Stories: "))
import pandas as pd
from sklearn.preprocessing import StandardScaler, LabelEncoder
# Load Dataset
df = pd.read_csv("data.csv") # Replace with your dataset
# Remove Duplicates
df.drop_duplicates(inplace=True)
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load Dataset
df = pd.read_csv("data.csv") # Replace with your dataset
# Histogram of a Feature
df.hist(figsize=(10,8))
plt.show()
def check_password_strength(password):
if len(password) < 8:
return "Weak: Password should be at least 8 characters long."
if not re.search(r"[A-Z]", password):
return "Weak: Include at least one uppercase letter."
if not re.search(r"\d", password):
return "Weak: Include at least one digit."
if not re.search(r"[!@#$%^&*]", password):
return "Weak: Include at least one special character."
return "Strong Password!"
29. From Yahoo Finance download a stock price in Python and find the Simple
Moving Average and print the first 100 values and the related graph.
import yfinance as yf
import matplotlib.pyplot as plt
# Plot SMA
plt.figure(figsize=(10,5))
plt.plot(stock['Close'], label="Close Price")
plt.plot(stock['SMA_50'], label="50-Day SMA", linestyle="dashed")
plt.legend()
plt.title("Stock Price and 50-Day SMA")
plt.show()
30. From Yahoo finance download a stock price in Python and draw the needed
graphs to present the Open, Close, High, Low, Adj. Close, Volume of five year data.
import yfinance as yf
import matplotlib.pyplot as plt
plt.subplot(2, 3, 2)
plt.plot(stock['High'], label='High Price', color='g')
plt.title("High Price")
plt.subplot(2, 3, 3)
plt.plot(stock['Low'], label='Low Price', color='r')
plt.title("Low Price")
plt.subplot(2, 3, 4)
plt.plot(stock['Close'], label='Close Price', color='b')
plt.title("Close Price")
plt.subplot(2, 3, 5)
plt.plot(stock['Adj Close'], label='Adjusted Close', color='purple')
plt.title("Adj Close Price")
plt.subplot(2, 3, 6)
plt.bar(stock.index, stock['Volume'], color='gray')
plt.title("Trading Volume")
plt.tight_layout()
plt.show()