30 useful pyton code from chatgpt
- Get link
- X
- Other Apps
tell me mostly used phyton code , problme names 10 scenarios problems and solution, to be executed in colab?
Here are ten common Python scenarios with corresponding solutions that you can execute in Google Colab. Each scenario is accompanied by a brief description and code examples.
1. Data Analysis with Pandas
Problem: Analyzing a CSV file
Solution:
pythonimport pandas as pd
# Load CSV file into a DataFrame
df = pd.read_csv('https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv')
# Display first few rows
print(df.head())
# Summary statistics
print(df.describe())
# Plotting the data
import matplotlib.pyplot as plt
df.plot(kind='line', x='Month', y='1958')
plt.show()
2. Machine Learning with Scikit-Learn
Problem: Training a simple model
Solution:
pythonfrom sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Load dataset
iris = load_iris()
X = iris.data
y = iris.target
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train a RandomForest model
model = RandomForestClassifier()
model.fit(X_train, y_train)
# Predict and evaluate
y_pred = model.predict(X_test)
print(f'Accuracy: {accuracy_score(y_test, y_pred)}')
3. Data Visualization with Matplotlib
Problem: Plotting a bar chart
Solution:
pythonimport matplotlib.pyplot as plt
# Data
categories = ['A', 'B', 'C']
values = [3, 7, 5]
# Plot
plt.bar(categories, values)
plt.xlabel('Category')
plt.ylabel('Values')
plt.title('Bar Chart Example')
plt.show()
4. Image Processing with OpenCV
Problem: Reading and displaying an image
Solution:
pythonimport cv2
from google.colab.patches import cv2_imshow
# Load an image
image = cv2.imread('https://upload.wikimedia.org/wikipedia/commons/2/2f/Google_2015_logo.svg')
# Display the image
cv2_imshow(image)
# Convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2_imshow(gray_image)
5. Natural Language Processing with NLTK
Problem: Tokenizing a text
Solution:
pythonimport nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
# Sample text
text = "Hello world! This is a test."
# Tokenize text
tokens = word_tokenize(text)
print(tokens)
6. Web Scraping with BeautifulSoup
Problem: Extracting data from a webpage
Solution:
pythonimport requests
from bs4 import BeautifulSoup
# URL to scrape
url = 'https://example.com'
# Make a request
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extract data
titles = soup.find_all('h1')
for title in titles:
print(title.get_text())
7. Interactive Widgets with ipywidgets
Problem: Creating interactive sliders
Solution:
pythonimport ipywidgets as widgets
from IPython.display import display
# Create a slider
slider = widgets.IntSlider(value=5, min=0, max=10, step=1, description='Slider:')
# Display the slider
display(slider)
# Link the slider value to a variable
slider_value = widgets.Label()
def update_label(change):
slider_value.value = f'Slider value: {change["new"]}'
slider.observe(update_label, names='value')
display(slider_value)
8. Time Series Analysis with statsmodels
Problem: Plotting ACF and PACF
Solution:
pythonimport numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
# Generate synthetic time series data
np.random.seed(42)
data = np.cumsum(np.random.randn(1000)) + 100
# Plot the data
plt.plot(data)
plt.title('Time Series Data')
plt.show()
# Plot ACF and PACF
fig, axes = plt.subplots(1, 2, figsize=(15, 5))
sm.graphics.tsa.plot_acf(data, lags=40, ax=axes[0])
sm.graphics.tsa.plot_pacf(data, lags=40, ax=axes[1])
plt.show()
9. Deep Learning with TensorFlow
Problem: Training a simple neural network
Solution:
pythonimport tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Load dataset
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Build a simple model
model = Sequential([
Dense(128, activation='relu', input_shape=(784,)),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(x_train.reshape(-1, 784), y_train, epochs=5)
# Evaluate the model
loss, accuracy = model.evaluate(x_test.reshape(-1, 784), y_test)
print(f'Accuracy: {accuracy}')
10. Financial Analysis with yfinance
Problem: Fetching and plotting stock data
Solution:
python!pip install yfinance
import yfinance as yf
import matplotlib.pyplot as plt
# Fetch stock data
stock = yf.Ticker("AAPL")
hist = stock.history(period="1y")
# Plot the data
hist['Close'].plot(title="AAPL Stock Price")
plt.show()
These scenarios cover a broad range of common tasks in Python, from data analysis and machine learning to web scraping and deep learning. Each example can be run in Google Colab, providing a great environment for experimentation and learning.
Here are 20 additional useful Python code examples across various categories, suitable for execution in Google Colab.
11. Sentiment Analysis with TextBlob
Problem: Analyzing the sentiment of a text
Solution:
python!pip install textblob
from textblob import TextBlob
# Sample text
text = "I love this product! It's amazing."
# Analyze sentiment
blob = TextBlob(text)
print(f'Sentiment: {blob.sentiment}')
12. Web Scraping with Scrapy
Problem: Scraping data from a website
Solution:
python!pip install scrapy
import scrapy
from scrapy.crawler import CrawlerProcess
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = ['http://quotes.toscrape.com/']
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').get(),
'author': quote.css('span small::text').get(),
}
process = CrawlerProcess()
process.crawl(QuotesSpider)
process.start()
13. Working with JSON
Problem: Parsing and creating JSON data
Solution:
pythonimport json
# Sample JSON data
data = '{"name": "John", "age": 30, "city": "New York"}'
# Parse JSON
parsed_data = json.loads(data)
print(parsed_data)
# Create JSON
new_data = {
"name": "Jane",
"age": 25,
"city": "Los Angeles"
}
json_data = json.dumps(new_data)
print(json_data)
14. File I/O Operations
Problem: Reading from and writing to files
Solution:
python# Writing to a file
with open('example.txt', 'w') as file:
file.write('Hello, World!')
# Reading from a file
with open('example.txt', 'r') as file:
content = file.read()
print(content)
15. Automation with Selenium
Problem: Automating web browser interactions
Solution:
python!pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# Set up the webdriver
driver = webdriver.Chrome()
# Open a website
driver.get('https://www.google.com')
# Find the search box and enter a query
search_box = driver.find_element(By.NAME, 'q')
search_box.send_keys('Python programming')
search_box.send_keys(Keys.RETURN)
# Close the browser
driver.quit()
16. Data Cleaning with Pandas
Problem: Cleaning a messy dataset
Solution:
pythonimport pandas as pd
# Sample data
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda', 'James'],
'Age': [28, 22, 35, None, 29],
'City': ['New York', 'Paris', 'Berlin', 'London', None]
}
# Create DataFrame
df = pd.DataFrame(data)
# Fill missing values
df['Age'].fillna(df['Age'].mean(), inplace=True)
df['City'].fillna('Unknown', inplace=True)
# Display cleaned data
print(df)
17. Regular Expressions
Problem: Extracting information using regex
Solution:
pythonimport re
# Sample text
text = "The rain in Spain falls mainly in the plain."
# Find all words that start with 'S'
matches = re.findall(r'\bS\w+', text)
print(matches)
18. Multithreading
Problem: Running multiple threads
Solution:
pythonimport threading
def print_numbers():
for i in range(1, 6):
print(i)
def print_letters():
for letter in ['A', 'B', 'C', 'D', 'E']:
print(letter)
# Create threads
thread1 = threading.Thread(target=print_numbers)
thread2 = threading.Thread(target=print_letters)
# Start threads
thread1.start()
thread2.start()
# Wait for threads to complete
thread1.join()
thread2.join()
19. HTTP Requests with Requests
Problem: Making an HTTP request
Solution:
pythonimport requests
# Make a GET request
response = requests.get('https://api.github.com')
# Display response content
print(response.json())
20. Image Classification with Keras
Problem: Building a simple image classifier
Solution:
pythonimport tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
# Load dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Normalize data
x_train, x_test = x_train / 255.0, x_test / 255.0
# Build model
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D((2, 2)),
Flatten(),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
# Compile model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train model
model.fit(x_train, y_train, epochs=5)
# Evaluate model
loss, accuracy = model.evaluate(x_test, y_test)
print(f'Accuracy: {accuracy}')
21. Exploratory Data Analysis with Seaborn
Problem: Visualizing data distributions
Solution:
pythonimport seaborn as sns
import matplotlib.pyplot as plt
# Load dataset
df = sns.load_dataset('iris')
# Plot pairplot
sns.pairplot(df, hue='species')
plt.show()
22. Working with Dates and Times
Problem: Manipulating dates and times
Solution:
pythonfrom datetime import datetime, timedelta
# Current date and time
now = datetime.now()
print(f'Now: {now}')
# Add 7 days to current date
future_date = now + timedelta(days=7)
print(f'Future date: {future_date}')
# Format date as string
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
print(f'Formatted date: {formatted_date}')
23. Using API with OAuth2
Problem: Making authenticated API requests
Solution:
python!pip install requests-oauthlib
from requests_oauthlib import OAuth2Session
# OAuth2 credentials
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
token_url = 'https://provider.com/oauth2/token'
# Fetch token
oauth = OAuth2Session(client_id, client_secret)
token = oauth.fetch_token(token_url=token_url, client_id=client_id, client_secret=client_secret)
# Make authenticated request
response = oauth.get('https://provider.com/api/resource')
print(response.json())
24. Creating Dashboards with Dash
Problem: Building an interactive dashboard
Solution:
python!pip install dash
import dash
import dash_core_components as dcc
import dash_html_components as html
# Initialize the app
app = dash.Dash(__name__)
# Define the layout
app.layout = html.Div(children=[
html.H1(children='Hello Dash'),
dcc.Graph(
id='example-graph',
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': 'NYC'},
],
'layout': {
'title': 'Dash Data Visualization'
}
}
)
])
# Run the app
if __name__ == '__main__':
app.run_server(debug=True)
25. Data Pipelines with Airflow
Problem: Creating a simple data pipeline
Solution:
python!pip install apache-airflow
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import datetime
# Define the DAG
dag = DAG('simple_dag', description='A simple DAG', schedule_interval='0 12 * * *', start_date=datetime(2021, 1, 1), catchup=False)
# Define the tasks
task1 = BashOperator(task_id='print_date', bash_command='date', dag=dag)
task2 = BashOperator(task_id='sleep', bash_command='sleep 5', dag=dag)
# Set task dependencies
task1 >> task2
26. Text-to-Speech Conversion
Problem: Converting text to speech
Solution:
python!pip install gtts
from gtts import gTTS
# Sample text
text = "Hello, how are you?"
# Convert text to speech
tts = gTTS(text)
tts.save('hello.mp3')
# Play the audio
import IPython.display as ipd
ipd.Audio('hello.mp3')
---------
27. Speech Recognition
Problem: Converting speech to text
Solution:
python!pip install SpeechRecognition
import speech_recognition as sr
# Initialize recognizer
recognizer = sr.Recognizer()
# Load audio file
audio_file = 'path_to_audio.wav'
# Recognize speech from audio file
with sr.AudioFile(audio_file) as source:
audio = recognizer.record(source)
# Convert speech to text
text = recognizer.recognize_google(audio)
print(f'Recognized Text: {text}')
28. Image Processing with OpenCV
Problem: Applying filters to an image
Solution:
python!pip install opencv-python
import cv2
from google.colab.patches import cv2_imshow
# Load an image
image = cv2.imread('path_to_image.jpg')
# Convert image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur
blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
# Display the image
cv2_imshow(blurred_image)
29. Time Series Analysis with Statsmodels
Problem: Analyzing time series data
Solution:
python!pip install statsmodels
import pandas as pd
import statsmodels.api as sm
# Load time series data
data = sm.datasets.co2.load_pandas().data
data = data.dropna()
# Fit ARIMA model
model = sm.tsa.ARIMA(data, order=(1, 1, 1))
results = model.fit()
# Print summary
print(results.summary())
30. Financial Analysis with yfinance
Problem: Fetching stock data
Solution:
python!pip install yfinance
import yfinance as yf
# Fetch stock data
ticker = yf.Ticker('AAPL')
stock_data = ticker.history(period='1mo')
# Display stock data
print(stock_data)
31. Real-time Data Plotting with Bokeh
Problem: Creating interactive plots
Solution:
python!pip install bokeh
from bokeh.plotting import figure, show, output_notebook
output_notebook()
# Create a plot
p = figure(title="Simple Line Plot", x_axis_label='X', y_axis_label='Y')
# Add a line renderer
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], legend_label="Temp.", line_width=2)
# Show the plot
show(p)
32. NLP with SpaCy
Problem: Named entity recognition
Solution:
python!pip install spacy
!python -m spacy download en_core_web_sm
import spacy
# Load SpaCy model
nlp = spacy.load('en_core_web_sm')
# Sample text
text = "Apple is looking at buying U.K. startup for $1 billion"
# Process text
doc = nlp(text)
# Print named entities
for ent in doc.ents:
print(ent.text, ent.label_)
33. Data Transformation with PySpark
Problem: Transforming large datasets
Solution:
python!pip install pyspark
from pyspark.sql import SparkSession
# Initialize Spark session
spark = SparkSession.builder.appName('example').getOrCreate()
# Create DataFrame
data = [("James", "Smith", "USA", "CA"), ("Michael", "Rose", "USA", "NY"), ("Robert", "Williams", "USA", "CA")]
columns = ["firstname", "lastname", "country", "state"]
df = spark.createDataFrame(data, columns)
# Perform transformation
df_filtered = df.filter(df.state == "CA")
# Show results
df_filtered.show()
34. Building a REST API with Flask
Problem: Creating a simple REST API
Solution:
python!pip install Flask
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def home():
return jsonify(message="Hello, World!")
if __name__ == '__main__':
app.run(debug=True)
35. Creating Graphs with NetworkX
Problem: Visualizing a network graph
Solution:
python!pip install networkx
import networkx as nx
import matplotlib.pyplot as plt
# Create a graph
G = nx.Graph()
# Add nodes and edges
G.add_edge('A', 'B')
G.add_edge('B', 'C')
G.add_edge('C', 'A')
# Draw the graph
nx.draw(G, with_labels=True)
plt.show()
36. Geospatial Analysis with Geopandas
Problem: Plotting geospatial data
Solution:
python!pip install geopandas
import geopandas as gpd
# Load geospatial data
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# Plot the data
world.plot()
plt.show()
37. Machine Learning with scikit-learn
Problem: Training a classification model
Solution:
pythonfrom sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Load dataset
iris = load_iris()
X = iris.data
y = iris.target
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Train model
model = RandomForestClassifier()
model.fit(X_train, y_train)
# Make predictions
predictions = model.predict(X_test)
# Evaluate model
accuracy = accuracy_score(y_test, predictions)
print(f'Accuracy: {accuracy}')
38. Web Scraping with Beautiful Soup
Problem: Extracting data from HTML
Solution:
python!pip install beautifulsoup4
from bs4 import BeautifulSoup
import requests
# Fetch web page
response = requests.get('http://example.com')
soup = BeautifulSoup(response.content, 'html.parser')
# Extract title
title = soup.title.string
print(f'Title: {title}')
39. Object Detection with TensorFlow
Problem: Detecting objects in images
Solution:
pythonimport tensorflow as tf
# Load pre-trained model
model = tf.keras.applications.MobileNetV2(weights='imagenet')
# Load and preprocess image
image = tf.keras.preprocessing.image.load_img('path_to_image.jpg', target_size=(224, 224))
input_array = tf.keras.preprocessing.image.img_to_array(image)
input_array = tf.expand_dims(input_array, axis=0)
input_array = tf.keras.applications.mobilenet_v2.preprocess_input(input_array)
# Make predictions
predictions = model.predict(input_array)
decoded_predictions = tf.keras.applications.mobilenet_v2.decode_predictions(predictions)
print(decoded_predictions)
40. Sentiment Analysis with VADER
Problem: Analyzing sentiment of a text
Solution:
python!pip install vaderSentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# Initialize analyzer
analyzer = SentimentIntensityAnalyzer()
# Sample text
text = "I love this product! It's amazing."
# Analyze sentiment
sentiment = analyzer.polarity_scores(text)
print(f'Sentiment: {sentiment}')
41. Data Aggregation with Dask
Problem: Handling large datasets
Solution:
python!pip install dask
import dask.dataframe as dd
# Load dataset
df = dd.read_csv('path_to_large_dataset.csv')
# Perform aggregation
result = df.groupby('column_name').sum().compute()
print(result)
42. Email Sending with smtplib
Problem: Sending an email
Solution:
pythonimport smtplib
from email.mime.text import MIMEText
# Define email content
msg = MIMEText('This is a test email')
msg['Subject'] = 'Test Email'
msg['From'] = 'your_email@example.com'
msg['To'] = 'recipient_email@example.com'
# Send email
with smtplib.SMTP('smtp.example.com') as server:
server.login('your_username', 'your_password')
server.sendmail(msg['From'], [msg['To']], msg.as_string())
43. Data Visualization with Plotly
Problem: Creating interactive plots
Solution:
python!pip install plotly
import plotly.express as px
import pandas as pd
# Load dataset
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [10, 11, 12, 13, 14],
'z': [100, 101, 102, 103, 104]
})
# Create 3D scatter plot
fig = px.scatter_3d(df, x='x', y='y', z='z')
fig.show()
44. Working with SQLite
Problem: Using SQLite database
Solution:
pythonimport sqlite3
# Connect to database
conn = sqlite3.connect('example.db')
# Create a cursor
cursor = conn.cursor()
# Create table
cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY
- Get link
- X
- Other Apps