Tuesday, July 2, 2024

Install Polars DataFrame Library and with Local LLMs Using Ollama

 This video installs Polars and demonstrates how to integrate with Ollama local models to do data analysis.


Code:

conda create -n polarsdf python=3.11 -y && conda activate polarsdf

conda install jupyter -y

jupyter notebook

!pip install polars pyarrow pandas ollama

import polars as pl
from datetime import datetime

df = pl.DataFrame(
    {
        "date": [
            datetime(2024, 7, 1),
            datetime(2024, 7, 2),
            datetime(2024, 7, 3),
            datetime(2024, 7, 4),
            datetime(2024, 7, 5),
        ],
        "revenue": [1000.0, 1200.0, 1100.0, 1300.0, 1400.0],
        "expenses": [500, 600, 550, 650, 700]
    }
)

print(df)

df.write_csv("dummyfinance.csv")
df_csv = pl.read_csv("dummyfinance.csv")
print(df_csv)

df.select(pl.col("*"))

df.select(pl.col("revenue", "date"))

df.filter(
    pl.col("date").is_between(datetime(2024, 7, 2), datetime(2024, 7, 4)),
)


import ollama


prompt = "Analyze the financial data: "

for row in df.itertuples(index=True):
    prompt += f"Date: {row.date.strftime('%Y-%m-%d')}, Revenue: ${row.revenue}, Expenses: ${row.expenses}; "

prompt += "Predict the Revenue and Expenses for the next date."

response = ollama.generate(model='llama3', prompt=prompt)

predicted_revenue = response['response'].split('Revenue: $')[-1].split('\n')[0]
predicted_expenses = response['response'].split('Expenses: $')[-1].split('\n')[0]

print(predicted_revenue)
print(predicted_expenses)

No comments: