Difference between revisions of "Python for Data Science"

From Sinfronteras
Jump to: navigation, search
(Arrays)
(Arrays)
Line 120: Line 120:
 
| rowspan="9" |<h5 style="text-align:left">Methods for creating NumPy Arrays</h5>
 
| rowspan="9" |<h5 style="text-align:left">Methods for creating NumPy Arrays</h5>
 
|<h6 style="text-align:left">From a Python List</h6>
 
|<h6 style="text-align:left">From a Python List</h6>
| colspan="2" |'''''<code>array</code>'''''
+
| colspan="2" |'''''<code>array()</code>'''''
 
|We can create an array by directly converting a list or list of lists.
 
|We can create an array by directly converting a list or list of lists.
 
|<code>my_list = [1,2,3]</code>
 
|<code>my_list = [1,2,3]</code>
Line 131: Line 131:
 
|-
 
|-
 
| rowspan="8" |<h6 style="text-align:left">From Built-in NumPy Methods</h6>
 
| rowspan="8" |<h6 style="text-align:left">From Built-in NumPy Methods</h6>
| colspan="2" |'''''<code>arange</code>'''''
+
| colspan="2" |'''''<code>arange()</code>'''''
 
|Return evenly spaced values within a given interval.
 
|Return evenly spaced values within a given interval.
 
|<code>np.arange(0,10)</code>
 
|<code>np.arange(0,10)</code>
 
<code>np.arange(0,11,2)</code>
 
<code>np.arange(0,11,2)</code>
 
|-
 
|-
| colspan="2" |'''''<code>zeros</code>'''''
+
| colspan="2" |'''''<code>zeros()</code>'''''
 
|Generate arrays of zeros.
 
|Generate arrays of zeros.
 
|<code>np.zeros(3)</code>
 
|<code>np.zeros(3)</code>
 
<code>np.zeros((5,5))</code>
 
<code>np.zeros((5,5))</code>
 
|-
 
|-
| colspan="2" |'''''<code>ones</code>'''''
+
| colspan="2" |'''''<code>ones()</code>'''''
 
|Generate arrays of ones.
 
|Generate arrays of ones.
 
|<code>np.ones(3)</code>
 
|<code>np.ones(3)</code>
 
<code>np.ones((3,3))</code>
 
<code>np.ones((3,3))</code>
 
|-
 
|-
| colspan="2" |'''''<code>linspace</code>'''''
+
| colspan="2" |'''''<code>linspace()</code>'''''
 
|Return evenly spaced numbers over a specified interval.
 
|Return evenly spaced numbers over a specified interval.
 
|<code>np.linspace(0,10,3)</code>
 
|<code>np.linspace(0,10,3)</code>
 
<code>np.linspace(0,10,50)</code>
 
<code>np.linspace(0,10,50)</code>
 
|-
 
|-
| colspan="2" |'''''<code>eye</code>'''''
+
| colspan="2" |'''''<code>eye()</code>'''''
 
|Creates an identity matrix.
 
|Creates an identity matrix.
 
|<code>np.linspace(0,10,50)</code>
 
|<code>np.linspace(0,10,50)</code>
 
|-
 
|-
 
| rowspan="3" |'''''<code>random</code>'''''
 
| rowspan="3" |'''''<code>random</code>'''''
|'''''<code>rand</code>'''''
+
|'''''<code>rand()</code>'''''
 
|Create an array of the given shape and populate it with random samples from a uniform distribution over <code>[0, 1)</code>.
 
|Create an array of the given shape and populate it with random samples from a uniform distribution over <code>[0, 1)</code>.
 
|<code>np.random.rand(2)</code>
 
|<code>np.random.rand(2)</code>
 
<code>np.random.rand(5,5)</code>
 
<code>np.random.rand(5,5)</code>
 
|-
 
|-
|'''''<code>randn</code>'''''
+
|'''''<code>randn()</code>'''''
 
|Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform.
 
|Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform.
 
|<code>np.random.randn(2)</code>
 
|<code>np.random.randn(2)</code>
 
<code>np.random.randn(5,5)</code>
 
<code>np.random.randn(5,5)</code>
 
|-
 
|-
|'''''<code>randint</code>'''''
+
|'''''<code>randint()</code>'''''
 
|Return random integers from <code>low</code> (inclusive) to <code>high</code> (exclusive).
 
|Return random integers from <code>low</code> (inclusive) to <code>high</code> (exclusive).
 
|<code>np.random.randint(1,100)</code>
 
|<code>np.random.randint(1,100)</code>
Line 172: Line 172:
 
|-
 
|-
 
| colspan="2" rowspan="4" |<h5 style="text-align:left">Others Array Attributes and Methods</h5>
 
| colspan="2" rowspan="4" |<h5 style="text-align:left">Others Array Attributes and Methods</h5>
| colspan="2" |''<code>'''reshape'''</code>''
+
| colspan="2" |''<code>'''reshape()'''</code>''
 
|Returns an array containing the same data with a new shape.
 
|Returns an array containing the same data with a new shape.
 
|<code>arr.reshape(5,5)</code>
 
|<code>arr.reshape(5,5)</code>
 
|-
 
|-
| colspan="2" |'''''<code>max</code>, <code>min</code>, <code>argmax</code>, <code>argmin</code>'''''
+
| colspan="2" |'''''<code>max()</code>, <code>min()</code>, <code>argmax()</code>, <code>argmin()</code>'''''
 
|Finding max or min values. Or to find their index locations using argmin or argmax.
 
|Finding max or min values. Or to find their index locations using argmin or argmax.
 
|<code>arr.max()</code>
 
|<code>arr.max()</code>
 
<code>arr.argmax()</code>
 
<code>arr.argmax()</code>
 
|-
 
|-
| colspan="2" |''<code>'''Shape'''</code>''
+
| colspan="2" |''<code>'''shape()'''</code>''
 
|Shape is an attribute that arrays have (not a method).
 
|Shape is an attribute that arrays have (not a method).
 
|NO LO ENTENDI.. REVISAR!
 
|NO LO ENTENDI.. REVISAR!
 
|-
 
|-
| colspan="2" |''<code>'''dtype'''</code>''
+
| colspan="2" |''<code>'''dtype()'''</code>''
 
|You can also grab the data type of the object in the array.
 
|You can also grab the data type of the object in the array.
 
|<code>arr.dtype</code>
 
|<code>arr.dtype</code>
Line 191: Line 191:
 
| colspan="6" |
 
| colspan="6" |
 
|-
 
|-
| rowspan="8" |Indexing and Selection
+
| rowspan="7" |Indexing and Selection
  
 
*How to select elements or groups of elements from an array.
 
*How to select elements or groups of elements from an array.
 
*The general format is '''arr_2d[row][col]''' or '''arr_2d[row,col]'''. I recommend usually using the comma notation for clarity.
 
*The general format is '''arr_2d[row][col]''' or '''arr_2d[row,col]'''. I recommend usually using the comma notation for clarity.
 
|
 
|
| colspan="2" |
+
| colspan="4" |'''Creating sample array for the following examples'''<syntaxhighlight lang="python3">
|
 
|'''#Creating sample array for the following examples'''<syntaxhighlight lang="python3">
 
 
# 1D Array:
 
# 1D Array:
 
arr = np.arange(0,11)
 
arr = np.arange(0,11)
Line 213: Line 211:
 
       [20, 25, 30],
 
       [20, 25, 30],
 
       [35, 40, 45]])
 
       [35, 40, 45]])
</syntaxhighlight><br />
+
</syntaxhighlight>
 
|-
 
|-
 
|Bracket Indexing and Selection (Slicing)
 
|Bracket Indexing and Selection (Slicing)
 
| colspan="2" |
 
| colspan="2" |
|
+
|When '''slicing''', data is not copied, it's a view of the original array! This avoids memory problems!. To get a copy, need to use the method '''copy()'''. See important note below.
 
|'''#'''Get a value at an index
 
|'''#'''Get a value at an index
  
Line 223: Line 221:
  
 
arr_2d[1]
 
arr_2d[1]
 +
  
 
arr_2d[1][0]  
 
arr_2d[1][0]  
Line 232: Line 231:
  
 
arr[1:5]
 
arr[1:5]
 +
 +
slice_of_arr = arr[0:6]
 
|-
 
|-
| rowspan="4" |Broadcasting
+
| rowspan="2" |Broadcasting
 +
 
 +
 
 +
(Setting a value with index range )
  
  
 
Numpy arrays differ from a normal Python list because of their ability to broadcast.
 
Numpy arrays differ from a normal Python list because of their ability to broadcast.
 
| colspan="2" |
 
| colspan="2" |
|Setting a value with index range (Broadcasting)
+
|Setting a value with index range
 
|arr[0:5]=100
 
|arr[0:5]=100
  
Line 247: Line 251:
  
 
Output: array([100, 100, 100, 100, 100,  5,  6,  7,  8,  9,  10])
 
Output: array([100, 100, 100, 100, 100,  5,  6,  7,  8,  9,  10])
<br />
 
 
|-
 
|-
 
| colspan="2" |
 
| colspan="2" |
Line 253: Line 256:
 
|arr[:]=99
 
|arr[:]=99
 
|-
 
|-
| colspan="2" |
+
|Get a copy of an Array
|Slice an Array
+
| colspan="2" |'''<code>copy''()''</code>'''
|slice_of_arr = arr[0:6]
+
|When '''slicing''', data is not copied, it's a view of the original array! This avoids memory problems!. To get a copy, need to use the method '''copy()'''. See important note below.
 
+
|arr_copy = arr.copy()
 
+
|-
'''#'''Show slice
+
|'''Important notes on Slices'''
 
+
| colspan="4" |<syntaxhighlight lang="python3">
 +
slice_of_arr = arr[0:6]
 +
#Show slice
 
slice_of_arr
 
slice_of_arr
 
 
Output: array([0, 1, 2, 3, 4, 5])
 
Output: array([0, 1, 2, 3, 4, 5])
  
 
+
#Making changes in slice_of_arr
'''#Important notes on Slices:'''
 
 
 
'''#*'''Making changes in slice_of_arr
 
 
 
 
slice_of_arr[:]=99
 
slice_of_arr[:]=99
 
+
#Show slice
 
 
'''#*'''Show slice
 
 
 
 
slice_of_arr
 
slice_of_arr
 
 
Output: array([99, 99, 99, 99, 99, 99])
 
Output: array([99, 99, 99, 99, 99, 99])
  
 
+
#Now note the changes also occur in our original array!
'''#*'''Now note the changes also occur in our original array!
+
#Show
 
 
'''#'''Show
 
 
 
 
arr
 
arr
 +
Output: array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])
  
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])
+
#When slicing, data is not copied, it's a view of the original array! This avoids memory problems!
  
 
+
#To get a copy, need to use the method copy()
'''#*'''Data is not copied, it's a view of the original array! This avoids memory problems!
+
</syntaxhighlight>
 
 
<nowiki>#*</nowiki> To get a copy, need to use the method '''copy()'''
 
 
|-
 
|-
| colspan="2" |'''<code>copy</code>'''
+
|Indexing a 2D array (matrices)
|Get a copy of an Array
+
| colspan="2" |
|arr_copy = arr.copy()
+
|The general format is '''arr_2d[row][col]''' or '''arr_2d[row,col]'''. I recommend usually using the comma notation for clarity.
|-
 
| rowspan="2" |Indexing a 2D array (matrices)
 
| colspan="2" rowspan="2" |
 
| rowspan="2" |The general format is '''arr_2d[row][col]''' or '''arr_2d[row,col]'''. I recommend usually using the comma notation for clarity.
 
 
|arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))
 
|arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))
  
Line 309: Line 297:
 
[20, 25, 30],
 
[20, 25, 30],
 
[35, 40, 45]])
 
[35, 40, 45]])
|-
 
|
 
 
|}
 
|}
  
  
 
<br />
 
<br />

Revision as of 19:36, 16 October 2019

For a standard Python tutorial go to Python



Anaconda

Anaconda is a free and open source distribution of the Python and R programming languages for data science and machine learning related applications (large-scale data processing, predictive analytics, scientific computing), that aims to simplify package management and deployment. Package versions are managed by the package management system conda. https://en.wikipedia.org/wiki/Anaconda_(Python_distribution)

En otras palabras, Anaconda puede ser visto como un paquete (a distribution) que incluye no solo Python (or R) but many libraries that are used in Data Science, as well as its own virtual environment system. It's an "all-in-one" install that is extremely popular in data science and Machine Learning.



Installation

https://linuxize.com/post/how-to-install-anaconda-on-ubuntu-18-04/

https://www.digitalocean.com/community/tutorials/how-to-install-the-anaconda-python-distribution-on-ubuntu-18-04



Anaconda comes with a few IDE

  • Jupyter Lab
  • Jupyter Notebook
  • Spyder
  • Qtconsole
  • and others



Anaconda Navigator

Anaconda Navigator is a GUI that helps you to easily start important applications and manage the packages in your local Anaconda installation

You can open the Anaconda Navigator from the Terminal:

anaconda-navigator



Jupyter

Jupyter comes with Anaconda.

  • It is a development environment (IDE) where we can write codes; but it also allows us to display images, and write down markdown notes.
  • It is the most popular IDE in data science for exploring and analyzing data.
  • Other famoues IDE for Python are Sublime Text and PyCharm.
  • There is Jupyter Lab and Jupyter Notebook



Online Jupyter

There are many sites that provides solutions to run your Jupyter Notebook in the cloud: https://www.dataschool.io/cloud-services-for-jupyter-notebook/

I have tried:

https://cocalc.com/projects/595bf475-61a7-47fa-af69-ba804c3f23f9/files/?session=default
Parece bueno, pero tiene opciones que no son gratis


https://www.kaggle.com/adeloaleman/kernel1917a91630/edit
Parece bueno pero no encontré la forma adicionar una TOC


Es el que estoy utilizando ahora



Udemy - Python for Data Science and Machine Learning Bootcamp

https://www.udemy.com/course/python-for-data-science-and-machine-learning-bootcamp/



Most popular Python Data Science Libraries

  • NumPy
  • SciPy
  • Pandas
  • Seaborn
  • SciKit'Learn
  • MatplotLib
  • Plotly
  • PySpartk



NumPy

  • NumPy (or Numpy) is a Linear Algebra Library for Python, the reason it is so important for Data Science with Python is that almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks.



Installation

It is highly recommended you install Python using the Anaconda distribution to make sure all underlying dependencies (such as Linear Algebra libraries) all sync up with the use of a conda install.

  • If you have Anaconda, install NumPy by:
conda install numpy
  • If you are not using Anaconda distribution:
pip install numpy


Arrays

Method Description/Comments Example
Methods for creating NumPy Arrays
From a Python List
array() We can create an array by directly converting a list or list of lists. my_list = [1,2,3]

np.array(my_list)


my_matrix = [[1,2,3],[4,5,6],[7,8,9]]

np.array(my_matrix)

From Built-in NumPy Methods
arange() Return evenly spaced values within a given interval. np.arange(0,10)

np.arange(0,11,2)

zeros() Generate arrays of zeros. np.zeros(3)

np.zeros((5,5))

ones() Generate arrays of ones. np.ones(3)

np.ones((3,3))

linspace() Return evenly spaced numbers over a specified interval. np.linspace(0,10,3)

np.linspace(0,10,50)

eye() Creates an identity matrix. np.linspace(0,10,50)
random rand() Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1). np.random.rand(2)

np.random.rand(5,5)

randn() Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform. np.random.randn(2)

np.random.randn(5,5)

randint() Return random integers from low (inclusive) to high (exclusive). np.random.randint(1,100)

np.random.randint(1,100,10)

Others Array Attributes and Methods
reshape() Returns an array containing the same data with a new shape. arr.reshape(5,5)
max(), min(), argmax(), argmin() Finding max or min values. Or to find their index locations using argmin or argmax. arr.max()

arr.argmax()

shape() Shape is an attribute that arrays have (not a method). NO LO ENTENDI.. REVISAR!
dtype() You can also grab the data type of the object in the array. arr.dtype
Indexing and Selection
  • How to select elements or groups of elements from an array.
  • The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend usually using the comma notation for clarity.
Creating sample array for the following examples
# 1D Array:
arr = np.arange(0,11)
#Show
arr
Output: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# 2D Array
arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))
#Show
arr_2d
Output: 
array([[ 5, 10, 15],
       [20, 25, 30],
       [35, 40, 45]])
Bracket Indexing and Selection (Slicing) When slicing, data is not copied, it's a view of the original array! This avoids memory problems!. To get a copy, need to use the method copy(). See important note below. #Get a value at an index

arr[8]

arr_2d[1]


arr_2d[1][0]

arr_2d[1,0] # The same that above


#Get values in a range

arr[1:5]

slice_of_arr = arr[0:6]

Broadcasting


(Setting a value with index range )


Numpy arrays differ from a normal Python list because of their ability to broadcast.

Setting a value with index range arr[0:5]=100


#Show

arr

Output: array([100, 100, 100, 100, 100, 5, 6, 7, 8, 9, 10])

Setting all the values of an Array arr[:]=99
Get a copy of an Array copy() When slicing, data is not copied, it's a view of the original array! This avoids memory problems!. To get a copy, need to use the method copy(). See important note below. arr_copy = arr.copy()
Important notes on Slices
slice_of_arr = arr[0:6]
#Show slice
slice_of_arr
Output: array([0, 1, 2, 3, 4, 5])

#Making changes in slice_of_arr
slice_of_arr[:]=99
#Show slice
slice_of_arr
Output: array([99, 99, 99, 99, 99, 99])

#Now note the changes also occur in our original array!
#Show
arr
Output: array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

#When slicing, data is not copied, it's a view of the original array! This avoids memory problems!

#To get a copy, need to use the method copy()
Indexing a 2D array (matrices) The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend usually using the comma notation for clarity. arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))


#Show

arr_2d

array([[ 5, 10, 15], [20, 25, 30], [35, 40, 45]])