Skip to content

Instantly share code, notes, and snippets.

@harusametime
Created February 3, 2021 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harusametime/bd4072c54a86ab215651907db6f5b24e to your computer and use it in GitHub Desktop.
Save harusametime/bd4072c54a86ab215651907db6f5b24e to your computer and use it in GitHub Desktop.
import boto3
redshift = boto3.client('redshift')
credentials = redshift.get_cluster_credentials(
DbUser='demouser',
DbName='dev',
ClusterIdentifier='redshiftml',
DurationSeconds=3600,
AutoCreate=False
)
tmp_db_user = credentials['DbUser']
tmp_db_password = credentials['DbPassword']
import psycopg2
conn = psycopg2.connect(
host="<redshift_endpoint_name>",
port=5439,
dbname='dev',
user=tmp_db_user,
password=tmp_db_password
)
sql = """
CREATE MODEL demo_ml.customer_churn_model
FROM (SELECT state,
area_code,
total_charge/account_length AS average_daily_spend,
cust_serv_calls/account_length AS average_daily_cases,
churn
FROM demo_ml.customer_activity
WHERE record_date < '2020-01-01'
)
TARGET churn
FUNCTION predict_customer_churn
IAM_ROLE 'arn:aws:iam::373011628954:role/RedshiftML'
SETTINGS (
S3_BUCKET 'redshiftml-373011628954',
MAX_RUNTIME 1800
)
;
END;
"""
with conn.cursor() as cur:
cur.execute(sql)
sql = """
SHOW MODEL demo_ml.customer_churn_model;
"""
with conn.cursor() as cur:
cur.execute(sql)
result = cur.fetchall()
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment