Friday, March 31, 2023
No Result
View All Result
Get the latest A.I News on A.I. Pulses
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing
No Result
View All Result
Get the latest A.I News on A.I. Pulses
No Result
View All Result

ChatGPT as a Python Programming Assistant

January 21, 2023
141 9
Home Data science
Share on FacebookShare on Twitter


Unique picture by Sigmund on Unsplash | Edits by writer
 

ChatGPT from OpenAI is… you already know. You’ve got learn sufficient about it, and introductions are now not wanted. If you happen to occur to not know what ChatGPT is, take a look at this primary, after which come on again to proceed.

Conversations with ChatGPT will be entertaining, even enlightening. However let’s take a look at how useful it will probably really be.

Let’s examine what ChatGPT can do in the case of writing some code from specs that we offer. We are going to begin easy and construct from there.

 

 I figured {that a} good place to start out was a knowledge processing script of some kind. Since I intend to have ChatGPT write some machine studying scripts afterwards utilizing a wide range of Python libraries, this appeared like an inexpensive place to start out.

 

The Purpose

 First, I wished to make some makes an attempt at loading a dataset; making certain all dataset values are numeric values, or changing them to be such; checking for lacking values; and splitting the dataset into practice and check units.

 

The Immediate

 Write a Python script that does the next:- masses a dataset from csv on disk to Pandas DataFrame- ensures that dataset is fully numeric; converts string values to numeric and tracks this conversion in a dictionary to reference later- checks for lacking values within the DataFrame and imputes lacking values for any report that has one lacking worth; fully drops a report that has multiple lacking value- splits the dataset into check and coaching units

 

The Response

 

import pandas as pd

# Load dataset from csv to DataFrame
df = pd.read_csv(‘dataset.csv’)

# Initialize dictionary to trace string to numeric conversions
conversions = {}

# Convert string values to numeric and monitor conversions in dictionary
for col in df.columns:
if df[col].dtype == object:
conversions[col] = {val: i for i, val in enumerate(df[col].distinctive())}
df[col] = df[col].map(conversions[col])

# Test for lacking values
missing_values = df.isna().sum()

# Impute lacking values for information with one lacking worth
for col in missing_values[missing_values == 1].index:
df[col].fillna(df[col].median(), inplace=True)

# Drop information with multiple lacking worth
df.dropna(thresh=len(df.columns) – 1, inplace=True)

# Cut up dataset into check and coaching units
from sklearn.model_selection import train_test_split
train_df, test_df = train_test_split(df, test_size=0.2)

 

Observe: the script masses the dataset from csv file identify “dataset.csv” and splits the dataframe into check and coaching units with 80/20 ratio. If you wish to use totally different file identify or ratio, change the trail/ratio accordingly.

 

The Evaluation

 ChatGPT produced a helpful script. It selected a dataset identify, in addition to a practice/check cut up share, and famous each of those factors and change them if desired. General, mission completed.

 

 

The Purpose

 Now that we have now a script that does one thing, let’s refactor it a bit. A cornerstone of excellent coding is using capabilities to logically manage you code, facilitate reuse, and guarantee you do not repeat your self.

 

The Immediate

 Rewrite the above script to make use of a sequence of capabilities that make sense

 

The Response

 

import pandas as pd
from sklearn.model_selection import train_test_split

def load_and_convert_data(file_path):
# Load dataset from csv to DataFrame
df = pd.read_csv(file_path)

# Initialize dictionary to trace string to numeric conversions
conversions = {}

# Convert string values to numeric and monitor conversions in dictionary
for col in df.columns:
if df[col].dtype == object:
conversions[col] = {val: i for i, val in enumerate(df[col].distinctive())}
df[col] = df[col].map(conversions[col])

return df, conversions

def handle_missing_values(df):
# Test for lacking values
missing_values = df.isna().sum()

# Impute lacking values for information with one lacking worth
for col in missing_values[missing_values == 1].index:
df[col].fillna(df[col].median(), inplace=True)

# Drop information with multiple lacking worth
df.dropna(thresh=len(df.columns) – 1, inplace=True)

return df

def split_data(df, test_size):
return train_test_split(df, test_size=test_size)

# Use the capabilities
file_path = “dataset.csv”
df, conversions = load_and_convert_data(file_path)
df = handle_missing_values(df)
train_df, test_df = split_data(df, 0.2)

 

Right here, I’ve used three capabilities:

1. load_and_convert_data(file_path) masses the csv dataset and convert the string values to numeric.2. handle_missing_values(df) handles the lacking values within the dataset.3. split_data(df, test_size) splits the dataset into check and coaching units.

The primary script makes use of these capabilities with the intention to load, convert and clear the dataset, then cut up it into check and practice units.

The trail of the dataset and the check ratio are inputs for these capabilities.

 

The Evaluation

 If you happen to take a look at the primary script output by ChatGPT, you possibly can in all probability see some somewhat apparent strains you’d draw so far as what performance can be separated into capabilities. I definitely did. ChatGPT did precisely what I anticipated it to do, on this regard. Once more, a reliable script has been generated.

 

 

The Purpose

 OK, our script seems to be a bit higher now that it is utilizing some capabilities for group. Subsequent, let’s prolong the script barely with the intention to make it in order that the person can specify a couple of parameters whereas working the script.

 

The Immediate

 Rewrite the above program to solicit the next from the command line within the type of arguments, and use them in this system:- the trail/identify of the dataset csv file- the practice/check cut up size- the brink for the variety of lacking values per report to drop after which to drop whole report, as an alternative of imputing the lacking values

 

The Response

 

import pandas as pd
import argparse
from sklearn.model_selection import train_test_split

def load_and_convert_data(file_path):
# Load dataset from csv to DataFrame
df = pd.read_csv(file_path)

# Initialize dictionary to trace string to numeric conversions
conversions = {}

# Convert string values to numeric and monitor conversions in dictionary
for col in df.columns:
if df[col].dtype == object:
conversions[col] = {val: i for i, val in enumerate(df[col].distinctive())}
df[col] = df[col].map(conversions[col])

return df, conversions

def handle_missing_values(df, threshold):
# Test for lacking values
missing_values = df.isna().sum()

# Impute lacking values for information with one lacking worth
for col in missing_values[missing_values == 1].index:
df[col].fillna(df[col].median(), inplace=True)

# Drop information with greater than threshold lacking worth
df.dropna(thresh=len(df.columns) – threshold, inplace=True)

return df

def split_data(df, test_size):
return train_test_split(df, test_size=test_size)

if __name__ == ‘__main__’:
parser = argparse.ArgumentParser()
parser.add_argument(“file_path”, assist=”path/identify of the dataset csv file”)
parser.add_argument(“test_size”, assist=”practice/check cut up dimension”, sort=float)
parser.add_argument(“threshold”, assist=”threshold for the variety of lacking values per report”, sort=int)
args = parser.parse_args()

# Use the capabilities
df, conversions = load_and_convert_data(args.file_path)
df = handle_missing_values(df, args.threshold)
train_df, test_df = split_data(df, args.test_size)

 

On this model, I’ve used the argparse module to take the inputs of file path, check/practice cut up dimension, and threshold for the variety of lacking values per report as command-line arguments. The person must run the script with the right arguments, for instance:

 python script.py dataset.csv 0.2 1 

It will run the script utilizing the “dataset.csv” file, a test_size of 0.2 and a threshold of 1 for the lacking values.

The primary script makes use of these arguments within the capabilities to load, convert and clear the dataset, then cut up it into check and practice units.

It is also value mentioning that, when working script, the script ought to be run from command line, somewhat than working it from an IDE.

 

The Evaluation

 Actually, not a lot evaluation is required right here, provided that ChatGPT defined what it did fairly nicely. I will really observe one thing right here that I had beforehand not, and provides ChatGPT a pat on the again for its commenting, which is expressive and concise.

 

 

The Purpose

 Now to illustrate we wished to make this code “manufacturing” prepared (humor me). Let’s get ChatGPT to wrap these capabilities in a Streamlit app.

 

The Immediate

 Rewrite the above program as a Streamlit app, permitting for the person to supply the identical arguments as within the command line args above

 

The Response

 

import pandas as pd
import streamlit as st
from sklearn.model_selection import train_test_split

def load_and_convert_data(file_path):
# Load dataset from csv to DataFrame
df = pd.read_csv(file_path)

# Initialize dictionary to trace string to numeric conversions
conversions = {}

# Convert string values to numeric and monitor conversions in dictionary
for col in df.columns:
if df[col].dtype == object:
conversions[col] = {val: i for i, val in enumerate(df[col].distinctive())}
df[col] = df[col].map(conversions[col])

return df, conversions

def handle_missing_values(df, threshold):
# Test for lacking values
missing_values = df.isna().sum()

# Impute lacking values for information with one lacking worth
for col in missing_values[missing_values == 1].index:
df[col].fillna(df[col].median(), inplace=True)

# Drop information with greater than threshold lacking worth
df.dropna(thresh=len(df.columns) – threshold, inplace=True)

return df

def split_data(df, test_size):
return train_test_split(df, test_size=test_size)

def primary():
st.set_page_config(page_title=”Knowledge Preprocessing”, page_icon=”:guardsman:”, structure=”large”)
st.title(“Knowledge Preprocessing”)

file_path = st.text_input(“Enter the trail/identify of the dataset csv file: “)
test_size = st.number_input(“Enter the practice/check cut up dimension (decimal between 0 and 1): “, step=0.01, worth=0.2)
threshold = st.number_input(“Enter the brink for the variety of lacking values per report: “, step=1, worth=1)

if st.button(“Course of Knowledge”):
df, conversions = load_and_convert_data(file_path)
df = handle_missing_values(df, threshold)
train_df, test_df = split_data(df, test_size)
st.success(“Knowledge preprocessing accomplished!”)

if __name__ == ‘__main__’:
primary()

 

This model is a Streamlit app that enables the person to supply the identical arguments as command-line arguments. It makes use of the Streamlit library to create an interactive net app that enables the person to enter the file path, check/practice cut up dimension, and threshold for the variety of lacking values per report. The person can then click on the “Course of Knowledge” button to run the script and preprocess the information. It makes use of the identical capabilities because the earlier model of the script to load, convert, and clear the dataset, then cut up it into check and practice units.

To run the script, that you must have Streamlit put in, you possibly can run the command pip set up streamlit in your terminal. As soon as put in, run the script with the command streamlit run script.py.

 

The Evaluation

 It did what it was speculated to do. ChatGPT took our functioning… capabilities and constructed a easy Streamlit net app with them.

 

Screenshot of the Streamlit web app created with the script generated by ChatGPTThe Streamlit net app created with the script generated by ChatGPT

 

 ChatGPT did a superbly competent job of making, refactoring, extending, and including a wrapper round a easy information preprocessing Python script we wished it to write down for us, from the specs that we supplied. Whereas it wasn’t essentially the most advanced of packages by a protracted shot, it was a step within the path of getting ChatGPT be genuinely helpful by appearing as a programming assistant. The outcomes hit the mark at every step.

Subsequent time, we’ll construct a much less trivial program with some elevated complexity and extra necessities. I am excited to see how nicely ChatGPT performs beneath strain. Hopefully you’re too.

  Matthew Mayo (@mattmayo13) is a Knowledge Scientist and the Editor-in-Chief of KDnuggets, the seminal on-line Knowledge Science and Machine Studying useful resource. His pursuits lie in pure language processing, algorithm design and optimization, unsupervised studying, neural networks, and automatic approaches to machine studying. Matthew holds a Grasp’s diploma in laptop science and a graduate diploma in information mining. He will be reached at editor1 at kdnuggets[dot]com. 



Source link

Tags: AssistantChatGPTProgrammingPython
Next Post

Scaling Information Administration By Apache Gobblin

10 Industries That Can Profit Vastly from Robotic Palletizing

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent News

Interpretowalność modeli klasy AI/ML na platformie SAS Viya

March 31, 2023

Can a Robotic’s Look Affect Its Effectiveness as a Office Wellbeing Coach?

March 31, 2023

Robotic Speak Episode 43 – Maitreyee Wairagkar

March 31, 2023

What Is Abstraction In Pc Science?

March 31, 2023

How Has Synthetic Intelligence Helped App Growth?

March 31, 2023

Leverage GPT to research your customized paperwork

March 31, 2023

Categories

  • A.I News
  • A.I. Startups
  • Computer Vision
  • Data science
  • Machine learning
  • Natural Language Processing
  • Robotics
A.I. Pulses

Get The Latest A.I. News on A.I.Pulses.com.
Machine learning, Computer Vision, A.I. Startups, Robotics News and more.

Categories

  • A.I News
  • A.I. Startups
  • Computer Vision
  • Data science
  • Machine learning
  • Natural Language Processing
  • Robotics
No Result
View All Result

Recent News

  • Interpretowalność modeli klasy AI/ML na platformie SAS Viya
  • Can a Robotic’s Look Affect Its Effectiveness as a Office Wellbeing Coach?
  • Robotic Speak Episode 43 – Maitreyee Wairagkar
  • Home
  • DMCA
  • Disclaimer
  • Cookie Privacy Policy
  • Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2022 A.I. Pulses.
A.I. Pulses is not responsible for the content of external sites.

No Result
View All Result
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing

Copyright © 2022 A.I. Pulses.
A.I. Pulses is not responsible for the content of external sites.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In