Bokeh Cheat Sheet Python For Data Science: 3 Renderers & Visual Customizations
Bokeh Cheat Sheet Python For Data Science: 3 Renderers & Visual Customizations
fill_color='white')
Outside Plot Area
>>> [Link]([Link]([1.5,3.5,5.5]), [1,4,3],
r1 = [Link]([Link]([1,2,3]), [Link]([3,2,1])
[Link]([[3,4,5],[3,2,1]]),
Bokeh’s mid-level general purpose [Link] interface is centered Legend Background & Border
around two main components: data
and glyphs. Selection and Non-Selection Glyphs
>>> p = figure(tools='box_select')
>>> [Link].border_line_color = "navy"
nonselection_alpha=0.1)
Rows & Columns Layout
Hover Glyphs
>>> from [Link] import HoverTool
Rows
>>> hover = HoverTool(tooltips=None, mode='vline')
>>> from [Link] import row
The basic steps to creating plots with the [Link]
interface are:
>>> p3.add_tools(hover) >>> layout = row(p1,p2,p3)
1. Prepare some data (Python lists, NumPy arrays, Pandas DataFrames and other sequences of values)
Columns
2. Create a new plot
>>> from [Link] import columns
color=dict(field='origin',
Grid Layout
>>> y = [6, 7, 2, 4, 5]
transform=color_mapper),
x_axis_label='x',
>>> row1 = [p1,p2]
y_axis_label='y')
>>> row2 = [p3]
>>>
>>>
>>>
from [Link] import Panel, Tabs
Linked Brushing
>>> from [Link] import output_file, show
>>> p4 = figure(plot_width = 100, tools='box_select,lasso_select')
data2,
Query
how='left',
on='X1')
data2,
on='X1')
columns='Type',
DataFrame columns={"Country":"cntry",
how='inner',
"Capital":"cptl",
on='X1')
"Population":"ppltn"})
>>> [Link](data1,
Reindexing data2,
how='outer',
on='X1')
Pivot Table >>> s2 = [Link](['a','c','d','e','b'])
columns values='Value',
>>> [Link](range(4),
>>> s3 = [Link](range(5),
index='Date',
method='ffill') method='bfill') Join
columns='Type']) Country Capital Population
0 3
[Link]([5,4,3])]
Horizontal/Vertical
>>> df5 = [Link]([Link](3, 2), index=arrays)
names=['first', 'second'])
> Dates
> Duplicate Data
>>> [Link](df2, #Gather columns into rows
id_vars=["Date"],
value_vars=["Type", "Value"],
>>> df2['Date']= pd.to_datetime(df2['Date'])
>>> [Link](level=0).sum()
[Link]/courses/data-science-for-business [Link]/groups/business
Exploration and Visualization Experimentation and Prediction
The type of dashboard you should use depends on what you’ll be using it for. Machine Learning
Common Dashboard Elements Machine learning is an application of artificial intelligence (AI) that builds
algorithms and statistical models to train data to address specific questions
Type What is it best for? Example without explicit instructions.
Stacked bar chart Tracking composition over time Example Recommendation systems, email Image segmentation,
subject optimization, churn customer segmentation
prediction
Excel Power BI R Shiny Time Series Forecasting is a technique for predicting events through a
sequence of time and can capture seasonality or periodic events.
Sheets Tableau [Link]
Natural Language Processing (NLP) allows computers to process and analyze
Looker
large amounts of natural language data.
- Text as input data
- Word counts track the important words in a text
When You Should Request a Dashboard - Word embeddings create features that group similar words
When you’ll use it multiple times Deep Learning / Neural Networks enables Explainable AI is an emerging field in
unsupervised machine learning using data machine learning that applies AI such
that is unstructured or unlabeled. that results can be easily understood.
[Link]/courses/data-science-for-business [Link]/groups/business
> Model Architecture > Inspect Model
Sequential Model
Python For Data Science
>>> [Link](Dense(12,
metrics=['accuracy'])
input_dim=8,
Keras activation='relu'))
>>> [Link](Dense(8,kernel_initializer='uniform',activation='relu'))
>>> [Link](optimizer='rmsprop',
loss='categorical_crossentropy',
>>> [Link](Dense(512,activation='relu',input_shape=(784,)))
loss='mse',
>>> [Link](Dense(32,
activation='relu',
Convolutional Neural Network (CNN) > Model Training
input_dim=100))
>>> [Link](optimizer='rmsprop',
>>> [Link](Conv2D(32,(3,3),padding='same',input_shape=x_train.shape[1:]))
y_train4,
loss='binary_crossentropy',
>>> [Link](Activation('relu'))
batch_size=32,
metrics=['accuracy'])
>>> [Link](Conv2D(32,(3,3)))
epochs=15,
>>> [Link](data,labels,epochs=10,batch_size=32)
>>> [Link](Activation('relu'))
verbose=1,
> Data
>>> [Link](Activation('relu'))
>>>
>>>
[Link](Conv2D(64,(3, 3)))
[Link](Activation('relu'))
> Evaluate Your Model's Performance
>>> [Link](MaxPooling2D(pool_size=(2,2)))
Your data needs to be stored as NumPy arrays or as a list of NumPy arrays. Ideally, you split the data in training and >>> [Link](Dropout(0.25))
>>> score = [Link](x_test,
test sets, for which you can also resort to the train_test_split module of sklearn.cross_validation. >>> [Link](Flatten())
y_test,
>>> [Link](Dense(512))
batch_size=32)
>>> [Link](Activation('relu'))
>>> [Link](Dense(num_classes))
>>>
>>>
from [Link] import boston_housing, mnist,
cifar10, imdb
(x_train,y_train),(x_test,y_test) = mnist.load_data()
>>> [Link](Activation('softmax'))
> Save/ Reload Models
>>>
>>>
(x_train2,y_train2),(x_test2,y_test2) = boston_housing.load_data()
(x_train3,y_train3),(x_test3,y_test3) = cifar10.load_data()
>>> [Link](LSTM(128,dropout=0.2,recurrent_dropout=0.2))
>>> [Link](loss='categorical_crossentropy',
optimizer=opt,
Early Stopping
Sequence Padding Train and Test Sets
>>> from [Link] import EarlyStopping
random_state=42) batch_size=32,
epochs=15,
Standardization/Normalization callbacks=[early_stopping_monitor])
>>> from [Link] import to_categorical
>>> Y_test3 = to_categorical(y_test3, num_classes) >>> standardized_X_test = [Link](x_test2) Learn Data Skills Online at [Link]
> Plotting Routines > Plotting Cutomize Plot
>>> [Link](x,y,marker=".")
>>> [Link](x,y,marker="o")
>>> y = [Link](x)
>>> axes[0,1].streamplot(X,Y,U,V) #Plot a 2D field of arrows style='italic')
xy=(8, 0),
textcoords='data',
>>> U = -1 - X**2 + Y
Mathtext
>>> V = 1 + X - Y**2
>>>
>>>
from [Link] import get_sample_data
img = [Link](get_sample_data('axes_grid/bivariate_normal.npy')) > Plot Anatomy & Workflow >>> [Link](r'$sigma_i=15$', fontsize=20)
Legends
>>> fig = [Link]()
>>> [Link](title='An Example Axes', #Set a title and x-and y-axis labels
xlabel='X-Axis')
>>> fig.add_axes()
>>> x = [1,2,3,4] #Step 1
direction='inout',
>>> [Link]([2,4,6],
>>> fig3.subplots_adjust(wspace=0.5, #Adjust the spacing between subplots
[5,15,25],
hspace=0.3,
left=0.125,
marker='^')
right=0.9,
>>> [Link]['top'].set_visible(False) #Make the top axis line for a plot invisible
> Show Plot > Close and Clear >>> [Link]['bottom'].set_position(('outward',10)) #Move the bottom axis line outward
>>> [Link]()
>>> [Link]() #Clear the entire figure
>>>
>>>
[Link] #Name of data type
> Data Types >>> a[2] #Select the element at the 2nd index
1.5 2
2 3
3
6.0 4 5 6
>>> np.int64 #Signed 64-bit integer types
Numpy
>>> np.float32 #Standard double-precision floating point
Slicing
>>> [Link] #Complex numbers represented by 128 floats
>>> a[0:2] #Select items at index 0 and 1
1 2 3
>>> Numpy
[Link] #Boolean type storing TRUE and FALSE values
array([1, 2])
It provides a high-performance multidimensional array object, and tools for array([[1.5, 2., 3.]])
1.5 2 3
4 5 6
working with these arrays >>> c[1,...] #Same as [1,:,:]
>>> g = a - b #Subtraction
Fancy Indexing
array([[-0.5, 0. , 0. ],
array([ 4. , 2. , 6. , 1.5])
>>> b[[1, 0, 1, 0]][:,[0,1,2,0]] #Select a subset of the matrix’s rows and columns
>>> b + a #Addition
array([[ 4. ,5. , 6. , 4. ],
array([[ 2.5, 4. , 6. ],
[ 1.5, 2. , 3. , 1.5],
[ 5. , 7. , 9. ]])
[ 4. , 5. , 6. , 4. ],
[ 1.5, 2. , 3. , 1.5]])
>>> a / b #Division
array([[ 0.66666667, 1. , 1. ],
>>> a * b #Multiplication
> Array Manipulation
> Creating Arrays
array([[ 1.5, 4. , 9. ],
>>> a = [Link]([1,2,3])
>>> [Link](b) #Square root
>>> i.T #Permute array dimensions
>>> b = [Link]([(1.5,2,3), (4,5,6)], dtype = float)
>>> [Link](a) #Print sines of an array
>>> c = [Link]([[(1.5,2,3), (4,5,6)],[(3,2,1), (4,5,6)]], dtype = float) >>> [Link](b) #Element-wise cosine
Changing Array Shape
>>> [Link](a) #Element-wise natural logarithm
>>> [Link]() #Flatten the array
[ 4. , 5. , 6. ]])
>>> [Link]('my_array', a)
>>> [Link](axis=1) #Cumulative sum of the elements
[ 3, 20]])
>>> [Link]('[Link]', a, b)
>>> [Link]() #Mean
>>> np.c_[a,d] #Create stacked column-wise arrays
>>> [Link]('my_array.npy') >>> [Link](b) #Median
[array([[[ 1.5, 2. , 1. ],
>>> [Link]("[Link]")
>>> df.to_csv('[Link]')
>>>
>>>
>>>
[Link] #(rows,columns)
Learn Pandas Basics online at [Link] Read and Write to Excel >>> [Link]() #Number of non-NA values
>>> pd.read_excel(‘[Link]’)
Pandas
>>>
>>> [Link]() #Cummulative sum of values
Use the following import convention: >>> from sqlalchemy import create_engine
pd.read_sql_table('my_table', engine)
> Applying Functions
>>> pd.read_sql_query("SELECT * FROM my_table;", engine)
read_sql() is a convenience wrapper around read_sql_table() and
read_sql_query() >>> f = lambda x: x*2
> Pandas Data Structures >>> df.to_sql('myDf', engine) >>> [Link](f) #Apply function
Series
> Selection Also see NumPy Arrays
> Data Alignment
A one-dimensional labeled array
a 3
capable of holding any data type b -5 Getting Internal Data Alignment
Index
c 7 >>> s['b'] #Get one element
>>> s = [Link]([3, -5, 7, 4], index=['a', 'b', 'c', 'd']) >>> df[1:] #Get subset of a DataFrame
>>> s3 = [Link]([7, -2, 3], index=['a', 'c', 'd'])
c 5.0
You can also do the internal data alignment yourself with
the help of the fill methods:
Index 1 India New Delhi 1303171035 >>> [Link]([0],[0])
>>> [Link](s3, fill_values=0)
'Belgium' a 10.0
By Label
>>> data = {'Country': ['Belgium', 'India', 'Brazil'],
c 5.0
>>> df = [Link](data,
>>> [Link]([0], ['Country'])
>>> [Link](s3, fill_value=4)
By Label/Position
> Dropping
>>> [Link][2] #Select single row of subset of rows
Country Brazil
Capital Brasília
Population 207847528
1 New Delhi
2 Brasília
Boolean Indexing
>>> help([Link]) >>> s[~(s > 1)] #Series s where value is not >1
>>> s[(s < -1) | (s > 2)] #s where value is <-1 or >2
>>> my_string
>>>
>>>
a = 'is'
b = 'nice'
String Operations
Selecting List Elements Index starts at 0
Learn Python Basics online at [Link] >>> my_string * 2
'thisStringIsAwesomethisStringIsAwesome'
Subset
>>> my_string + 'Innit'
'thisStringIsAwesomeInnit'
>>> my_list[1]
#Select item at index 1
>>> x=5
>>> my_list2[1][:2]
>>> x
5
String Methods
>>> my_string.upper()
#String to uppercase
List Operations
Calculations With Variables >>>
>>>
my_string.lower()
#String to lowercase
True
>>> x*2 #Multiplication of two variables
10
25
1
>>> my_list.index(a) #Get the index of an item
Types and Type Conversion Selecting Numpy Array Elements Index starts at 0 >>>
>>>
del(my_list[0:1])
#Remove an item
str()
>>> my_list.insert(0,'!')
#Insert an item
'5', '3.45', 'True' #Variables to strings >>> my_array[1] #Select item at index 1
>>> my_list.sort() #Sort the list
2
int()
Slice
5, 3, 1 #Variables to integers
>>> my_array[0:2]
#Select items at index 0 and 1
float()
5.0, 1.0 #Variables to floats
array([1, 2])
array([1, 4])
True, True, True #Variables to booleans
Leading open data science
Free IDE that is included
Create and share
Numpy Array Operations platform powered by Python with Anaconda documents with live code
>>> my_array * 2
array([2, 4, 6, 8])
Data analysis Scientific computing 2D plotting Machine learning array([6, 8, 10, 12]) >>> help(str)
1
Boxplot yticks=[0,2.5,5])
Data Also see Lists, NumPy & Pandas >>> [Link](x="alive", Boxplot
Plot
y="age",
>>> import pandas as pd hue="adult_male",
>>> import numpy as np >>> [Link]("A Title") Add plot title
data=titanic)
>>> uniform_data = [Link](10, 12) >>> [Link]("Survived") Adjust the label of the y-axis
>>> [Link](data=iris,orient="h") Boxplot with wide-form data
>>> data = [Link]({'x':[Link](1,101), >>> [Link]("Sex") Adjust the label of the x-axis
'y':[Link](0,4,100)}) Violinplot >>> [Link](0,100) Adjust the limits of the y-axis
>>> [Link](x="age", Violin plot >>> [Link](0,10) Adjust the limits of the x-axis
Seaborn also offers built-in data sets: y="sex", >>> [Link](ax,yticks=[0,5]) Adjust a plot property
>>> titanic = sns.load_dataset("titanic") hue="survived", >>> plt.tight_layout() Adjust subplot params
>>> iris = sns.load_dataset("iris") data=titanic)
Python lists, NumPy arrays, Pandas DataFrames and other sequences of values
2. Create a new plot
>>> color_mapper = CategoricalColorMapper(
factors=['US', 'Asia', 'Europe'],
palette=['blue', 'red', 'green'])
4 Output & Export
3. Add renderers for your data, with visual customizations >>> [Link]('mpg', 'cyl', source=cds_df, Notebook
color=dict(field='origin',
4. Specify where to generate the output transform=color_mapper), >>> from [Link] import output_notebook, show
5. Show or save the results legend='Origin') >>> output_notebook()
>>> from [Link] import figure
>>> from [Link] import output_file, show Legend Location HTML
>>> x = [1, 2, 3, 4, 5] Step 1
>>> y = [6, 7, 2, 4, 5] Inside Plot Area Standalone HTML
>>> p = figure(title="simple line example", Step 2 >>> [Link] = 'bottom_left' >>> from [Link] import file_html
>>> from [Link] import CDN
x_axis_label='x',
>>> html = file_html(p, CDN, "my_plot")
y_axis_label='y') Outside Plot Area
>>> [Link](x, y, legend="Temp.", line_width=2) Step 3 >>> from [Link] import Legend
>>> r1 = [Link]([Link]([1,2,3]), [Link]([3,2,1]) >>> from [Link] import output_file, show
>>> output_file("[Link]") Step 4 >>> r2 = [Link]([1,2,3,4], [3,4,5,6]) >>> output_file('my_bar_chart.html', mode='cdn')
>>> show(p) Step 5 >>> legend = Legend(items=[("One" ,[p1, r1]),("Two",[r2])],
location=(0, -30)) Components
1 Data Also see Lists, NumPy & Pandas
>>> p.add_layout(legend, 'right')
Legend Orientation
>>> from [Link] import components
>>> script, div = components(p)
Under the hood, your data is converted to Column Data
Sources. You can also do this manually: >>> [Link] = "horizontal" PNG
>>> import numpy as np >>> [Link] = "vertical"
>>> from [Link] import export_png
>>> import pandas as pd >>> export_png(p, filename="[Link]")
>>> df = [Link]([Link]([[33.9,4,65, 'US'], Legend Background & Border
[32.4,4,66, 'Asia'],
[21.4,4,109, 'Europe']]), >>> [Link].border_line_color = "navy" SVG
columns=['mpg','cyl', 'hp', 'origin'], >>> [Link].background_fill_color = "white"
index=['Toyota', 'Fiat', 'Volvo']) >>> from [Link] import export_svgs
>>> from [Link] import ColumnDataSource Rows & Columns Layout >>> p.output_backend = "svg"
>>> export_svgs(p, filename="[Link]")
>>> cds_df = ColumnDataSource(df) Rows
>>> from [Link] import row
>>> A = [Link]([Link]((2,2)))
Division
>>> B = [Link](b)
>>> [Link](A,D) #Division
>>> C = [Link]([Link]((10,5)))
NumPy extension of Python. >>> [Link](A) #Trace >>> linalg.expm2(A) #Matrix exponential (Taylor Series)
> Interacting With NumPy Also see NumPy >>> [Link](A,1) #L1 norm (max column sum)
>>> a = [Link]([1,2,3])
>>> [Link].matrix_rank(C) #Matrix rank >>> [Link](D) Matrix cosine
>>>
>>>
[Link]() #Flatten the array
>>>
>>>
F = [Link](3, k=1) #Create a 2X2 identity matrix
>>> p = poly1d([3,4,5]) #Create a polynomial object Sparse Matrix Routines Singular Value Decomposition
>>> U,s,Vh = [Link](B) #Singular Value Decomposition (SVD)
Vectorizing Functions >>> [Link](I) #Inverse >>> Sig = [Link](s,M,N) #Construct sigma matrix in SVD
Norm LU Decomposition
>>> def myfunc(a): if a < 0:
>>> P,L,U = [Link](C) #LU Decomposition
return a*2
>>> [Link](I) #Norm
else:
Solving linear problems
return a/2
>>>
>>>
np.real_if_close(c,tol=1000) #Return a real array if complex parts close to 0
>>>
>>>
g = [Link](0,[Link],num=5) #Create an array of evenly spaced values(number of samples)
g [3:] += [Link]
> Asking For Help Learn Data Skills Online at
>>>
>>>
[Link](g) #Unwrap
[Link]
>>> [Link]([c<4],[c*2]) #Return values from a list of arrays depending on
conditions
>>> help([Link])
row="sex")
>>> g = [Link]([Link],"age")
y="sepal_length",
data=iris,
ax=ax)
>>> [Link](x="pclass", #Draw a categorical plot onto a
Facetgrid
y="survived",
>>> [Link](x="sepal_width", #Plot data and regression model fits across a FacetGrid
y="sepal_length",
>>> plot = [Link](data.y, #Plot univariate distribution
hue="species",
kde=False,
data=iris)
color="b")
>>> h = [Link](iris) #Subplot grid for plotting pairwise
relationships
y="y",
The Python visualization library Seaborn is based on matplotlib and provides data=data)
Categorical Plots
"sepal_width",
y="petal_length",
data=iris)
Bar Chart
4. Further customize your plot
Axisgrid Objects >>> [Link](x="sex", #Show point estimates & confidence intervals with scatterplot glyphs
hue="class",
y="total_bill",
>>> [Link](xlim=(0,5), #Set the limit and ticks of the
x-and y-axis
palette="Greens_d")
data=tips,
ylim=(0,5),
aspect=2)
xticks=[0,2.5,5],
Point Plot
>>> g = (g.set_axis_labels("Tip","Total bill(USD)").
yticks=[0,2.5,5])
>>> [Link](x="class", #Show point estimates &
confidence intervals as
rectangular bars
set(xlim=(0,10),ylim=(0,100)))
y="survived",
data=titanic,
palette={"male":"g",
Boxplot
linestyles=["-","--"])
data=titanic)
2 Figure Aesthetics Also see Matplotlib 5 Show or Save Plot Also see Matplotlib
transparent=True)
"[Link]":8})
Color Palette
#Return a dict of params or use with with to temporarily set the style
>>> [Link]() #Clear an axis
scaler = StandardScaler().fit(X_train)
standardized_X = [Link](X_train)
Accuracy Score
>>> [Link](X_test, y_test)
#Estimator score method
Confusion Matrix
>>> scaler = Normalizer().fit(X_train)
Scikit-learn >>>
>>>
normalized_X = [Link](X_train)
normalized_X_test = [Link](X_test)
>>> from [Link] import confusion_matrix
Scikit-learn is an open source Python library that
implements a range of Binarization Regression Metrics
machine learning,
preprocessing, cross-validation and visualization
X_test = [Link](X_test)
Clustering Metrics
>>> [Link](X_train, y_train)
Homogeneity
>>> lr = LinearRegression(normalize=True)
>>> X_train, X_test, y_train, y_test = train_test_split(X,
y,
random_state=0)
Support Vector Machines (SVM)
>>> from [Link] import SVC
Grid Search
> Model Fitting
Naive Bayes
>>> from sklearn.naive_bayes import GaussianNB
>>> print(grid.best_score_)
Unsupervised Learning
Unsupervised Learning Estimators >>> print(grid.best_estimator_.n_neighbors)
>>> k_means.fit(X_train) #Fit the model to the data
>>> print(rsearch.best_score_)
>>> y_pred = [Link]([Link]((2,5)))
#Predict labels
use [Link] to start a web server and show the visualization in your browser.
spaCy Cheat Sheet Span indices are exclusive. So doc[2:4] is a span starting at
token 2, up to – but not including! – token 4.
>>> doc = nlp("This is a text")
text'
>>> doc = nlp("This is a sentence")
spaCy >>> from [Link] import Span #Import the Span object
>>> span = Span(doc, 3, 5, label="GPE") #Span for "New York" with label GPE (geopolitical)
>>> [Link]
spaCy is a free, open-source library for advanced Natural Language 'New York’
processing (NLP) in Python. It's designed
specifically for production use and Visualize named entities
helps you build
applications that process and "understand" large volumes
>>> doc = nlp("Larry Page founded Google")
>>> import spacy Attributes return label IDs. For string labels, use the attributes with an underscore. For example, token.pos_ .
and more. See here for available models:
[Link]/models
Comparing similarity
>>> $ python -m spacy download en_core_web_sm
Syntactic dependencies Predicted by Statistical model
>>> doc1 = nlp("I like cats")
Check that your installed models are up to date >>> doc = nlp("This is a text.")
>>>
>>>
[Link](doc2)
#Compare 2 documents
>>> nlp = [Link]("en_core_web_sm") # Load the installed model "en_core_web_sm"
>>> doc = nlp("Larry Page founded Google")
>>> doc[2].vector_norm
>>> [([Link], ent.label_) for ent in [Link]]
#Text and label of named entity span
information about the tokens, their linguistic features and their relationships >>> [[Link] for sent in [Link]] #[Link] is a generator that yields sentence spans
Accessing token attributes Base noun phrases Needs the tagger and parser
>>> [Link]
[('tagger', <[Link]>),
('ner', <[Link]>)]
>>> [Link]("RB")
'adverb'
Custom components
>>> [Link]("GPE")
Learn Data Skills Online at
'Countries, cities, states' def custom_component(doc):
#Function that modifies the doc and returns it
Attribute extensions With default value # Each dict represents one token and its attributes
Property extensions With getter and setter >>> for match_id, start, end in matches:
Sentence Boundary Detection
# Get the matched span by slicing the Doc
span = doc[start:end]
Method extensions Callable Method >>> pattern1 = [{"LEMMA": "love"}, {"LOWER": "cats"}]
Dependency Parsing
# Register custom attribute on Span class
# "book", "a cat", "the sea" (noun + optional article)
>>> doc[3:5].has_label("GPE")
True
Operators and quantifiers tokens, like subject or object.
Can be added to a token dict as the "OP" key
Named Entity Recognition (NER)
! Negate pattern and match exactly 0 times
Statistical model
Process for making predictions based on
examples.
Training
Updating a statistical model with new examples.
Version 2.1 Get the latest version at [Link]/visuals Order private training at [Link]/training
TensorFlow v2.0 Cheat Sheet
A Reference Machine Learning Workflow [Link] represents a sequence of elements each containing
one or more Tensor object(-s). This can be exemplified by a pair of
Here’s a conceptual diagram and a workflow example:
tensors representing an image and a corresponding class label.
import tensorflow as tf
DATASET_URL = “[Link] \
“learning-databases/covtype/[Link]”
DATASET_SIZE = 387698
dataset_path = [Link].get_file(
fname=DATASET_URL.split(’/’)[-1], origin=DATASET_URL)
COLUMN_NAMES = [
’Elevation’, ’Aspect’, ’Slope’,
’Horizontal_Distance_To_Hydrology’,
’Vertical_Distance_To_Hydrology’,
’Horizontal_Distance_To_Roadways’,
’Hillshade_9am’, ’Hillshade_Noon’, ’Hillshade_3pm’,
’Horizontal_Distance_To_Fire_Points’, ’Soil_Type’,
’Cover_Type’]
def _parse_line(line):
Version 2.1 Get the latest version at [Link]/visuals Order private training at [Link]/training
TensorFlow v2.0 Cheat Sheet
# Build, train, and evaluate the estimator
model = [Link](feature_columns,
n_classes=4)
[Link](input_fn=lambda: csv_input_fn(dataset_path),
steps=10000)
[Link] aluate(
input_fn=lambda: csv_input_fn(dataset_path, test=True))
serving_input_fn = _builder(_spec_maker(feature_columns))
export_path = model.export_saved_model(
“/tmp/from_estimator/”, serving_input_fn)
The following code sample shows how to load and use the
saved model with Python.
# Import model from SavedModel
imported = tf.saved_model.load(export_path)
Version 2.1 Get the latest version at [Link]/visuals Order private training at [Link]/training
NumPy offers diverse array creation functions like np.array() to create arrays from lists, np.zeros() and np.ones() for arrays of zeros and ones, np.arange() and np.linspace() for generating evenly spaced values, and np.random.random() for random values. These facilitate initial data preparation. Manipulation functions such as np.concatenate() to merge arrays, np.hstack() and np.vstack() for horizontal and vertical stacking, np.reshape() for changing array dimensions, and np.delete() for removing elements streamline processing tasks by enabling efficient data organization and manipulation .
NumPy provides robust techniques for matrix arithmetic, facilitating efficient scientific computing. Basic operations include np.add(), np.subtract(), np.multiply(), and np.divide() for element-wise arithmetic. The dot product can be computed using np.dot() which is crucial for linear algebra operations. Advanced functions like np.exp() and np.log() enable support for exponential and logarithmic transformations. These tools are relevant because they enable complex calculations with higher dimensional arrays efficiently, crucial for operations in fields like machine learning and data analysis .
NumPy supports various data types suited for different data processing needs. np.int64 represents signed 64-bit integer types for precise integer arithmetic. np.float32 is used for standard double-precision floating point arithmetic, offering a balance between precision and memory consumption. np.complex handles complex numbers which are essential for computations involving imaginary numbers. np.bool stores Boolean values, mainly for logical operations. np.object can wrap any Python object, facilitating generic operations. Fixed-length types like np.string_ and np.unicode_ manage fixed-length strings, important for string manipulation tasks .
NumPy offers multiple data aggregation methods, such as np.sum() for array-wise summation, np.mean() for calculating means, np.median() to find the median, np.std() for standard deviation, and np.corrcoef() for correlation coefficients. These functions allow for comprehensive statistical analyses, facilitating quick insights into data distributions and relationships. Applied in practice, such as in summarizing experimental results or verifying data integrity in preprocessing, they provide an efficient means to gather insights from large datasets without manually iterating through data points .
NumPy facilitates dimensionality manipulation using functions like np.reshape() for altering array shapes, np.ravel() for flattening arrays, and np.transpose() for permuting dimensions. These operations are crucial in data analysis as they allow data to be prepared in required shapes for specific algorithms, enable performance optimizations by aligning data structures with computational routines, and help in reshaping data for better visibility and interpretation. This capability is integral in handling multivariate data and preparing datasets for machine learning algorithms .
Seaborn, built on Matplotlib, is specifically tailored for statistical data visualization, offering a high-level interface for constructing informative and attractive graphics. The process begins with importing Seaborn and preparing data, often using built-in datasets like 'tips'. Visualization starts with functions such as sns.lmplot() for linear regression plots, sns.set_style() to enhance plot aesthetics, and further customizations like setting axis labels and limits. It benefits users by simplifying complex visualization tasks, provides a wide variety of plot types, supports custom styling, and integrates seamlessly with data processing libraries like Pandas .
Matplotlib facilitates subplot management using functions such as plt.subplots() to create a grid of subplots. To adjust subplot spacing, techniques like fig.tight_layout() and fig3.subplots_adjust() are used. fig.tight_layout() automatically adjusts subplot parameters to give specified padding, while fig3.subplots_adjust(wspace=0.5, hspace=0.3) manually specifies the width and height spaces between subplots .
Seaborn extends Matplotlib by offering several advantages for statistical visualizations, such as simpler syntax for complex plots, themes to enhance aesthetics, and built-in functions for inter-variable relationships like sns.pairplot(). These features make it particularly well-suited for exploratory data analysis. However, a drawback is that it relies on Matplotlib for basic functionality, which might not be optimal for demands requiring highly customized visualizations. Matplotlib offers exhaustive control over aesthetic details and supports a broader range of complex plot types necessary for detailed customization .
Matplotlib enhances visual comprehension by offering diverse plot types such as bar, scatter, histogram, and contour plots, each suitable for different data insights. Simultaneously, it offers extensive customization options—set titles, labels, colors, and styles—which allow tailoring the visual presentation to highlight specific data aspects effectively. Functions like plt.title() for plot titles, ax.set() for axis customization, and plt.setp() for line properties ensure that plots are not only informative but also visually appealing, aiding in better analysis and communication of data insights .
Matplotlib allows comprehensive plot styling through parameters like color, linewidth, and linestyle in functions like ax.plot() and plt.scatter(). The 'cmap' parameter specifically plays a crucial role in visual styling by defining a colormap that maps scalar data to colors. It is fundamental for enhancing the aesthetic appeal of continuous data visualizations and is commonly used in functions like plt.imshow() for image data or contour plots. Custom colormaps improve the readability and interpretability of visual data, ensuring that plots convey the correct information graphically .