You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/use-cases/AI_ML/jupyter-notebook.md
+45-29Lines changed: 45 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
---
2
2
slug: /use-cases/AI/jupyter-notebook
3
-
sidebar_label: 'Exploring data in Jupyter notebooks with chdb'
4
-
title: 'Exploring data in Jupyter notebooks with chdb'
5
-
description: 'This guide explains how to setup and use chdb to explore data from ClickHouse Cloud or local files in Jupyer notebooks'
6
-
keywords: ['ML', 'Jupyer', 'chdb', 'pandas']
3
+
sidebar_label: 'Exploring data in Jupyter notebooks with chDB'
4
+
title: 'Exploring data in Jupyter notebooks with chDB'
5
+
description: 'This guide explains how to setup and use chDB to explore data from ClickHouse Cloud or local files in Jupyer notebooks'
6
+
keywords: ['ML', 'Jupyer', 'chDB', 'pandas']
7
7
doc_type: 'guide'
8
8
---
9
9
@@ -18,14 +18,20 @@ import image_7 from '@site/static/images/use-cases/AI_ML/jupyter/7.png';
18
18
import image_8 from '@site/static/images/use-cases/AI_ML/jupyter/8.png';
19
19
import image_9 from '@site/static/images/use-cases/AI_ML/jupyter/9.png';
20
20
21
-
# Exploring data with Jupyter notebooks and chdb
21
+
# Exploring data with Jupyter notebooks and chDB
22
22
23
-
In this guide, you will learn how you can explore a dataset on ClickHouse Cloud data in Jupyter notebook with the help of [chdb](/chdb) - a fast in-process SQL OLAP Engine powered by ClickHouse.
23
+
In this guide, you will learn how you can explore a dataset on ClickHouse Cloud data in Jupyter notebook with the help of [chDB](/chdb) - a fast in-process SQL OLAP Engine powered by ClickHouse.
24
24
25
-
Pre-requisites:
25
+
**Prerequisites**:
26
26
- a virtual environment
27
27
- a working ClickHouse Cloud service and your [connection details](/cloud/guides/sql-console/gather-connection-details)
28
28
29
+
**What you'll learn:**
30
+
- Connect to ClickHouse Cloud from Jupyter notebooks using chDB
31
+
- Query remote datasets and convert results to Pandas DataFrames
32
+
- Combine cloud data with local CSV files for analysis
33
+
- Visualize data using matplotlib
34
+
29
35
We'll be using the UK Property Price dataset which is available on ClickHouse Cloud as one of the starter datasets.
30
36
It contains data about the prices that houses were sold for in the United Kingdom from 1995 to 2024.
31
37
@@ -47,7 +53,7 @@ Then click `Import dataset`:
47
53
48
54
ClickHouse will automatically create the `pp_complete` table in the `default` database and fill the table with 28.92 million rows of price point data.
49
55
50
-
In order to reduce the likelihood of exposing your credentials, we recommend to add your Cloud username and password as environment variables.
56
+
In order to reduce the likelihood of exposing your credentials, we recommend to add your Cloud username and password as environment variables on your local machine.
51
57
From a terminal run the following command to add your username and password as environment variables:
In the snippet above, `chdb.query(query, "DataFrame")` runs the specified query and outputs the result to the terminal as a pandas DataFrame.
150
+
In the snippet above, `chdb.query(query, "DataFrame")` runs the specified query and outputs the result to the terminal as a Pandas DataFrame.
145
151
In the query we are using the `remoteSecure` function to connect to ClickHouse Cloud.
146
152
The `remoteSecure` functions takes as parameters:
147
153
- a connection string
148
154
- the name of the database and table to use
149
-
- your user name
155
+
- your username
150
156
- your password
151
157
152
-
As a security best practice, you should should prefer using environment variables for the username and password parameters rather than specifying them directly in the function, although this is possible if you wish.
158
+
As a security best practice, you should prefer using environment variables for the username and password parameters rather than specifying them directly in the function, although this is possible if you wish.
153
159
154
-
The `remoteSecure` function connects to the remote ClickHouse Cloud service, runs the query and returns the result. Depending on the size of your data this could take a few seconds. In this case we return an average price point per year, and filter by `town='LONDON'`. The result is then stored as a DataFrame in a variable called `df`.
160
+
The `remoteSecure` function connects to the remote ClickHouse Cloud service, runs the query and returns the result.
161
+
Depending on the size of your data, this could take a few seconds.
162
+
In this case we return an average price point per year, and filter by `town='LONDON'`.
163
+
The result is then stored as a DataFrame in a variable called `df`.
155
164
156
165
`df.head` displays only the first few rows of the returned data:
157
166
@@ -170,7 +179,7 @@ dtype: object
170
179
```
171
180
172
181
Notice that while `date` is of type `Date` in ClickHouse, in the resulting data frame it is of type `uint16`.
173
-
chdb automatically infers the most appropriate type when returning the DataFrame.
182
+
chDB automatically infers the most appropriate type when returning the DataFrame.
174
183
175
184
With the data now available to us in a familiar form, let's explore how prices of property in London have changed with time.
Perhaps unsurprisingly, property prices in London have massively increased over time.
206
+
Perhaps unsurprisingly, property prices in London have increased substantially over time.
198
207
199
-
A colleague has sent us a .csv file with additional housing related variables.
200
-
Let's plot some of these against the housing prices and see if we can discover any interesting correlations.
208
+
A fellow data scientist has sent us a .csv file with additional housing related variables and is curious how
209
+
the number of houses sold in London has changed over time.
210
+
Let's plot some of these against the housing prices and see if we can discover any correlation.
201
211
202
212
You can use the `file` table engine to read files directly on your local machine.
203
213
In a new cell, run the following command to make a new DataFrame from the local .csv file.
@@ -207,7 +217,7 @@ query = f"""
207
217
SELECT
208
218
toYear(date) AS year,
209
219
sum(houses_sold)*1000
210
-
FROM file('/Users/sstruw/Desktop/housing_in_london_monthly_variables.csv')
220
+
FROM file('/Users/datasci/Desktop/housing_in_london_monthly_variables.csv')
211
221
WHERE area = 'city of london' AND houses_sold IS NOT NULL
212
222
GROUP BY toYear(date)
213
223
ORDER BY year;
@@ -265,9 +275,15 @@ plt.show()
265
275
266
276
<Imagesize="md"img={image_9}alt="Plot of remote data set and local data set"/>
267
277
268
-
It looks like housing prices in London have steadily risen over the years, while the number of houses sold has fluctuated greatly over time but generally trends downwards, at times even dropping below 1995 levels.
269
-
Yikes!
278
+
From the plotted data, we see that sales started around 160000 in the year 1995 and surged quickly, peaking at around 540000 in 19999.
279
+
After that, volumes declined sharply through the mid-2000s, dropping severely during the 2007-2008 financial crisis and falling to around 140 000.
280
+
Prices on the other hand showed steady, consistent growth from about £150,000 in 1995 to around £300,000 by 2005.
281
+
Growth accelerated significantly after 2012, rising steeply from roughly £400,000 to over £1,000,000 by 2019.
282
+
Unlike sales volume, prices showed minimal impact from the 2008 crisis and maintained an upward trajectory. Yikes!
270
283
271
284
## Summary {#summary}
272
285
273
-
Whilst your average London-based data scientist may not be able to afford their own home any time soon, chdb allows you to easily work with data from multiple sources like ClickHouse Cloud and local CSV files easily in Jupyter notebook using the libraries you know and love like Pandas and matplotlib.
286
+
This guide demonstrated how chDB enables seamless data exploration in Jupyter notebooks by connecting ClickHouse Cloud with local data sources.
287
+
Using the UK Property Price dataset, we showed how to query remote ClickHouse Cloud data with the `remoteSecure()` function, read local CSV files with the `file()` table engine, and convert results directly to Pandas DataFrames for analysis and visualization.
288
+
Through chDB, data scientists can leverage ClickHouse's powerful SQL capabilities alongside familiar Python tools like Pandas and matplotlib, making it easy to combine multiple data sources for comprehensive analysis.
289
+
While many a London-based data scientist may not be able to afford their own home or apartment any time soon, at least they can analyze the market that priced them out!
0 commit comments