Another week has passed, and as I've started to adapt to my new responsibilities at work and the onset of summer, I find myself allocating more time to my Machine Learning project and data analysis case study than to my coursework. I can only promise consistent, incremental progress each week, and I'm eager to eventually showcase the effort I've been putting into building my portfolio. But for now, let's delve into this week's update, which includes a discussion of two projects and a somewhat advanced SQL concept: Transaction Control.
LangChain:
After a series of trials and errors with the simple sequential chain function, I've created a rudimentary rap battle generator featuring two historical figures who share a birthday (default is always today, feel free to change it to whatever date you prefer). I encourage you to visit my GitHub page and try it for yourself.
Upon execution, the prompt returns the two chosen people born on this day in history, along with brief biographies. It then generates a rap verse for each individual, including a verse boasting about their own achievements and another dissing their opponent:
# Two famous persons who were born on July 9th in history are:
"""
1. Tom Hanks: The renowned American actor and filmmaker,
known for his performances in movies such as "Forrest Gump,"
"Cast Away," and "Saving Private Ryan," was born on July 9, 1956.
2. Courtney Love: The American musician, singer, and actress,
best known as the frontwoman of the alternative rock band Hole
and for her marriage to Kurt Cobain, was born on July 9, 1964.
"""
"""
Tom Hanks:
I'm Tom Hanks, gifted on the screen,
Gump, Wilson, Ryan, I'm movin' with the dream.
From comedy to drama, I can't be beat,
Legendary acting, yeah, that's my feat.
"""
"""
Courtney Love:
I'm Courtney Love, rockin' the stage,
Hole's frontwoman, unleashin' my rage.
Married to Cobain, a love so pure,
Musical rebel, my voice will endure.
"""
"""
Tom Hanks dissing Courtney Love:
Courtney Love, your wild ways ain't real,
I'm Tom Hanks, my talent's the deal.
Acting accolades, from Hollywood to the core,
You may rock, but I win this war.
"""
"""
Courtney Love dissing Tom Hanks:
Tom Hanks, you're sweet like a box of chocolates,
But I'm Courtney Love, a wild fire that won't quit.
Your movies may shine, but it's all just pretend,
I'll scream through the mic, see how my fire transcends.
"""
At the moment, I'm working on an enhanced version that utilizes the sequential chain function as opposed to the simple sequential chain. However, I haven't quite figured out the details yet, so it's something to anticipate in the near future.
Data Analysis Case Study - Bellabeat:
This project was initially meant to be the capstone project for the Google Data Analytics course. However, I've chosen to expand its scope and transform it into a grander initiative. The ultimate goal is to create a personal web application for my CS50 final project. This application will host my entire portfolio (which is currently under construction, maybe 3% at this point), my resume and contacts, and this case study. I plan to elevate the case study by creating dashboards in both Tableau and PowerBI and applying machine learning analysis. Since I'm exploring uncharted territory on many fronts, I've projected a completion date of late August or September, as mentioned in my LinkedIn post previously. At this point, the volume of work required to meet my personal standards for a serious project feels somewhat overwhelming.
If you've taken the Google Data Analytics course or read my previous blogs, you should be familiar with the six-step framework of data analysis that the course proposes:
Step | Task | Completion |
ASK | Business task summary | Done |
PREPARE | Data collection and sourcing | Done |
PROCESS | Data cleaning and documentation | In progress |
ANALYZE | Organize data and make discoveries | - |
SHARE | Visualizations | - |
ACT | Next step and reflection | - |
I've chosen this case study over the other (city bike rental or something simialr to that) mainly because I've already worked with that dataset multiple times throughout the course. I thought it best to start anew and engage with a subject matter more detached from my personal experiences. In this case, it's a study for Bellabeat, a company focused on women-centric health products. However, it's still early in the project, and I don't have much to showcase yet. Therefore, this may be the last time I mention it until at least I'm working on the visualizations.
Transaction control in SQL:
Transaction Control in SQL refers to the way that SQL handles transactions to ensure data integrity. Transactions are a key part of ensuring data integrity and are used to ensure that databases remain in a consistent state even if an operation fails.
A transaction is a single unit of work. If a transaction is successful, all data modifications made during the transaction are committed and become a permanent part of the database. If a transaction encounters errors and must be stopped, all data modifications are erased.
Let's consider an online banking scenario where a user wants to transfer money from one account to another. This transaction involves two steps:
Deducting an amount from the sender's account.
Adding the same amount to the receiver's account.
BEGIN TRY
BEGIN TRANSACTION transfer; -- Initiates the transaction called transfer.
/*
Table name is `accounts` with two columns:
1. account_number
2. balance
*/
UPDATE accounts
SET balance = balance - 100 -- Deducts amount from sender's account
WHERE account_number = '1000101';
-- If the balance is negative, rollback the transaction.
IF ((SELECT balance FROM accounts WHERE account_number = '1000101') < 0)
BEGIN
ROLLBACK TRANSACTION transfer;
THROW 50000, 'Insufficient funds', 1; -- Raises an exception.
END;
SAVE TRANSACTION after_deduction; -- Sets up a savepoint
UPDATE accounts
SET balance = balance + 100 -- Adds amount to receiver's account
WHERE account_number = '1000102';
/*
@@ERROR is a system function that returns the error code
from the last statement.
When @@ERROR = 0, it means there's no error
*/
IF @@ERROR != 0
BEGIN
ROLLBACK TRANSACTION after_deduction; -- Rollbacks to the savepoint.
THROW; -- Re-throw the error.
END;
COMMIT; -- If both updates were successful, commit the transaction.
END TRY
BEGIN CATCH
/*
This is a check to see if there are any active transactions.
If @@TRANCOUNT is greater than 0, that means there is at least
one active transaction.
*/
IF @@TRANCOUNT > 0
ROLLBACK;
-- Re-throw the error.
THROW;
END CATCH;
This example illustrates a simplified banking scenario where a transaction involves transferring funds from one account to another. In reality, transactions may include additional steps, such as recording transaction details in a separate table, handling transaction fees, or implementing other business-related steps. Furthermore, error-checking logic could be either managed by the application using the database or controlled by triggers or stored procedures within the database itself.
Key to understanding the transaction mechanism are the ACID properties: Atomicity, Consistency, Isolation, and Durability.
Atomicity: In this example, the transaction is treated as a single, indivisible unit of work, which either completes entirely or not at all. The importance of Atomicity is demonstrated in our example by ensuring that money isn't lost or created during the transaction - either both the debit from one account and the credit to another occur, or neither occurs. If an error arises at any point during the transaction, the ROLLBACK command is invoked to undo the entire transaction or up to a save point, offering more granular control.
Consistency: The transaction ensures that the database transitions from one valid state to another. If a transaction were to violate the database's consistency rules, it would be rolled back.
Isolation: Isolation ensures that concurrent execution of transactions results in a database state that would have been obtained if transactions were executed sequentially. This provides every transaction with a "snapshot" of the database to work against, isolating it from the execution of other transactions.
Durability: Once a transaction has been committed, it will remain so, even in the face of power loss, crashes, or errors. In our example, the COMMIT command makes changes permanent and ensures that they survive subsequent system failures.
Thus, by using a transaction, we maintain the integrity of the data, adhering to the ACID properties fundamental to reliable data operations.