jkitchin / scimax

An emacs starterkit for scientists and engineers
Other
1.03k stars 122 forks source link

not a scimax issue, but a question #479

Closed OrionRandD closed 1 year ago

OrionRandD commented 1 year ago

How to prettify this org mode block analysis of titanic.csv?

If i use jupyter-notebook in a browser, it will render beautifully.

https://yewtu.be/watch?v=I_oq-9OJkfc

But, I think, with Emacs/scimax I can do better. Or, Am I dreaming that Emacs is not the right tool for the job?

As a suggestion, if you have time, perhaps you could reproduce this girl tuto using scimax... It would be a big add to us who love scimax for science...

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Titanic machine learning with scimax

+begin_src python :results output

import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt

titanic = pd.read_csv("titanic.csv")

print()

print(titanic[titanic["Age"]==30])

print(titanic[titanic["Age"]!=30])

+end_src

+RESULTS:

: PassengerId Survived Pclass ... Fare Cabin Embarked : 0 1 0 3 ... 7.2500 NaN S : 1 2 1 1 ... 71.2833 C85 C : 2 3 1 3 ... 7.9250 NaN S : 3 4 1 1 ... 53.1000 C123 S : 4 5 0 3 ... 8.0500 NaN S : : [5 rows x 12 columns]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Dataset is here: ;; https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

jkitchin commented 1 year ago

I would switch to emacs-jupyter:

#+begin_src jupyter-python
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

titanic = pd.read_csv("titanic.csv")
titanic
#+end_src

#+RESULTS:
:RESULTS:
|     | PassengerId | Survived | Pclass | Name                                              | Sex    |  Age | SibSp | Parch |           Ticket |    Fare | Cabin | Embarked |
|-----+-------------+----------+--------+---------------------------------------------------+--------+------+-------+-------+------------------+---------+-------+----------|
|   0 |           1 |        0 |      3 | Braund, Mr. Owen Harris                           | male   | 22.0 |     1 |     0 |        A/5 21171 |  7.2500 |   NaN | S        |
|   1 |           2 |        1 |      1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 |     1 |     0 |         PC 17599 | 71.2833 |   C85 | C        |
|   2 |           3 |        1 |      3 | Heikkinen, Miss. Laina                            | female | 26.0 |     0 |     0 | STON/O2. 3101282 |  7.9250 |   NaN | S        |
|   3 |           4 |        1 |      1 | Futrelle, Mrs. Jacques Heath (Lily May Peel)      | female | 35.0 |     1 |     0 |           113803 | 53.1000 |  C123 | S        |
|   4 |           5 |        0 |      3 | Allen, Mr. William Henry                          | male   | 35.0 |     0 |     0 |           373450 |  8.0500 |   NaN | S        |
| ... |         ... |      ... |    ... | ...                                               | ...    |  ... |   ... |   ... |              ... |     ... |   ... | ...      |
| 886 |         887 |        0 |      2 | Montvila, Rev. Juozas                             | male   | 27.0 |     0 |     0 |           211536 | 13.0000 |   NaN | S        |
| 887 |         888 |        1 |      1 | Graham, Miss. Margaret Edith                      | female | 19.0 |     0 |     0 |           112053 | 30.0000 |   B42 | S        |
| 888 |         889 |        0 |      3 | Johnston, Miss. Catherine Helen "Carrie"          | female |  NaN |     1 |     2 |       W./C. 6607 | 23.4500 |   NaN | S        |
| 889 |         890 |        1 |      1 | Behr, Mr. Karl Howell                             | male   | 26.0 |     0 |     0 |           111369 | 30.0000 |  C148 | C        |
| 890 |         891 |        0 |      3 | Dooley, Mr. Patrick                               | male   | 32.0 |     0 |     0 |           370376 |  7.7500 |   NaN | Q        |

891 rows × 12 columns
:END:

You could also look into tabulate like this:

#+BEGIN_SRC python :results output org
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from tabulate import tabulate

titanic = pd.read_csv("titanic.csv")
print(tabulate(titanic.head(), headers=titanic.columns, tablefmt='orgtbl'))
#+END_SRC

#+RESULTS:
#+begin_src org
|   | PassengerId | Survived | Pclass | Name                                                | Sex    | Age | SibSp | Parch | Ticket           |    Fare | Cabin | Embarked |
|---+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------|
| 0 |           1 |        0 |      3 | Braund, Mr. Owen Harris                             | male   |  22 |     1 |     0 | A/5 21171        |    7.25 |   nan | S        |
| 1 |           2 |        1 |      1 | Cumings, Mrs. John Bradley (Florence Briggs Thayer) | female |  38 |     1 |     0 | PC 17599         | 71.2833 |   C85 | C        |
| 2 |           3 |        1 |      3 | Heikkinen, Miss. Laina                              | female |  26 |     0 |     0 | STON/O2. 3101282 |   7.925 |   nan | S        |
| 3 |           4 |        1 |      1 | Futrelle, Mrs. Jacques Heath (Lily May Peel)        | female |  35 |     1 |     0 | 113803           |    53.1 |  C123 | S        |
| 4 |           5 |        0 |      3 | Allen, Mr. William Henry                            | male   |  35 |     0 |     0 | 373450           |    8.05 |   nan | S        |
#+end_src
OrionRandD commented 1 year ago

Thx a lot... And how I would export the two examples to either html's or pdf's?

jkitchin commented 1 year ago

C-c C-e lo for pdf, or C-c C-e ho for html I guess.

OrionRandD commented 1 year ago

Ok. This I know. What I meant is not exporting the whole org file, but I thought there were directives to add in the: begin_src jupyter-python BEGIN_SRC python :results output org

to automagically get the html/pdf files... Thx a lot, anyway...

jkitchin commented 1 year ago

I don't get what you want. Just a pdf or html of the table?

OrionRandD commented 1 year ago

I don't get what you want. Just a pdf or html of the table?

What I mean is sending the

+RESULTS:

+begin_src org

| | PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |---+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------| | 0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22 | 1 | 0 | A/5 21171 | 7.25 | nan | S | | 1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Thayer) | female | 38 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | | 2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26 | 0 | 0 | STON/O2. 3101282 | 7.925 | nan | S | | 3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35 | 1 | 0 | 113803 | 53.1 | C123 | S | | 4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35 | 0 | 0 | 373450 | 8.05 | nan | S |

+end_src

to an html, or pdf file, telling, e.g. jupyet to do it from this line:

+begin_src jupyter-python

like so:

+begin_src jupyter-python :results output pdf

or

+begin_src jupyter-python :results output html

jkitchin commented 1 year ago

It is not possible to do this:

+begin_src jupyter-python :results output pdf

This doesn't actually make html, it just expects the output to be html, and then it wraps them in a #+begin_export html block.

you could also use tabulate to output html, and just write it to a file I think.

you can highlight the table with your mouse, and then do the export to html or pdf like I described earlier. that will only export the highlighted region.

OrionRandD commented 1 year ago

It is not possible to do this:

+begin_src jupyter-python :results output pdf

This doesn't actually make html, it just expects the output to be html, and then it wraps them in a #+begin_export html block.

you could also use tabulate to output html, and just write it to a file I think.

you can highlight the table with your mouse, and then do the export to html or pdf like I described earlier. that will only export the highlighted region.

Great! Thx a lot, again...