5.10. Exemple Jupyter AI avec ollama#

%load_ext jupyter_ai_magics
#%config AiMagics.default_language_model = "ollama:mistral"
%config AiMagics.default_language_model = "ollama:codellama"

%%ai -f math
Generate the 2D heat equation in LaTeX surrounded by `$$`

$\displaystyle The 2D heat equation is given by:

\[\frac{\partial u}{\partial t} = \alpha \nabla^2 u\]

where \(u=u(x,y,t)\) is the temperature at a point \((x,y)\) and time \(t\), \(\alpha\) is the thermal diffusivity, and \(\nabla^2\) is the Laplacian operator.$

%%ai -f code
Load the "titanic.csv" file. Do a univariate analysis.
Write code to show the relevent plots. 
Use a single figure to make te plots using subplots.

AI generated code inserted below ⬇️

import pandas as pd
from matplotlib import pyplot as plt

# Load the data from titanic.csv file
df = pd.read_csv('titanic.csv')

# Perform univariate analysis on the survival column
survival_counts = df['survived'].value_counts()
survival_probs = survival_counts / len(df)
survival_probs.plot(kind='bar', rot=0, figsize=(10,6))
plt.xlabel('Survival')
plt.ylabel('Frequency')
plt.title('Survival Probability')
plt.show()

# Perform univariate analysis on the pclass column
pclass_counts = df['pclass'].value_counts()
pclass_probs = pclass_counts / len(df)
pclass_probs.plot(kind='bar', rot=0, figsize=(10,6))
plt.xlabel('Pclass')
plt.ylabel('Frequency')
plt.title('Pclass Probability')
plt.show()

# Perform univariate analysis on the sex column
sex_counts = df['sex'].value_counts()
sex_probs = sex_counts / len(df)
sex_probs.plot(kind='bar', rot=0, figsize=(10,6))
plt.xlabel('Sex')
plt.ylabel('Frequency')
plt.title('Sex Probability')
plt.show()
---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

File /var/lib/jupyterhub/venvs/py3.9/lib/python3.9/site-packages/pandas/core/indexes/base.py:3621, in Index.get_loc(self, key, method, tolerance)
   3620 try:
-> 3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:


File /var/lib/jupyterhub/venvs/py3.9/lib/python3.9/site-packages/pandas/_libs/index.pyx:136, in pandas._libs.index.IndexEngine.get_loc()


File /var/lib/jupyterhub/venvs/py3.9/lib/python3.9/site-packages/pandas/_libs/index.pyx:163, in pandas._libs.index.IndexEngine.get_loc()


File pandas/_libs/hashtable_class_helper.pxi:5198, in pandas._libs.hashtable.PyObjectHashTable.get_item()


File pandas/_libs/hashtable_class_helper.pxi:5206, in pandas._libs.hashtable.PyObjectHashTable.get_item()


KeyError: 'survived'


The above exception was the direct cause of the following exception:


KeyError                                  Traceback (most recent call last)

Cell In[5], line 8
      5 df = pd.read_csv('titanic.csv')
      7 # Perform univariate analysis on the survival column
----> 8 survival_counts = df['survived'].value_counts()
      9 survival_probs = survival_counts / len(df)
     10 survival_probs.plot(kind='bar', rot=0, figsize=(10,6))


File /var/lib/jupyterhub/venvs/py3.9/lib/python3.9/site-packages/pandas/core/frame.py:3505, in DataFrame.__getitem__(self, key)
   3503 if self.columns.nlevels > 1:
   3504     return self._getitem_multilevel(key)
-> 3505 indexer = self.columns.get_loc(key)
   3506 if is_integer(indexer):
   3507     indexer = [indexer]


File /var/lib/jupyterhub/venvs/py3.9/lib/python3.9/site-packages/pandas/core/indexes/base.py:3623, in Index.get_loc(self, key, method, tolerance)
   3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:
-> 3623     raise KeyError(key) from err
   3624 except TypeError:
   3625     # If we have a listlike key, _check_indexing_error will raise
   3626     #  InvalidIndexError. Otherwise we fall through and re-raise
   3627     #  the TypeError.
   3628     self._check_indexing_error(key)


KeyError: 'survived'

5.10.1. Gestion de l’erreur avec le chat Jupyternaut#

Le programme fournit contient des erreurs. On utilise alors le chat Jupyternaut pour avoir des indication en tapant la commande suivante dans la section chat à gauche:

/fix

La réponse est la suivante:

The error message is indicating that the column survived does not exist in the DataFrame. This is likely because the CSV file you are loading does not contain a header row with the column names, or the column name is misspelled.

To fix this issue, you can try adding the following line before reading the CSV file:

df = pd.read_csv("your_file.csv", header=0)

This will specify that the first row of the CSV file contains the column names. Alternatively, you can manually add the column names to the DataFrame using the columns parameter in pd.read_csv:

df = pd.read_csv("your_file.csv", columns=["survived", "name", ...])

Make sure that the list of column names matches the actual column names in your CSV file.

5.10.2. Correction de l’erreur#

Avec les indications précédente on corrige l’erreur qui provient du fait que le nom des colonnes dans le fichier commence par une minuscule et qu’ollama convertit tout en minuscule. Le programme corrigé est donné ci-dessous.

import pandas as pd
from matplotlib import pyplot as plt

# Load the data from titanic.csv file
df = pd.read_csv('titanic.csv')

# Perform univariate analysis on the survival column
survival_counts = df['Survived'].value_counts()
survival_probs = survival_counts / len(df)
survival_probs.plot(kind='bar', rot=0, figsize=(10,6))
plt.xlabel('Survival')
plt.ylabel('Frequency')
plt.title('Survival Probability')
plt.show()

# Perform univariate analysis on the pclass column
pclass_counts = df['Pclass'].value_counts()
pclass_probs = pclass_counts / len(df)
pclass_probs.plot(kind='bar', rot=0, figsize=(10,6))
plt.xlabel('Pclass')
plt.ylabel('Frequency')
plt.title('Pclass Probability')
plt.show()

# Perform univariate analysis on the sex column
sex_counts = df['Sex'].value_counts()
sex_probs = sex_counts / len(df)
sex_probs.plot(kind='bar', rot=0, figsize=(10,6))
plt.xlabel('Sex')
plt.ylabel('Frequency')
plt.title('Sex Probability')
plt.show()

png

png

png

5.11. The END#

%ai list

Provider

Environment variable

Set?

Models

ai21

AI21_API_KEY

  • ai21:j1-large
  • ai21:j1-grande
  • ai21:j1-jumbo
  • ai21:j1-grande-instruct
  • ai21:j2-large
  • ai21:j2-grande
  • ai21:j2-jumbo
  • ai21:j2-grande-instruct
  • ai21:j2-jumbo-instruct

gpt4all

Not applicable.

N/A

  • gpt4all:ggml-gpt4all-j-v1.2-jazzy
  • gpt4all:ggml-gpt4all-j-v1.3-groovy
  • gpt4all:ggml-gpt4all-l13b-snoozy
  • gpt4all:mistral-7b-openorca.Q4_0
  • gpt4all:mistral-7b-instruct-v0.1.Q4_0
  • gpt4all:gpt4all-falcon-q4_0
  • gpt4all:wizardlm-13b-v1.2.Q4_0
  • gpt4all:nous-hermes-llama2-13b.Q4_0
  • gpt4all:gpt4all-13b-snoozy-q4_0
  • gpt4all:mpt-7b-chat-merges-q4_0
  • gpt4all:orca-mini-3b-gguf2-q4_0
  • gpt4all:starcoder-q4_0
  • gpt4all:rift-coder-v0-7b-q4_0
  • gpt4all:em_german_mistral_v01.Q4_0

huggingface_hub

HUGGINGFACEHUB_API_TOKEN

See https://huggingface.co/models for a list of models. Pass a model’s repository ID as the model ID; for example, huggingface_hub:ExampleOwner/example-model.

mistralai

MISTRAL_API_KEY

  • mistralai:open-mistral-7b
  • mistralai:open-mixtral-8x7b
  • mistralai:open-mixtral-8x22b
  • mistralai:mistral-small-latest
  • mistralai:mistral-medium-latest
  • mistralai:mistral-large-latest
  • mistralai:codestral-latest

ollama

Not applicable.

N/A

See https://www.ollama.com/library for a list of models. Pass a model’s name; for example, deepseek-coder-v2.

qianfan

QIANFAN_AK, QIANFAN_SK

  • qianfan:ERNIE-Bot
  • qianfan:ERNIE-Bot-4

togetherai

TOGETHER_API_KEY

  • togetherai:Austism/chronos-hermes-13b
  • togetherai:DiscoResearch/DiscoLM-mixtral-8x7b-v2
  • togetherai:EleutherAI/llemma_7b
  • togetherai:Gryphe/MythoMax-L2-13b
  • togetherai:Meta-Llama/Llama-Guard-7b
  • togetherai:Nexusflow/NexusRaven-V2-13B
  • togetherai:NousResearch/Nous-Capybara-7B-V1p9
  • togetherai:NousResearch/Nous-Hermes-2-Yi-34B
  • togetherai:NousResearch/Nous-Hermes-Llama2-13b
  • togetherai:NousResearch/Nous-Hermes-Llama2-70b

Aliases and custom commands:

Name

Target

gpt2

huggingface_hub:gpt2

gpt3

openai:davinci-002

chatgpt

openai-chat:gpt-3.5-turbo

gpt4

openai-chat:gpt-4

ernie-bot

qianfan:ERNIE-Bot

ernie-bot-4

qianfan:ERNIE-Bot-4

titan

bedrock:amazon.titan-tg1-large

openrouter-claude

openrouter:anthropic/claude-3.5-sonnet:beta