Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


Manual de prácticas con NumPy en Python, Ejercicios de Programación Javascript

Este documento proporciona una guía práctica para trabajar con numpy, una biblioteca de python ampliamente utilizada para el análisis de datos científicos. El documento abarca temas como la creación y manipulación de arrays, operaciones matemáticas, transformaciones de datos, visualización de datos y mucho más. Es una excelente fuente de información para aquellos que deseen aprender a utilizar numpy de manera eficiente y efectiva.

Tipo: Ejercicios

2023/2024

Subido el 25/01/2024

jeffersson-bustos-ortiz
jeffersson-bustos-ortiz 🇪🇸

1 documento

1 / 9

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
Selecting List Elements
Import libraries
>>> import numpy
>>> import numpy as np
Selective import
>>> from math import pi
>>> help(str)
Python For Data Science Cheat Sheet
Python Basics
Learn More Python for Data Science Interactively at www.datacamp.com
Variable Assignment
Strings
>>> x=5
>>> x
5
>>> x+2 Sum of two variables
7
>>> x-2 Subtraction of two variables
3
>>> x*2 Multiplication of two variables
10
>>> x**2 Exponentiation of a variable
25
>>> x%2 Remainder of a variable
1
>>> x/float(2) Division of a variable
2.5
Variables and Data Types
str() '5', '3.45', 'True'
int() 5, 3, 1
float() 5.0, 1.0
bool() True, True, True
Variables to strings
Variables to integers
Variables to floats
Variables to booleans
Lists
>>> a = 'is'
>>> b = 'nice'
>>> my_list = ['my', 'list', a, b]
>>> my_list2 = [[4,5,6,7], [3,4,5,6]]
Subset
>>> my_list[1]
>>> my_list[-3]
Slice
>>> my_list[1:3]
>>> my_list[1:]
>>> my_list[:3]
>>> my_list[:]
Subset Lists of Lists
>>> my_list2[1][0]
>>> my_list2[1][:2]
Also see NumPy Arrays
>>> my_list.index(a)
>>> my_list.count(a)
>>> my_list.append('!')
>>> my_list.remove('!')
>>> del(my_list[0:1])
>>> my_list.reverse()
>>> my_list.extend('!')
>>> my_list.pop(-1)
>>> my_list.insert(0,'!')
>>> my_list.sort()
Get the index of an item
Count an item
Append an item at a time
Remove an item
Remove an item
Reverse the list
Append an item
Remove an item
Insert an item
Sort the list
Index starts at 0
Select item at index 1
Select 3rd last item
Select items at index 1 and 2
Select items after index 0
Select items before index 3
Copy my_list
my_list[list][itemOfList]
Libraries
>>> my_string.upper()
>>> my_string.lower()
>>> my_string.count('w')
>>> my_string.replace('e', 'i')
>>> my_string.strip()
>>> my_string = 'thisStringIsAwesome'
>>> my_string
'thisStringIsAwesome'
Numpy Arrays
>>> my_list = [1, 2, 3, 4]
>>> my_array = np.array(my_list)
>>> my_2darray = np.array([[1,2,3],[4,5,6]])
>>> my_array.shape
>>> np.append(other_array)
>>> np.insert(my_array, 1, 5)
>>> np.delete(my_array,[1])
>>> np.mean(my_array)
>>> np.median(my_array)
>>> my_array.corrcoef()
>>> np.std(my_array)
Asking For Help
>>> my_string[3]
>>> my_string[4:9]
Subset
>>> my_array[1]
2
Slice
>>> my_array[0:2]
array([1, 2])
Subset 2D Numpy arrays
>>> my_2darray[:,0]
array([1, 4])
>>> my_list + my_list
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list * 2
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list2 > 4
True
>>> my_array > 3
array([False, False, False, True], dtype=bool)
>>> my_array * 2
array([2, 4, 6, 8])
>>> my_array + np.array([5, 6, 7, 8])
array([6, 8, 10, 12])
>>> my_string * 2
'thisStringIsAwesomethisStringIsAwesome'
>>> my_string + 'Innit'
'thisStringIsAwesomeInnit'
>>> 'm' in my_string
True DataCamp
Learn Python for Data Science Interactively
Scientific computing
Data analysis
2D plotting
Machine learning
Also see Lists
Get the dimensions of the array
Append items to an array
Insert items in an array
Delete items in an array
Mean of the array
Median of the array
Correlation coefficient
Standard deviation
String to uppercase
String to lowercase
Count String elements
Replace String elements
Strip whitespaces
Select item at index 1
Select items at index 0 and 1
my_2darray[rows, columns]
Install Python
Calculations With Variables
Leading open data science platform
powered by Python
Free IDE that is included
with Anaconda
Create and share
documents with live code,
visualizations, text, ...
Types and Type Conversion
String Operations
List Operations
List Methods
Index starts at 0
String Methods
String Operations
Selecting Numpy Array Elements Index starts at 0
Numpy Array Operations
Numpy Array Functions
pf3
pf4
pf5
pf8
pf9

Vista previa parcial del texto

¡Descarga Manual de prácticas con NumPy en Python y más Ejercicios en PDF de Programación Javascript solo en Docsity!

Selecting List Elements

Import libraries

>>> import numpy

>>> import numpy as np

Selective import

>>> from math import pi

>>> help(str)

Python For Data Science Cheat Sheet

Python Basics

Learn More Python for Data Science Interactively at www.datacamp.com

Variable Assignment

Strings

>>> x=

>>> x

>>> x+ 2 Sum of two variables

>>> x- 2 Subtraction of two variables

>>> x* 2 Multiplication of two variables

>>> x** 2 Exponentiation of a variable

>>> x% 2 Remainder of a variable

>>> x/float(2) Division of a variable

Variables and Data Types

str() '5', '3.45', 'True'

int() 5, 3, 1

float() 5.0, 1.

bool() True, True, True

Variables to strings

Variables to integers

Variables to floats

Variables to booleans

Lists

>>> a = 'is'

>>> b = 'nice'

>>> my_list = ['my', 'list', a, b]

>>> my_list2 = [[4,5,6,7], [3,4,5,6]]

Subset

>>> my_list[1]

>>> my_list[-3]

Slice

>>> my_list[1:3]

>>> my_list[1:]

>>> my_list[:3]

>>> my_list[:]

Subset Lists of Lists

>>> my_list2[1][0]

>>> my_list2[1][:2]

Also see NumPy Arrays

>>> my_list.index(a)

>>> my_list.count(a)

>>> my_list.append('!')

>>> my_list.remove('!')

>>> del(my_list[0:1])

>>> my_list.reverse()

>>> my_list.extend('!')

>>> my_list.pop(-1)

>>> my_list.insert(0,'!')

>>> my_list.sort()

Get the index of an item

Count an item

Append an item at a time

Remove an item

Remove an item

Reverse the list

Append an item

Remove an item

Insert an item

Sort the list

Index starts at 0

Select item at index 1

Select 3rd last item

Select items at index 1 and 2

Select items after index 0

Select items before index 3

Copy my_list

my_list[list][itemOfList]

Libraries

>>> my_string.upper()

>>> my_string.lower()

>>> my_string.count('w')

>>> my_string.replace('e', 'i')

>>> my_string.strip()

>>> my_string = 'thisStringIsAwesome'

>>> my_string

'thisStringIsAwesome'

Numpy Arrays

>>> my_list = [1, 2, 3, 4]

>>> my_array = np.array(my_list)

>>> my_2darray = np.array([[1,2,3],[4,5,6]])

>>> my_array.shape

>>> np.append(other_array)

>>> np.insert(my_array, 1, 5)

>>> np.delete(my_array,[1])

>>> np.mean(my_array)

>>> np.median(my_array)

>>> my_array.corrcoef()

>>> np.std(my_array)

Asking For Help

>>> my_string[3]

>>> my_string[4:9]

Subset

>>> my_array[1]

Slice

>>> my_array[0:2]

array([1, 2])

Subset 2D Numpy arrays

>>> my_2darray[:,0]

array([1, 4])

>>> my_list + my_list

['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']

>>> my_list * 2

['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']

>>> my_list2 > 4

True

>>> my_array > 3

array([False, False, False, True], dtype=bool)

>>> my_array * 2

array([2, 4, 6, 8])

>>> my_array + np.array([5, 6, 7, 8])

array([6, 8, 10, 12])

>>> my_string * 2

'thisStringIsAwesomethisStringIsAwesome'

>>> my_string + 'Innit'

'thisStringIsAwesomeInnit'

>>> 'm' in my_string

True DataCamp

Scientific computing

Data analysis

2D plotting

Machine learning

Also see Lists

Get the dimensions of the array

Append items to an array

Insert items in an array

Delete items in an array

Mean of the array

Median of the array

Correlation coefficient

Standard deviation

String to uppercase

String to lowercase

Count String elements

Replace String elements

Strip whitespaces

Select item at index 1

Select items at index 0 and 1

my_2darray[rows, columns]

Install Python

Calculations With Variables

Leading open data science platform powered by Python

Free IDE that is included with Anaconda

Create and share documents with live code, visualizations, text, ...

Types and Type Conversion

String Operations

List Operations

List Methods

Index starts at 0

String Methods

String Operations

Selecting Numpy Array Elements Index starts at 0

Numpy Array Operations

Numpy Array Functions

DataCamp

Saving/Loading Notebooks

Working with Different Programming Languages

Asking For Help

Widgets Python For Data Science Cheat Sheet

Jupyter Notebook Learn More Python for Data Science Interactively at www.DataCamp.com

Kernels provide computation and communication with front-end interfaces

like the notebooks. There are three main kernels:

Installing Jupyter Notebook will automatically install the IPython kernel.

Create new notebook

Open an existing

Make a copy of the^ notebook

current notebook

Rename notebook

Writing Code And Text

Save current notebook

and record checkpoint

Revert notebook to a

previous checkpoint

Preview of the printed

notebook

Download notebook as

  • IPython notebook
  • Python
  • HTML
  • Markdown
  • reST
  • LaTeX
  • PDF

Close notebook & stop

running any scripts

IRkernel IJulia

Cut currently selected cells

to clipboard

Copy cells from

clipboard to current

cursor position

Paste cells from

clipboard above

current cell

Paste cells from

clipboard below

Paste cells from^ current cell

clipboard on top

of current cel

Delete current cells

Revert “Delete Cells”

invocation

Split up a cell from

current cursor

position

Merge current cell

with the one above

Merge current cell

with the one below

Move current cell up Move current cell

down

Adjust metadata

underlying the

current notebook

Find and replace

in selected cells

Insert image in

selected cells

Restart kernel

Restart kernel & run

all cells

Restart kernel & run

all cells

Interrupt kernel

Interrupt kernel &

clear all output

Connect back to a

remote notebook

Run other installed

kernels

Code and text are encapsulated by 3 basic cell types: markdown cells, code

cells, and raw NBConvert cells.

Edit Cells

Insert Cells

View Cells

Notebook widgets provide the ability to visualize and control changes

in your data, often as a control like a slider, textbox, etc.

You can use them to build interactive GUIs for your notebooks or to

synchronize stateful and stateless information between Python and

JavaScript.

Toggle display of Jupyter

logo and filename

Toggle display of toolbar

Toggle line numbers

in cells

Toggle display of cell

action icons:

  • None
  • Edit metadata
  • Raw cell format
  • Slideshow
  • Attachments
  • Tags

Add new cell above the

current one

Add new cell below the

current one

Executing Cells

Run selected cell(s) Run current cells down

and create a new one

below

Run current cells down

and create a new one

above Run all cells

Save notebook

with interactive

widgets

Download serialized

state of all widget

models in use

Embed current

widgets

Walk through a UI tour

List of built-in keyboard

Edit the built-in^ shortcuts

keyboard shortcuts

Notebook help topics

Description of

markdown available

in notebook

About Jupyter Notebook

Information on

unofficial Jupyter

Notebook extensions

Python help topics

IPython help topics

NumPy help topics

SciPy help topics

Pandas help topics

SymPy help topics

Matplotlib help topics

Run all cells above the

current cell

Run all cells below

the current cell

Change the cell type of

current cell

toggle, toggle

scrolling and clear

toggle, toggle current outputs

scrolling and clear

all output

1. Save and checkpoint 2. Insert cell below 3. Cut cell 4. Copy cell(s) 5. Paste cell(s) below 6. Move cell up 7. Move cell down 8. Run current cell

9. Interrupt kernel 10. Restart kernel 11. Display characteristics 12. Open command palette 13. Current kernel 14. Kernel status 15. Log out from notebook server

Command Mode:

Edit Mode:

1 2 3 4 5 6 7 8 9 10 11 12

13 14

15

Copy attachments of

current cell

Remove cell

attachments

Paste attachments of

current cell

Python For Data Science Cheat Sheet

SciPy - Linear Algebra

Learn More Python for Data Science Interactively at www.datacamp.com

SciPy

DataCamp

Learn Python for Data Science Interactively

Interacting With NumPy Also see NumPy

The SciPy library is one of the core packages for scientific computing that provides mathematical algorithms and convenience functions built on the NumPy extension of Python.

Index Tricks

>>> np.mgrid[0:5,0:5] Create a dense meshgrid

>>> np.ogrid[0:2,0:2] Create an open meshgrid

>>> np.r_[[3,[0]*5,-1:1:10j] Stack arrays vertically (row-wise)

>>> np.c_[b,c] Create stacked column-wise arrays

Shape Manipulation

Polynomials

Vectorizing Functions

Type Handling

>>> np.angle(b,deg=True) Return the angle of the complex argument

>>> g = np.linspace(0,np.pi,num=5) Create an array of evenly spaced values

(number of samples) >>> g [3:] += np.pi

>>> np.unwrap(g) Unwrap

>>> np.logspace(0,10,3) Create an array of evenly spaced values (log scale)

>>> np.select([c<4],[c*2]) Return values from a list of arrays depending on

conditions

>>> misc.factorial(a) Factorial

>>> misc.comb(10,3,exact=True) Combine N things taken at k time

>>> misc.central_diff_weights(3) Weights for Np-point central derivative

>>> misc.derivative(myfunc,1.0) Find the n-th derivative of a function at a point

Other Useful Functions

>>> np.real(c) Return the real part of the array elements

>>> np.imag(c) Return the imaginary part of the array elements

>>> np.real_if_close(c,tol=1000) Return a real array if complex parts close to 0

>>> np.cast'f' Cast object to a data type

>>> def myfunc(a): if a < 0: return a* else: return a/

>>> np.vectorize(myfunc) Vectorize functions

>>> from numpy import poly1d

>>> p = poly1d([3,4,5]) Create a polynomial object

>>> np.transpose(b) Permute array dimensions

>>> b.flatten() Flatten the array

>>> np.hstack((b,c)) Stack arrays horizontally (column-wise)

>>> np.vstack((a,b)) Stack arrays vertically (row-wise)

>>> np.hsplit(c,2) Split the array horizontally at the 2nd index

>>> np.vpslit(d,2) Split the array vertically at the 2nd index

>>> import numpy as np >>> a = np.array([1,2,3]) >>> b = np.array([(1+5j,2j,3j), (4j,5j,6j)]) >>> c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]])

>>> help(scipy.linalg.diagsvd) >>> np.info(np.matrix)

Linear Algebra

You’ll use the linalg and sparse modules. Note that scipy.linalg contains and expands on numpy.linalg.

>>> from scipy import linalg, sparse

Creating Matrices

>>> A = np.matrix(np.random.random((2,2))) >>> B = np.asmatrix(b) >>> C = np.mat(np.random.random((10,5))) >>> D = np.mat([[3,4], [5,6]])

Also see NumPy

Basic Matrix Routines

Inverse

>>> A.I Inverse

>>> linalg.inv(A) Inverse

>>> A.T Tranpose matrix

>>> A.H Conjugate transposition

>>> np.trace(A) Trace

Norm

>>> linalg.norm(A) Frobenius norm

>>> linalg.norm(A,1) L1 norm (max column sum)

>>> linalg.norm(A,np.inf) L inf norm (max row sum)

Rank

>>> np.linalg.matrix_rank(C) Matrix rank

Determinant

>>> linalg.det(A) Determinant

Solving linear problems

>>> linalg.solve(A,b) Solver for dense matrices

>>> E = np.mat(a).T Solver for dense matrices

>>> linalg.lstsq(D,E) Least-squares solution to linear matrix

equation

Generalized inverse

>>> linalg.pinv(C) Compute the pseudo-inverse of a matrix

(least-squares solver)

>>> linalg.pinv2(C) Compute the pseudo-inverse of a matrix

(SVD)

Addition

>>> np.add(A,D) Addition

Subtraction

>>> np.subtract(A,D) Subtraction

Division

>>> np.divide(A,D) Division

Multiplication

>>> np.multiply(D,A) Multiplication

>>> np.dot(A,D) Dot product

>>> np.vdot(A,D) Vector dot product

>>> np.inner(A,D) Inner product

>>> np.outer(A,D) Outer product

>>> np.tensordot(A,D) Tensor dot product

>>> np.kron(A,D) Kronecker product

Exponential Functions

>>> linalg.expm(A) Matrix exponential

>>> linalg.expm2(A) Matrix exponential (Taylor Series)

>>> linalg.expm3(D) Matrix exponential (eigenvalue

decomposition) Logarithm Function

>>> linalg.logm(A) Matrix logarithm

Trigonometric Tunctions

>>> linalg.sinm(D) Matrix sine

>>> linalg.cosm(D) Matrix cosine

>>> linalg.tanm(A) Matrix tangent

Hyperbolic Trigonometric Functions

>>> linalg.sinhm(D) Hypberbolic matrix sine

>>> linalg.coshm(D) Hyperbolic matrix cosine

>>> linalg.tanhm(A) Hyperbolic matrix tangent

Matrix Sign Function

>>> np.sigm(A) Matrix sign function

Matrix Square Root

>>> linalg.sqrtm(A) Matrix square root

Arbitrary Functions

>>> linalg.funm(A, lambda x: x*x) Evaluate matrix function

Matrix Functions

Asking For Help

Decompositions

Eigenvalues and Eigenvectors

>>> la, v = linalg.eig(A) Solve ordinary or generalized

eigenvalue problem for square matrix

>>> l1, l2 = la Unpack eigenvalues

>>> v[:,0] First eigenvector

>>> v[:,1] Second eigenvector

>>> linalg.eigvals(A) Unpack eigenvalues

Singular Value Decomposition

>>> U,s,Vh = linalg.svd(B) Singular Value Decomposition (SVD)

>>> M,N = B.shape

>>> Sig = linalg.diagsvd(s,M,N) Construct sigma matrix in SVD

LU Decomposition

>>> P,L,U = linalg.lu(C) LU Decomposition

>>> F = np.eye(3, k=1) Create a 2X2 identity matrix >>> G = np.mat(np.identity(2)) Create a 2x2 identity matrix >>> C[C > 0.5] = 0 >>> H = sparse.csr_matrix(C) Compressed Sparse Row matrix >>> I = sparse.csc_matrix(D) Compressed Sparse Column matrix >>> J = sparse.dok_matrix(A) Dictionary Of Keys matrix >>> E.todense() Sparse matrix to full matrix >>> sparse.isspmatrix_csc(A) Identify sparse matrix

Creating Sparse Matrices

Inverse >>> sparse.linalg.inv(I) Inverse Norm >>> sparse.linalg.norm(I) Norm Solving linear problems >>> sparse.linalg.spsolve(H,I) Solver for sparse matrices

Sparse Matrix Routines

Sparse Matrix Functions

>>> sparse.linalg.expm(I) Sparse matrix exponential

Sparse Matrix Decompositions

>>> la, v = sparse.linalg.eigs(F,1) Eigenvalues and eigenvectors

>>> sparse.linalg.svds(H, 2) SVD

Python For Data Science Cheat Sheet

Pandas Basics

Learn Python for Data Science Interactively at www.DataCamp.com

Pandas

DataCamp

Series

DataFrame

d

c

b

A one-dimensional labeled array a capable of holding any data type

Index

Index

Columns

A two-dimensional labeled data structure with columns of potentially different types

The Pandas library is built on NumPy and provides easy-to-use data structures and data analysis tools for the Python programming language.

>>> import pandas as pd

Use the following import convention:

Pandas Data Structures

>>> s = pd.Series([3, -5, 7, 4], index=['a', 'b', 'c', 'd'])

>>> data = {'Country': ['Belgium', 'India', 'Brazil'], 'Capital': ['Brussels', 'New Delhi', 'Brasília'], 'Population': [11190846, 1303171035, 207847528]}

>>> df = pd.DataFrame(data, columns=['Country', 'Capital', 'Population'])

Selection

>>> s['b'] Get one element

>>> df[1:] Get subset of a DataFrame

Country Capital Population 1 India New Delhi 1303171035 2 Brazil Brasília 207847528

By Position

>>> df.iloc([0],[0]) Select single value by row &

'Belgium' column

>>> df.iat([0],[0])

'Belgium'

By Label

>>> df.loc([0], ['Country']) Select single value by row &

'Belgium' column labels

>>> df.at([0], ['Country'])

'Belgium'

By Label/Position

>>> df.ix[2] Select single row of

Country Brazil subset of rows

Capital Brasília Population 207847528

>>> df.ix[:,'Capital'] Select a single column of

0 Brussels subset of columns

1 New Delhi 2 Brasília

>>> df.ix[1,'Capital'] Select rows and columns

'New Delhi'

Boolean Indexing

>>> s[~(s > 1)] Series s where value is not >

>>> s[(s < -1) | (s > 2)] s where value is <-1 or >

>>> df[df['Population']>1200000000] Use filter to adjust DataFrame

Setting

>>> s['a'] = 6 Set index a of Series s to 6

Applying Functions

>>> f = lambda x: x*

>>> df.apply(f) Apply function

>>> df.applymap(f) Apply function element-wise

Retrieving Series/DataFrame Information

>>> df.shape (rows,columns)

>>> df.index Describe index

>>> df.columns Describe DataFrame columns

>>> df.info() Info on DataFrame

>>> df.count() Number of non-NA values

Getting

Also see NumPy Arrays

Selecting, Boolean Indexing & Setting

Basic Information

Summary

>>> df.sum() Sum of values

>>> df.cumsum() Cummulative sum of values

>>> df.min()/df.max() Minimum/maximum values

>>> df.idxmin()/df.idxmax() Minimum/Maximum index value

>>> df.describe() Summary statistics

>>> df.mean() Mean of values

>>> df.median() Median of values

Dropping

>>> s.drop(['a', 'c']) Drop values from rows (axis=0)

>>> df.drop('Country', axis=1) Drop values from columns(axis=1)

Data Alignment

>>> s.add(s3, fill_value=0)

a 10. b -5. c 5. d 7.

>>> s.sub(s3, fill_value=2)

>>> s.div(s3, fill_value=4)

>>> s.mul(s3, fill_value=3)

>>> s3 = pd.Series([7, -2, 3], index=['a', 'c', 'd'])

>>> s + s

a 10. b NaN c 5. d 7.

Arithmetic Operations with Fill Methods

Internal Data Alignment

NA values are introduced in the indices that don’t overlap:

You can also do the internal data alignment yourself with the help of the fill methods:

Sort & Rank

>>> df.sort_index() Sort by labels along an axis

>>> df.sort_values(by='Country') Sort by the values along an axis

>>> df.rank() Assign ranks to entries

Belgium Brussels

India New Delhi

Brazil Brasília

Country Capital

11190846

1303171035

207847528

Population

I/O

Read and Write to CSV

>>> pd.read_csv('file.csv', header=None, nrows=5)

>>> df.to_csv('myDataFrame.csv')

Read and Write to Excel

>>> pd.read_excel('file.xlsx')

>>> pd.to_excel('dir/myDataFrame.xlsx', sheet_name='Sheet1')

Read multiple sheets from the same file

>>> xlsx = pd.ExcelFile('file.xls')

>>> df = pd.read_excel(xlsx, 'Sheet1')

>>> help(pd.Series.loc)

Asking For Help

Read and Write to SQL Query or Database Table

>>> from sqlalchemy import create_engine

>>> engine = create_engine('sqlite:///:memory:')

>>> pd.read_sql("SELECT * FROM my_table;", engine)

>>> pd.read_sql_table('my_table', engine)

>>> pd.read_sql_query("SELECT * FROM my_table;", engine)

>>> pd.to_sql('myDf', engine)

read_sql()is a convenience wrapper around read_sql_table() and

read_sql_query()

Python For Data Science Cheat Sheet

Matplotlib

Learn Python Interactively at www.DataCamp.com

Matplotlib

DataCamp Learn Python for Data Science Interactively

Prepare The Data Also see^ Lists^ &^ NumPy

Matplotlib is a Python 2D plotting library which produces publication-quality figures in a variety of hardcopy formats and interactive environments across platforms.

>>> import numpy as np >>> x = np.linspace(0, 10, 100) >>> y = np.cos(x) >>> z = np.sin(x)

Show Plot

>>> plt.show()

Matplotlib 2.0.0 - Updated on: 02/

Save Plot

Save figures

>>> plt.savefig('foo.png')

Save transparent figures

>>> plt.savefig('foo.png', transparent=True)

>>> fig = plt.figure() >>> fig2 = plt.figure(figsize=plt.figaspect(2.0))

2^ Create Plot

Plot Anatomy & Workflow

All plotting is done with respect to an Axes. In most cases, a

subplot will fit your needs. A subplot is an axes on a grid system.

>>> fig.add_axes() >>> ax1 = fig.add_subplot(221) # row-col-num >>> ax3 = fig.add_subplot(212) >>> fig3, axes = plt.subplots(nrows=2,ncols=2) >>> fig4, axes2 = plt.subplots(ncols=3)

Customize Plot

Colors, Color Bars & Color Maps

Markers

Linestyles

Mathtext

Text & Annotations

Limits, Legends & Layouts

The basic steps to creating plots with matplotlib are:

1 Prepare data 2 Create plot 3 Plot 4 Customize plot 5 Save plot 6 Show plot

>>> import matplotlib.pyplot as plt >>> x = [1,2,3,4] >>> y = [10,20,25,30] >>> fig = plt.figure() >>> ax = fig.add_subplot(111) >>> ax.plot(x, y, color='lightblue', linewidth=3) >>> ax.scatter([2,4,6], [5,15,25], color='darkgreen', marker='^') >>> ax.set_xlim(1, 6.5) >>> plt.savefig('foo.png') >>> plt.show()

Step 3, 4

Step 2

Step 1

Step 3

Step 6

Plot Anatomy Workflow

Limits & Autoscaling

>>> ax.margins(x=0.0,y=0.1) Add padding to a plot >>> ax.axis('equal') Set the aspect ratio of the plot to 1 >>> ax.set(xlim=[0,10.5],ylim=[-1.5,1.5]) Set limits for x-and y-axis >>> ax.set_xlim(0,10.5) Set limits for x-axis

Legends

>>> ax.set(title='An Example Axes', Set a title and x-and y-axis labels ylabel='Y-Axis', xlabel='X-Axis') >>> ax.legend(loc='best') No overlapping plot elements

Ticks

>>> ax.xaxis.set(ticks=range(1,5), Manually set x-ticks ticklabels=[3,100,-12,"foo"]) >>> ax.tick_params(axis='y', Make y-ticks longer and go in and out direction='inout', length=10)

Subplot Spacing

>>> fig3.subplots_adjust(wspace=0.5, Adjust the spacing between subplots hspace=0.3, left=0.125, right=0.9, top=0.9, bottom=0.1) >>> fig.tight_layout() (^) Fit subplot(s) in to the figure area

Axis Spines

>>> ax1.spines['top'].set_visible(False) Make the top axis line for a plot invisible >>> ax1.spines['bottom'].set_position(('outward',10)) Move the bottom axis line outward

Figure

Axes

>>> data = 2 * np.random.random((10, 10)) >>> data2 = 3 * np.random.random((10, 10)) >>> Y, X = np.mgrid[-3:3:100j, -3:3:100j] >>> U = -1 - X2 + Y >>> V = 1 + X - Y >>> from matplotlib.cbook import get_sample_data >>> img = np.load(get_sample_data('axes_grid/bivariate_normal.npy'))

>>> fig, ax = plt.subplots() >>> lines = ax.plot(x,y) Draw points with lines or markers connecting them >>> ax.scatter(x,y) Draw unconnected points, scaled or colored >>> axes[0,0].bar([1,2,3],[3,4,5]) Plot vertical rectangles (constant width) >>> axes[1,0].barh([0.5,1,2.5],[0,1,2]) Plot horiontal rectangles (constant height) >>> axes[1,1].axhline(0.45) Draw a horizontal line across axes >>> axes[0,1].axvline(0.65) Draw a vertical line across axes >>> ax.fill(x,y,color='blue') Draw filled polygons >>> ax.fill_between(x,y,color='yellow') Fill between y-values and 0

3 Plotting Routines

1D Data

>>> fig, ax = plt.subplots() >>> im = ax.imshow(img, Colormapped or RGB arrays cmap='gist_earth', interpolation='nearest', vmin=-2, vmax=2)

2D Data or Images

Vector Fields

>>> axes[0,1].arrow(0,0,0.5,0.5) Add an arrow to the axes >>> axes[1,1].quiver(y,z) Plot a 2D field of arrows >>> axes[0,1].streamplot(X,Y,U,V) Plot a 2D field of arrows

Data Distributions

>>> ax1.hist(y) Plot a histogram >>> ax3.boxplot(y) Make a box and whisker plot >>> ax3.violinplot(z) Make a violin plot

>>> axes2[0].pcolor(data2) Pseudocolor plot of 2D array >>> axes2[0].pcolormesh(data) Pseudocolor plot of 2D array >>> CS = plt.contour(Y,X,U) Plot contours >>> axes2[2].contourf(data1) Plot filled contours >>> axes2[2]= ax.clabel(CS) Label a contour plot

Figure

Axes/Subplot

Y-axis

X-axis

1D Data

2D Data or Images

>>> plt.plot(x, x, x, x2, x, x3) >>> ax.plot(x, y, alpha = 0.4) >>> ax.plot(x, y, c='k') >>> fig.colorbar(im, orientation='horizontal') >>> im = ax.imshow(img, cmap='seismic')

>>> fig, ax = plt.subplots() >>> ax.scatter(x,y,marker=".") >>> ax.plot(x,y,marker="o")

>>> plt.title(r'$sigma_i=15$', fontsize=20)

>>> ax.text(1, -2.1, 'Example Graph', style='italic') >>> ax.annotate("Sine", xy=(8, 0), xycoords='data', xytext=(10.5, 0), textcoords='data', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"),)

>>> plt.plot(x,y,linewidth=4.0) >>> plt.plot(x,y,ls='solid') >>> plt.plot(x,y,ls='--') >>> plt.plot(x,y,'--',x2,y2,'-.') >>> plt.setp(lines,color='r',linewidth=4.0)

>>> import matplotlib.pyplot as plt

Close & Clear

>>> plt.cla() Clear an axis >>> plt.clf() Clear the entire figure >>> plt.close() Close a window

Python For Data Science Cheat Sheet

Seaborn

Learn Data Science Interactively at www.DataCamp.com

Statistical Data Visualization With Seaborn

DataCamp

Figure Aesthetics

Data

The Python visualization library Seaborn is based on matplotlib and provides a high-level interface for drawing attractive statistical graphics.

Make use of the following aliases to import the libraries:

The basic steps to creating plots with Seaborn are:

  1. Prepare some data
  2. Control figure aesthetics
  3. Plot with Seaborn
  4. Further customize your plot

>>> import pandas as pd >>> import numpy as np >>> uniform_data = np.random.rand(10, 12) >>> data = pd.DataFrame({'x':np.arange(1,101), 'y':np.random.normal(0,4,100)})

>>> import matplotlib.pyplot as plt >>> import seaborn as sns

Plotting With Seaborn

>>> import matplotlib.pyplot as plt >>> import seaborn as sns >>> tips = sns.load_dataset("tips") >>> sns.set_style("whitegrid") >>> g = sns.lmplot(x="tip", y="total_bill", data=tips, aspect=2) >>> g = (g.set_axis_labels("Tip","Total bill(USD)"). set(xlim=(0,10),ylim=(0,100))) >>> plt.title("title") >>> plt.show(g)

Step 4

Step 2

Step 1

Step 5

Step 3

>>> titanic = sns.load_dataset("titanic") >>> iris = sns.load_dataset("iris")

Seaborn also offers built-in data sets:

4^ Further Customizations

Show or Save Plot

>>> sns.set() (Re)set the seaborn default

>>> sns.set_style("whitegrid") Set the matplotlib parameters

>>> sns.set_style("ticks", Set the matplotlib parameters

{"xtick.major.size":8, "ytick.major.size":8})

>>> sns.axes_style("whitegrid") Return a dict of params or use with

with to temporarily set the style

Axis Grids

>>> f, ax = plt.subplots(figsize=(5,6)) Create a figure and one subplot

>>> plt.title("A Title") Add plot title

>>> plt.ylabel("Survived") Adjust the label of the y-axis

>>> plt.xlabel("Sex") Adjust the label of the x-axis

>>> plt.ylim(0,100) Adjust the limits of the y-axis

>>> plt.xlim(0,10) Adjust the limits of the x-axis

>>> plt.setp(ax,yticks=[0,5]) Adjust a plot property

>>> plt.tight_layout() Adjust subplot params

>>> plt.show() Show the plot

>>> plt.savefig("foo.png") Save the plot as a figure

>>> plt.savefig("foo.png", Save transparent figure

transparent=True)

>>> sns.regplot(x="sepal_width", Plot data and a linear regression

y="sepal_length", model fit

data=iris, ax=ax)

>>> g.despine(left=True) Remove left spine

>>> g.set_ylabels("Survived") Set the labels of the y-axis

>>> g.set_xticklabels(rotation=45) Set the tick labels for x

>>> g.set_axis_labels("Survived", Set the axis labels

"Sex")

>>> h.set(xlim=(0,5), Set the limit and ticks of the

ylim=(0,5), x-and y-axis

xticks=[0,2.5,5], yticks=[0,2.5,5])

Close & Clear

>>> plt.cla() Clear an axis

>>> plt.clf() Clear an entire figure

>>> plt.close() Close a window

Also see Lists, NumPy & Pandas

Also see Matplotlib

Also see Matplotlib

Also see Matplotlib

Also see Matplotlib

Context Functions

>>> sns.set_context("talk") Set context to "talk"

>>> sns.set_context("notebook", Set context to "notebook",

font_scale=1.5, Scale font elements and

rc={"lines.linewidth":2.5}) override param mapping

Seaborn styles

>>> sns.set_palette("husl",3) Define the color palette

>>> sns.color_palette("husl") Use with with to temporarily set palette

>>> flatui = ["#9b59b6","#3498db","#95a5a6","#e74c3c","#34495e","#2ecc71"]

>>> sns.set_palette(flatui) Set your own color palette

Color Palette

Plot

Axisgrid Objects

>>> g = sns.FacetGrid(titanic, Subplot grid for plotting conditional

col="survived", relationships

row="sex") >>> g = g.map(plt.hist,"age")

>>> sns.factorplot(x="pclass", Draw a categorical plot onto a

y="survived", Facetgrid

hue="sex", data=titanic)

>>> sns.lmplot(x="sepal_width", Plot data and regression model fits

y="sepal_length", across a FacetGrid

hue="species", data=iris)

Categorical Plots^ Regression Plots

Scatterplot

>>> sns.stripplot(x="species", Scatterplot with one

y="petal_length", categorical variable

data=iris)

>>> sns.swarmplot(x="species", Categorical scatterplot with

y="petal_length", non-overlapping points

data=iris)

Bar Chart

>>> sns.barplot(x="sex", Show point estimates and

y="survived", confidence intervals with

hue="class", scatterplot glyphs

data=titanic)

Count Plot

>>> sns.countplot(x="deck", Show count of observations

data=titanic, palette="Greens_d")

Point Plot

>>> sns.pointplot(x="class", Show point estimates and

y="survived", confidence intervals as

hue="sex", rectangular bars

data=titanic, palette={"male":"g", "female":"m"}, markers=["^","o"], linestyles=["-","--"])

Boxplot

>>> sns.boxplot(x="alive", Boxplot

y="age", hue="adult_male", data=titanic)

>>> sns.boxplot(data=iris,orient="h") Boxplot with wide-form data

Violinplot

>>> sns.violinplot(x="age", Violin plot

y="sex", hue="survived", data=titanic)

>>> plot = sns.distplot(data.y, Plot univariate distribution

kde=False, color="b")

Distribution Plots

>>> h = sns.PairGrid(iris) Subplot grid for plotting pairwise

>>> h = h.map(plt.scatter) relationships

>>> sns.pairplot(iris) Plot pairwise bivariate distributions

>>> i = sns.JointGrid(x="x", Grid for bivariate plot with marginal

y="y", univariate plots

data=data) >>> i = i.plot(sns.regplot, sns.distplot)

>>> sns.jointplot("sepal_length", Plot bivariate distribution

"sepal_width", data=iris, kind='kde')

Matrix Plots

>>> sns.heatmap(uniform_data,vmin=0,vmax=1) Heatmap