How can I print the decision tree classifier in Jupyter notebook? - scikit-learn

export_graphviz(treeclf, out_file='tree_titanic.dot', feature_names=feature_cols)
At the command line, run this to convert to PNG:
dot -Tpng tree_titanic.dot -o tree_titanic.png
I am using the above code, but when I try to run the (dot command) in the terminal, it doesn't work. How can I run this command in jupyter to visualize the tree? Thanks.

You can run terminal commands in jupyter by putting a ! before the command. For example:
! dot -Tpng tree_titanic.dot -o tree_titanic.png
You'll probably need to have Graphviz installed on your system in order to have the dot program.
You can then use the display sub-module in jupyter/ipython to display the image off the local file system.

Related

Having issues with a shell command in Python

I am currently studying Python. In my course I have got to reading files. My tutor is showing me the following syntax....
!cat data_file/sample.txt
When he executes this command on screen it lets him view the text contents of the file.
When I execute the code it gives me an error of cat not recognized as a valid shell command in Python.
I have read through as much Python documentation as I can and have come up with nothing!
Can anyone please help??
Its not a python specific functionality. Please use Jupyter (Ipython).
Your tutor is probably using jupyter, which in turn uses IPython.
The bang (!) means that Ipython will execute a system shell command, like:
!cat data_file/sample.txt
In this case output the contents of a txt file. This functionality is documented here

What is the difference between starting a command with an ! and starting a command with % in Google Colab? !<command> or %<command>

If I open a Python 3 notebook in Google Colab and I run:
!pwd
!cd ..
!pwd
I get:
/content
/content
Whereas running:
!pwd
%cd ..
!pwd
Results in:
/content
/
/
What's the difference between the execution of !some command and %some command?
A bang ! open a new process before calling that command.
So, a !cd won't change your current process. After it has run, you return back to where you were.
%cd is a magic (there are a few others) that do what you want.
So, normally you will use ! for most of your linux commands. Except a few magics that you will use occationally e.g. %cd, %env

Linux how to write a python file without an IDE

Make a copy of a computer python file you wrote for some other course without an IDE just a simple text editor
Modify this program slightly, without using an IDE.
Run the modified program, without using an IDE
How do I create a python file with linux?
To create a python file using Linux use command touch to create a file(will create a file in current directory, to know the current directory use cd command)
touch myfile.py
Open the file using one of the available text editors, for example vi:
vi myfile.py
Type your code and use command :wq to save and close the file.
a=2
b=3
print a+b
Run your code using python command:
python myfile.py
5 #output
To check or install Python on Linux, please refer to AWS detailed instructions: LINK
To get more instructions on using vi editor: LINK
It is as usual with writing c programs and others. No difference at all. If you are familiar with linux command line you can use command line text editors. Otherwise use gedit , sublime text,vs code,atom..etc (all are text editors with GUI) just as you use a notepad in windows.

Can I run a script that uses 2 different command lines?

Sorry if the title is vague, I am fairly new to Linux and I don't really know how else to put it. I am creating a script and when I run it, I got it to run Sage but after it does so, the next command isn't executed. I presume this is because the first couple were in the standard Terminal (bash?) and everything after ./sage isn't -- here's the script:
#!/bin/bash
cd /home/alex/Desktop/sage-7.6
./sage
#I also tried wait ${!} here but it didn't work
notebook("/home/alex/Desktop/sage-7.6/projects/zero forcing.sagenb")
How might I enter the last command in Sage after it opens (assuming it's possible)? Thanks!
Edit: Here's a picture of my problem. Sage runs but I can't get it to execute the notebook() command after it opens.
You need to run notebook() as sage code using the -c option mentioned [ here ]. Try the below code.
#!/bin/bash
/home/alex/Desktop/sage-7.6/sage # You can run the interactive shell directly
# At this point you have completely exited the sage interactive shell
# Presumably you want to run the below 'notebook()' after every interactive shell
# In that case do
/home/alex/Desktop/sage-7.6/sage -c 'notebook("/home/alex/Desktop/sage-7.6/projects/zero forcing.sagenb")'
I think what you really want is just to have one command that launches a notebook with a given name.
It turns out that in many Linux/Unix applications, there is automatic help at the command line. Try
/home/alex/.../sage -n -h
to get some help on the notebook. In particular,
sage -n -h --notebook=sagenb
gives a very, very long list of options, the first of which shows that
sage --notebook=sagenb directory=tp
will give you a new sage notebook server in the directory tp.sagenb.
All this said, I should also point out that the sagenb (sadly) is slowly becoming a legacy project in favor of the Jupyter notebook. In Sage 8.0 a conversion from sagenb to Jupyter will become the default, and even now you can just do
sage --notebook=jupyter --notebook-dir=/home/foo/bar
for that to start up.

How to make script file to run several commands on several terminals in ubuntu?

I have to open three terminals and each terminal should go to three directories and need to run three files on those three terminals. Now I need speed up my task. Beacuse of that need to do it by running a single script file.How can I do that?
As a example I have to run those three commands on three terminals. But I need to do those thing using one single file running.
cd /home/xyz/Desktop/project/components/core;
python3 Main.py
aria2c --enable-rpc
cd /home/xyz/Desktop/project/ui;
gulp serve
Try to put your commands in one bash file like those below and run it from terminal.
gnome-terminal -e command_1
gnome-terminal -e command_2
gnome-terminal -e command_3
Look here for more details https://askubuntu.com/questions/46627/how-can-i-make-a-script-that-opens-terminal-windows-and-executes-commands-in-the
One of my friends suggested this as an answer and it worked for me.I can run those terminal commands with & operator and then those commands run on background and they will run on the same terminal without any failure.I created a single file using below commands and when I run it, that file prompt to run those commands in same terminal.
!/bin/bash
cd /home/xyz/Desktop/project/components/core;python3 main.py &
aria2c --enable-rpc &
cd /home/kaveesh/Desktop/project/ui;gulp serve &

Resources