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:
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()
5.11. The END#
%ai list
Provider |
Environment variable |
Set? |
Models |
---|---|---|---|
|
|
❌ |
|
|
Not applicable. |
N/A |
|
|
|
❌ |
See https://huggingface.co/models for a list of models. Pass a model’s repository ID as the model ID; for example, |
|
|
❌ |
|
|
Not applicable. |
N/A |
See https://www.ollama.com/library for a list of models. Pass a model’s name; for example, |
|
|
❌ |
|
|
|
❌ |
|
Aliases and custom commands:
Name |
Target |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|