[ad_1]
Using AI to plot graphs, send emails/messages, and do web scraping in a few seconds.
Recently ChatGPT was released. This is an AI chatbot developed by OpenAI that specializes in dialogue. Its goal is to make AI systems more natural to interact with, but it can also help you when writing code.
You can make ChatGPT your coding assistant or even go beyond that!
Copy trading bots from the top traders. Try it for FREE.
I’ve been playing with ChatGPT a bit over the past few days. Here are some tasks I gave ChatGPT ordered by difficulty (spoiler alert: I was amazed at the results)
If you want to plot a linear regression, you can simply tell ChatGPT:
plot a linear regression with Python using matplotlib
We can be very specific by telling ChatGPT the library it should use, otherwise, it’ll choose itself.
Here’s the Python code created by ChatGPT (with comments included!)
import matplotlib.pyplot as plt
import numpy as np
# Generate some random data
x = np.random.rand(100)
y = x + np.random.normal(0, 0.1, size=100)# Fit a linear regression to the data
m, b = np.polyfit(x, y, 1)# Plot the data and the fitted line
plt.plot(x, y, 'o')
plt.plot(x, m*x + b, '-')
plt.show()
And here’s the output.
This was an easy one, so let’s try something a bit more difficult.
I was curious to see if ChatGPT could help us with third-party libraries such as pywhatkit. This library allows us to send messages using Whatsapp.
send a message on Whatsapp using Python and pywhatkit
For the sake of this article, I didn’t go one step further by telling ChatGPT my phone number or the message I wanted to send (I did something like this for task #3 though).
Here’s the Python code created by ChatGPT.
import pywhatkit
# Set the time in 24-hour format (HH:MM:SS) when you want the message to be sent
time = "HH:MM:SS"
# Set the phone number that you want to send the message to
phone_number = "1234567890"
# Set the message that you want to send
message = "Your message goes here"
# Use the sendwhatmsg method to send the message at the specified time
pywhatkit.sendwhatmsg(time, phone_number, message)
In addition to the code generated by ChatGPT, you have to have a WhatsApp session opened in your browser and have pywhatkit installed (more info here)
There are hundreds of code snippets on the internet that show you how to send an email with Python. You can go one step further with ChatGPT:
send an email from “email_1” to “email_2” with the subject “Email sent by ChatGPT” and the content “ChatGPT rocks!” using Python
Here’s the code:
import smtplib
# create an SMTP object
smtp_obj = smtplib.SMTP('smtp.gmail.com', 587)# start TLS encryption
smtp_obj.starttls()# login to the email account
smtp_obj.login('[email protected]', 'YOUR_PASSWORD')# construct the email message
message = "Subject: Email sent by ChatGPT\n\nChatGPT rocks!"# send the email
smtp_obj.sendmail('[email protected]', '[email protected]', message)# close the SMTP connection
smtp_obj.quit()
After typing the code, ChatGPT tells us that we should introduce our password in the “YOUR_PASSWORD”
placeholder.
Something that ChatGPT didn’t tell us this time (it does sometimes) was that we have to turn on 2-step verification to get a 16-character password in case we’re using Gmail.
Anyway, ChatGPT did an amazing job. After typing my password, I got the email:
Probably the most challenging task I gave ChatGPT was to scrape a website. As you might know, websites have different HTML, so the steps to scrape websites change from site to site.
ChatGPT did a great job scraping the name of books and prices of a site called books.toscrape.com
web scrape https://books.toscrape.com/ using Python and beautiful soup
Here’s the code.
import requests
from bs4 import BeautifulSoup
# Make a GET request to the website
response = requests.get("https://books.toscrape.com/")# Parse the HTML content of the website
soup = BeautifulSoup(response.text, "html.parser")# Extract the data you want using Beautiful Soup's various methods and attributes
titles = soup.find_all("h3")
prices = soup.find_all(class_="price_color")# Save the extracted data to a file or database, or use it for some other purpose
with open("book_data.txt", "w") as file:
for title, price in zip(titles, prices):
file.write(f"{title.text}: {price.text}\n")
And here’s the data extracted.
Extracting data without writing code myself … amazing!
For those into Excel automation, I also made ChatGPT generate an excel report.
I couldn’t come up with a good idea to make ChatGPT help me with automating Excel reports, so I only tried this.
generate an Excel report with Python
Here’s the code generated.
# Import the `openpyxl` library
import openpyxl
# Create a new Excel workbook
workbook = openpyxl.Workbook()
# Get the active sheet in the workbook (which will be the only sheet at this point)
sheet = workbook.active
# Add some data to the sheet
sheet["A1"] = "Product"
sheet["B1"] = "Sales"
# Add some more data to the sheet
products = ["Product A", "Product B", "Product C"]
sales = [100, 200, 300]
for i in range(len(products)):
sheet.cell(row=i+2, column=1).value = products[i]
sheet.cell(row=i+2, column=2).value = sales[i]
# Save the workbook to a file
workbook.save("sales_report.xlsx")
And here’s the output:
As you can see, I only got a simple template, but I believe that you only need to give ChatGPT the right command to get impressive results.
Now it’s your time to play with ChatGPT!
Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing
[ad_2]
Source link