syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
How to Start With VirtualBox, Linux, Python?

This was/is/will-be a presentation at a Meetup:

http://www.meetup.com/Palo-Alto-Data-Science-Association/events/229686809

Start With VirtualBox

What kind of laptop do you have?

If you have a Mac, your laptop is well suited for learning VirtualBox, Linux on VirtualBox, and Python on Linux.

If you have a windows laptop you should ask, is it okay if VirtualBox breaks my laptop?

When you install VirtualBox on a windows laptop, you face a small risk of breaking your laptop so it will not boot.

If you want to face this risk, then it is okay to install VirtualBox on your windows laptop.

If you do not want this risk, put your windows laptop in a safe place and then do one of two things:
  • Buy a Mac laptop, or,
  • Buy a windows laptop from a store which will refund your money if VirtualBox breaks the laptop
Stores which behave well are: Costco, Walmart, and Bestbuy.

Most other stores/websites do not behave well.

After you get a laptop suitable for VirtualBox, you need to enable virtualization.

On Mac this easy. Apple does it for you at the factory.

On windows this is not easy.

The best method is to go to youtube.com and search for: On Windows how to enable virtualization?

Each type of windows laptop is different so I cannot give exact directions on how to do this.

If you have a new windows laptop from Costco, maybe you have a friend who knows how to get into the BIOS of the laptop and then enable virtualization.

If your friend breaks your new laptop, return it for a refund.

After you enable virtualization on your laptop, you should download virtualbox software:

https://www.virtualbox.org/wiki/Downloads

After the download, installation is easy. Just click the downloaded file and answer questions.

Next, you have a choice: The first method is more difficult and thus more educational. The best way to learn it is to watch someone do it via youtube video. This will allow you to rewind and watch difficult sections until you understand it better.

The second method is easier; you just download the large file and then click on it. VirtualBox should wake up and ask you simple questions.

After you install Linux and then start it, you should see a login screen.

I did that and saw this:

The password should be: 'd'

Linux

After I entered the password, Linux displayed screen called the Desktop.

On the lefthand side are a set of icons which is called the 'Launcher':


At the top of the Launcer is an icon I call the 'Finder':

I used the Finder to find 'Terminal' and then I launched Terminal:


Linux Shell

The above Terminal is like a chat screen.

I use Terminal to chat with Linux.

The language I use to chat with Linux is called 'Shell' or sometimes called 'Bash'.

I list below some commands I can send to Linux:
  • ls
  • ls -l
  • cd
  • cd /usr
  • cd ..
  • cd /usr/lib/apt/methods/
  • id
  • date
Most of the commands I send to Linux are short and easy to remember.

If I dont know a command I should use, I use English to describe what I want from Linux and then I ask google.

Google is good at translating English questions into Linux commands.

Type these questions into google:

http://www.google.com/search?q=In+linux+how+much+disk+space+have+I

http://www.google.com/search?q=In+Linux+What+Is+My+IP+Address

The best way to learn about Linux Shell commands is to ask Google to translate English questions into Shell commands.

Before Google, the way to learn Linux Shell commands was to read books and study tutorials.

Now, that method of learning is old and inefficient.

Perhaps though, you should memorize these demos:
  • Create a file:
  • echo hello > ~/hello.txt
  • Copy a file:
  • cp ~/hello.txt ~/hello2.txt
  • List a file:
  • ls -l ~/hello.txt
  • cat ~/hello.txt
  • Update a file:
  • echo hola >> ~/hello.txt
  • Delete a file:
  • rm ~/hello2.txt

  • Create a folder:
  • mkdir ~/mydirectory
  • Copy a folder:
  • rsync -a ~/mydirectory ~/urdirectory
  • List a folder:
  • ls -l ~/mydirectory
  • Update a folder:
  • echo bonjour > ~/mydirectory/bonjour.txt
  • touch ~/mydirectory
  • Delete a folder:
  • rm -rf ~/urdirectory
In Linux we often use the word 'directory' to mean folder.

In Linux a directory is a folder.

Python

I like Anaconda Python.

I learned about Anaconda Python from this site:

https://www.continuum.io/downloads

I installed Anaconda on my Linux laptop by following the instructions at the above page.

I thought it was easy but I noticed a set of common questions from people who install it:
  • Should I use Python 2 or Python 3?
  • Second choice is Python 2, first choice is 3.
  • At the end of the install, it asks to change PATH, what should I do?
  • Let it change PATH.
  • Where does it live after installation?
  • Either ~/anaconda3 or ~/anaconda
  • How to test the installation?
  • Shell command: python
Demo:
dan@nia111:~/x611 $
dan@nia111:~/x611 $ python
Python 3.5.1 |Anaconda 2.4.1 (64-bit)| (default, Dec  7 2015, 11:16:01)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>>

Python Numbers

Above I see that python can behave like a shell. I type commands, it responds.

Try these commands:
my_i = 2
ur_i = 3
an_i = my_i + ur_i
an_i
divout = my_i / ur_i
divout
my_f = 2.2
ur_f = 3.3
an_f = my_f + ur_f
an_f
divout = my_f / ur_f
divout

Python List

A Python List is like a JavaScript Array. Lists are easy:
my_l = [0,1,2,3,4,5,6,7,8,9]
my_l + my_l
my_l[0]
my_l[0:3]
my_l[:3]
my_l[-1]
my_l[-1:]
my_l[-3:]
my_l[-5:-2]
# I should use predicate on a list.
# The predicate I want is (member > 4).
# I should code it up now:
ur_l = [member for member in my_l if (member > 4)]
Learning Python can be similar to learning Shell.

I describe what I want; I ask google.

Demo:

http://www.google.com/search?q=In+python+how+to+check+if+a+number+in+a+list

http://www.google.com/search?q=In+Python+how+to+get+length+of+List

Python Dictionary

I use a Python Dictionary to act as a key-value store. A Dictionary is like a JavaScript object or a Ruby Hash.
the_d = {'myky': 44, 'another_key': 'Im a string value'}
the_d['Im_akey_too'] = ['I',' am a ', ' List... ']
the_d
the_d['myky'] = 45
the_d
http://www.google.com/search?q=In+Python+how+to+find+if+key+in+a+Dictionary

http://www.google.com/search?q=In+Python+how+to+combine+Dictionaries

http://www.google.com/search?q=In+Python+how+to+list+all+keys+in+a+Dictionary

http://www.google.com/search?q=In+Python+how+to+list+all+values+in+a+Dictionary

http://www.google.com/search?q=In+Python+how+to+remove+key-value-pair+from+Dictionary

http://www.google.com/search?q=In+Python+how+to+get+length+of+Dictionary

Python Loops

In Python how to create a for loop?

Method 1:

for row in range(4):
  print(row)

for row in range(4,9):
  print(row)

for row in range(9,4,-1):
  print(row)
Method 2:

mylist_l = ['a','b','z','hello','world']
for row in mylist_l:
  print(row)

Python Comprehension

In Python how to create a Comprehension?

I use a loop to understand a Comprehension:

urlist_l = ['dan','ann','jan','randy','jill']
thelist_l = []
for row in urlist_l:
  row2 = 'hello '+row
  thelist_l.append(row2)
print(urlist_l)
print(thelist_l)
The comprehension below can replace the loop above:
zlist_l = ['hello '+row for row in urlist_l]
print(zlist_l)

Python Function

The syntax below demonstrates how to create and call a function:

def myhello(mystring_s):
  urstring_s = 'hello '+mystring_s
  return urstring_s

astring_s   = 'world'
thestring_s = myhello(astring_s)
print(thestring_s)

Python import

The Python import command allows me to import packages into my Python. I tried to use numpy:

dan@nia111:~/ttmp $ python
Python 3.5.1 |Anaconda 2.4.1 (64-bit)| (default, Dec  7 2015, 11:16:01) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> myarray_a = np.array([[1,2,3],[1,2,3],[1,2,3]])
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'np' is not defined
>>> 
It failed. I called import:

dan@nia111:~/ttmp $ 
dan@nia111:~/ttmp $ python
Python 3.5.1 |Anaconda 2.4.1 (64-bit)| (default, Dec  7 2015, 11:16:01) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> myarray_a = np.array([[1,2,3],[1,2,3],[1,2,3]])
>>> myarray_a
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
>>> 
>>> 
It worked.

Python Numpy

Here is some Numpy syntax. You should try it:
import numpy as np
row0_l = [0.1,1,2,3]
row1_l = [4,5,6,7]
row2_l = [8,9,10,11]
row3_l = [12,13,14,15]
myarray_a = np.array([row0_l,row1_l,row2_l,row3_l])
myarray_a[1:3, 1:4]
myarray_a[:, 1:4]
myarray_a[1:3, :]
# I should use a predicate on myarray_a
predicate1 = (myarray_a[:,0] == 8)
# English of above predicate:
# get all rows,
# where column0 == 8
predicate1
ur_array_a = myarray_a[predicate1,:]
ur_array_a
# Now, I should demo array arithmetic:
1.1 + myarray_a
myarray_a + myarray_a
myarray_a - myarray_a
myarray_a / myarray_a
myarray_a * myarray_a
(1.1 + myarray_a) / (2.2 - myarray_a)

Python Pandas

Often I use Pandas to read CSV files from Yahoo. I start this demo with shell commands in my terminal:
cd /tmp
wget --output-document=TEAM.csv http://ichart.finance.yahoo.com/table.csv?s=TEAM
Then I use Pandas to read it:
import pandas as pd
team_df = pd.read_csv('/tmp/TEAM.csv')
team_df.head()
team_df.columns
new_columns_l = [col_s.lower().replace(' ','_') for col_s in team_df.columns]
new_columns_l
team_df.columns = new_columns_l
team_df.head()
# I should create a predicate for team_df:
predicate2 = (team_df['date'] == '2016-03-17')
# I should apply the predicate:
t0317_df = team_df[predicate2]
# I should create a predicate for team_df:
predicate3 = (team_df['close'] > 30.0)
# I should apply the predicate:
t30_df = team_df[predicate3]
t30_df



To be continued...




syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me