From 0f9cecd5a1e9c019f003e4f7d9f4e4390a5f57ca Mon Sep 17 00:00:00 2001 From: Quarto GHA Workflow Runner Date: Tue, 26 Mar 2024 07:29:52 +0000 Subject: [PATCH] Built site for gh-pages --- .nojekyll | 2 +- .../Exercise 3 - Matplotlib.html | 98 +- Crash-Course-Numpy/00-NumPy-Arrays.html | 112 +- .../01-NumPy-Indexing-and-Selection.html | 86 +- Crash-Course-Numpy/02-NumPy-Operations.html | 72 +- Crash-Course-Numpy/03-NumPy-Exercises.html | 78 +- Crash-Course-Pandas/00-Intro-to-Pandas.html | 39 +- Crash-Course-Pandas/01-Series.html | 66 +- Crash-Course-Pandas/02-DataFrames.html | 110 +- Crash-Course-Pandas/03-Missing-Data.html | 48 +- Crash-Course-Pandas/04-Groupby.html | 60 +- Crash-Course-Pandas/05-Operations.html | 66 +- .../06-Data-Input-and-Output.html | 26 +- .../07-Pandas-Exercises-1.html | 87 +- .../09-Pandas-Exercises-2.html | 26 +- Crash-Course-Python/Exercise 1 - Python.html | 286 +-- comparision/Index.html | 26 +- comparision/dplyr-pandas.html | 26 +- comparision/ggplot2-matplotlib.html | 26 +- comparision/plsql-db2.html | 26 +- index.html | 26 +- machine-learning/transformer.html | 26 +- python-snippet/Code-optimize.html | 26 +- python-snippet/Environment.html | 26 +- python-snippet/Graphs.html | 26 +- python-snippet/Index.html | 26 +- python-snippet/JupyterNotebookFormat.html | 26 +- python-snippet/Modelling.html | 26 +- python-snippet/REST_API.html | 26 +- python-snippet/Statistics.html | 26 +- python-snippet/Ultilities.html | 26 +- .../VisualizeFeatureImportances.html | 26 +- .../beeswarm_strip_violin_plot.html | 44 +- .../convertLightGBMFromR2Python.html | 26 +- python-snippet/slopegraph.html | 26 +- r-snippet/DT.html | 26 +- r-snippet/create_package.html | 26 +- r-snippet/database.html | 26 +- r-snippet/dplyr.html | 26 +- r-snippet/file_folders.html | 26 +- r-snippet/gganimate.html | 26 +- r-snippet/ggplot2.html | 26 +- r-snippet/index.html | 26 +- r-snippet/mamba.html | 26 +- r-snippet/reticulate.html | 26 +- r-snippet/treemap.html | 26 +- search.json | 2079 ++++++++--------- site_libs/bootstrap/bootstrap-dark.min.css | 2 +- site_libs/bootstrap/bootstrap.min.css | 2 +- site_libs/quarto-nav/quarto-nav.js | 1 + sitemap.xml | 190 +- sqlserver-snippet/Index.html | 26 +- sqlserver-snippet/permissions.html | 26 +- sqlserver-snippet/synonym.html | 26 +- 54 files changed, 2687 insertions(+), 1725 deletions(-) diff --git a/.nojekyll b/.nojekyll index a907996..51e6c85 100644 --- a/.nojekyll +++ b/.nojekyll @@ -1 +1 @@ -8d2d0089 \ No newline at end of file +894a806a \ No newline at end of file diff --git a/Crash-Course-Matplotlib/Exercise 3 - Matplotlib.html b/Crash-Course-Matplotlib/Exercise 3 - Matplotlib.html index 5797351..aee71a1 100644 --- a/Crash-Course-Matplotlib/Exercise 3 - Matplotlib.html +++ b/Crash-Course-Matplotlib/Exercise 3 - Matplotlib.html @@ -2,7 +2,7 @@ - + @@ -485,9 +485,7 @@

On this page

@@ -530,9 +527,7 @@

Matplotlib - 2D and 3D plotting in Python

-
-

Matplotlib - 2D and 3D plotting in Python

-
+
# This line configures matplotlib to show figures embedded in the notebook, 
 # instead of opening a new window for each figure, similar to Exercise-0-Jupyter 
 %matplotlib inline
@@ -549,15 +544,15 @@

Introduction

One of the key features of matplotlib that I would like to emphasize, and that I think makes matplotlib highly suitable for generating figures for scientific publications is that all aspects of the figure can be controlled programmatically. This is important for reproducibility and convenient when one needs to regenerate the figure with updated data or change its appearance.

More information at the Matplotlib web page: http://matplotlib.org/

To get started using Matplotlib in a Python program, either include the symbols from the pylab module (the easy way):

-
+
from pylab import *

or import the matplotlib.pyplot module under the name plt (the tidy way):

-
+
import matplotlib
 import matplotlib.pyplot as plt
-
+
import numpy as np
@@ -566,17 +561,17 @@

MATLAB-like API

The easiest way to get started with plotting using matplotlib is often to use the MATLAB-like API provided by matplotlib.

It is designed to be compatible with MATLAB’s plotting functions, so it is easy to get started with if you are familiar with MATLAB.

To use this API from matplotlib, we need to include the symbols in the pylab module:

-
+
from pylab import *

Example

A simple figure with MATLAB-like plotting API:

-
+
x = np.linspace(0, 5, 10)
 y = x ** 2
-
+
figure()
 plot(x, y, 'r')
 xlabel('x')
@@ -592,7 +587,7 @@ 

Example

Most of the plotting related functions in MATLAB are covered by the pylab module. For example, subplot and color/symbol selection:

-
+
subplot(1,2,1)
 plot(x, y, 'r--')
 subplot(1,2,2)
@@ -614,7 +609,7 @@ 

Example

The matplotlib object-oriented API

The main idea with object-oriented programming is to have objects that one can apply functions and actions on, and no object or program states should be global (such as the MATLAB-like API). The real advantage of this approach becomes apparent when more than one figure is created, or when a figure contains more than one subplot.

To use the object-oriented API we start out very much like in the previous example, but instead of creating a new global figure instance we store a reference to the newly created figure instance in the fig variable, and from it we create a new axis instance axes using the add_axes method in the Figure class instance fig:

-
+
fig = plt.figure()
 
 axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)
@@ -633,7 +628,7 @@ 

The mat

Although a little bit more code is involved, the advantage is that we now have full control of where the plot axes are placed, and we can easily add more than one axis to the figure:

-
+
fig = plt.figure()
 
 axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # main axes
@@ -659,7 +654,7 @@ 

The mat

If we don’t care about being explicit about where our plot axes are placed in the figure canvas, then we can use one of the many axis layout managers in matplotlib. My favorite is subplots, which can be used like this:

-
+
fig, axes = plt.subplots()
 
 axes.plot(x, y, 'r')
@@ -674,7 +669,7 @@ 

The mat

-
+
fig, axes = plt.subplots(nrows=1, ncols=2)
 
 for ax in axes:
@@ -692,7 +687,7 @@ 

The mat

That was easy, but it isn’t so pretty with overlapping figure axes and labels, right?

We can deal with that by using the fig.tight_layout method, which automatically adjusts the positions of the axes on the figure canvas so that there is no overlapping content:

-
+
fig, axes = plt.subplots(nrows=1, ncols=2)
 
 for ax in axes:
@@ -713,7 +708,7 @@ 

The mat

Saving figures

To save a figure to a file we can use the savefig method in the Figure class:

-
+
fig.savefig("filename.png")
@@ -726,30 +721,30 @@

Legends, labels

Now that we have covered the basics of how to create a figure canvas and add axes instances to the canvas, let’s look at how decorate a figure with titles, axis labels, and legends.

Figure titles

A title can be added to each axis instance in a figure. To set the title, use the set_title method in the axes instance:

-
+
ax.set_title("title");

Axis labels

Similarly, with the methods set_xlabel and set_ylabel, we can set the labels of the X and Y axes:

-
+
ax.set_xlabel("x")
 ax.set_ylabel("y");

Legends

Legends for curves in a figure can be added in two ways. One method is to use the legend method of the axis object and pass a list/tuple of legend texts for the previously defined curves:

-
+
ax.legend(["curve1", "curve2", "curve3"]);

The method described above follows the MATLAB API. It is somewhat prone to errors and unflexible if curves are added to or removed from the figure (resulting in a wrongly labelled curve).

A better method is to use the label="label text" keyword argument when plots or other objects are added to the figure, and then using the legend method without arguments to add the legend to the figure:

-
+
ax.plot(x, x**2, label="curve1")
 ax.plot(x, x**3, label="curve2")
 ax.legend();

The advantage with this method is that if curves are added or removed from the figure, the legend is automatically updated accordingly.

The legend function takes an optional keyword argument loc that can be used to specify where in the figure the legend is to be drawn. The allowed values of loc are numerical codes for the various places the legend can be drawn. See http://matplotlib.org/users/legend_guide.html#legend-location for details. Some of the most common loc values are:

-
+
ax.legend(loc=0) # let matplotlib decide the optimal location
 ax.legend(loc=1) # upper right corner
 ax.legend(loc=2) # upper left corner
@@ -758,7 +753,7 @@ 

Legends, labels # .. many more options are available

The following figure shows how to use the figure title, axis labels and legends described above:

-
+
fig, ax = plt.subplots()
 
 ax.plot(x, x**2, label="y = x**2")
@@ -781,13 +776,13 @@ 

Settin

Colors

With matplotlib, we can define the colors of lines and other graphical elements in a number of ways. First of all, we can use the MATLAB-like syntax where 'b' means blue, 'g' means green, etc. The MATLAB API for selecting line styles are also supported: where, for example, ‘b.-’ means a blue line with dots:

-
+
# MATLAB style line color and style 
 ax.plot(x, x**2, 'b.-') # blue line with dots
 ax.plot(x, x**3, 'g--') # green dashed line

We can also define colors by their names or RGB hex codes and optionally provide an alpha value using the color and alpha keyword arguments:

-
+
fig, ax = plt.subplots()
 
 ax.plot(x, x+1, color="red", alpha=0.5) # half-transparant red
@@ -805,7 +800,7 @@ 

Colors

Line and marker styles

To change the line width, we can use the linewidth or lw keyword argument. The line style can be selected using the linestyle or ls keyword arguments:

-
+
fig, ax = plt.subplots(figsize=(12,6))
 
 ax.plot(x, x+1, color="blue", linewidth=0.25)
@@ -850,7 +845,7 @@ 

Control over

Plot range

The first thing we might want to configure is the ranges of the axes. We can do this using the set_ylim and set_xlim methods in the axis object, or axis('tight') for automatrically getting “tightly fitted” axes ranges:

-
+
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
 
 axes[0].plot(x, x**2, x, x**3)
@@ -876,7 +871,7 @@ 

Plot range

Logarithmic scale

It is also possible to set a logarithmic scale for one or both axes. This functionality is in fact only one application of a more general transformation system in Matplotlib. Each of the axes’ scales are set seperately using set_xscale and set_yscale methods which accept one parameter (with the value “log” in this case):

-
+
fig, axes = plt.subplots(1, 2, figsize=(10,4))
       
 axes[0].plot(x, x**2, x, np.exp(x))
@@ -897,7 +892,7 @@ 

Logarithmic scale

Scientific notation

With large numbers on axes, it is often better use scientific notation:

-
+
fig, ax = plt.subplots(1, 1)
       
 ax.plot(x, x**2, x, np.exp(x))
@@ -923,7 +918,7 @@ 

Scientific notation

Axis grid

With the grid method in the axis object, we can turn on and off grid lines. We can also customize the appearance of the grid lines using the same keyword arguments as the plot function:

-
+
fig, axes = plt.subplots(1, 2, figsize=(10,3))
 
 # default grid appearance
@@ -944,7 +939,7 @@ 

Axis grid

Axes where x and y is zero

-
+
fig, ax = plt.subplots()
 
 ax.spines['right'].set_color('none')
@@ -970,10 +965,10 @@ 

Axes where x an

Other 2D plot styles

In addition to the regular plot method, there are a number of other functions for generating different kind of plots. See the matplotlib plot gallery for a complete list of available plot types: http://matplotlib.org/gallery.html. Some of the more useful ones are show below:

-
+
n = np.array([0,1,2,3,4,5])
-
+
fig, axes = plt.subplots(1, 4, figsize=(12,3))
 
 axes[0].scatter(xx, xx + 0.25*np.random.randn(len(xx)))
@@ -995,7 +990,7 @@ 

Other 2D plot styles<

-
+
# A histogram
 n = np.random.randn(100000)
 fig, axes = plt.subplots(1, 2, figsize=(12,4))
@@ -1029,7 +1024,7 @@ 

Further reading

Versions

-
+
%reload_ext version_information
 %version_information numpy, scipy, matplotlib
@@ -1074,7 +1069,6 @@

Versions

-

@@ -1301,6 +1295,24 @@

Versions

// clear code selection e.clearSelection(); }); + var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//); + var mailtoRegex = new RegExp(/^mailto:/); + var filterRegex = new RegExp("https:\/\/nguyenngocbinh\.github\.io\/snippets"); + var isInternal = (href) => { + return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href); + } + // Inspect non-navigation links and adorn them if external + var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool)'); + for (var i=0; iVersions

try { href = new URL(href).hash; } catch {} const id = href.replace(/^#\/?/, ""); const note = window.document.getElementById(id); - return note.innerHTML; + if (note) { + return note.innerHTML; + } else { + return ""; + } }); } const xrefs = window.document.querySelectorAll('a.quarto-xref'); diff --git a/Crash-Course-Numpy/00-NumPy-Arrays.html b/Crash-Course-Numpy/00-NumPy-Arrays.html index a4b3a51..ddb83f0 100644 --- a/Crash-Course-Numpy/00-NumPy-Arrays.html +++ b/Crash-Course-Numpy/00-NumPy-Arrays.html @@ -2,7 +2,7 @@ - + @@ -482,14 +482,11 @@

On this page

    -
  • NumPy -
  • NumPy Arrays
    • Creating NumPy Arrays @@ -546,8 +543,6 @@

      NumPy

      -
      -

      NumPy

      NumPy is a powerful linear algebra library for Python. What makes it so important is that almost all of the libraries in the PyData ecosystem (pandas, scipy, scikit-learn, etc.) rely on NumPy as one of their main building blocks. Plus we will use it to generate data for our analysis examples later on!

      NumPy is also incredibly fast, as it has bindings to C libraries. For more info on why you would want to use arrays instead of lists, check out this great StackOverflow post.

      We will only learn the basics of NumPy. To get started we need to install it!

      @@ -567,12 +562,11 @@

      Using NumPy

      Once you’ve installed NumPy you can import it as a library:

      -
      +
      import numpy as np

      NumPy has many built-in functions and capabilities. We won’t cover them all but instead we will focus on some of the most important aspects of NumPy: vectors, arrays, matrices and number generation. Let’s start by discussing arrays.

      -

NumPy Arrays

NumPy arrays are the main way we will use NumPy throughout the course. NumPy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-dimensional (1D) arrays and matrices are 2D (but you should note a matrix can still have only one row or one column).

@@ -582,27 +576,27 @@

Creating NumPy Array

From a Python List

We can create an array by directly converting a list or list of lists:

-
+
my_list = [1,2,3]
 my_list
[1, 2, 3]
-
+
np.array(my_list)
array([1, 2, 3])
-
+
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
 my_matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
-
+
np.array(my_matrix)
array([[1, 2, 3],
@@ -618,13 +612,13 @@ 

Built-in Methods

arange

Return evenly spaced values within a given interval. [reference]

-
+
np.arange(0,10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
-
+
np.arange(0,11,2)
array([ 0,  2,  4,  6,  8, 10])
@@ -634,13 +628,13 @@

arange

zeros and ones

Generate arrays of zeros or ones. [reference]

-
+
np.zeros(3)
array([0., 0., 0.])
-
+
np.zeros((5,5))
array([[0., 0., 0., 0., 0.],
@@ -650,13 +644,13 @@ 

zeros and ones

[0., 0., 0., 0., 0.]])
-
+
np.ones(3)
array([1., 1., 1.])
-
+
np.ones((3,3))
array([[1., 1., 1.],
@@ -668,13 +662,13 @@ 

zeros and ones

linspace

Return evenly spaced numbers over a specified interval. [reference]

-
+
np.linspace(0,10,3)
array([ 0.,  5., 10.])
-
+
np.linspace(0,5,20)
array([0.        , 0.26315789, 0.52631579, 0.78947368, 1.05263158,
@@ -684,7 +678,7 @@ 

linspace

Note that .linspace() includes the stop value. To obtain an array of common fractions, increase the number of items:

-
+
np.linspace(0,5,21)
array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.25, 1.5 , 1.75, 2.  , 2.25, 2.5 ,
@@ -695,7 +689,7 @@ 

linspace

eye

Creates an identity matrix [reference]

-
+
np.eye(4)
array([[1., 0., 0., 0.],
@@ -712,13 +706,13 @@ 

Random

rand

Creates an array of the given shape and populates it with random samples from a uniform distribution over [0, 1). [reference]

-
+
np.random.rand(2)
array([0.37065108, 0.89813878])
-
+
np.random.rand(5,5)
array([[0.03932992, 0.80719137, 0.50145497, 0.68816102, 0.1216304 ],
@@ -732,13 +726,13 @@ 

rand

randn

Returns a sample (or samples) from the “standard normal” distribution [σ = 1]. Unlike rand which is uniform, values closer to zero are more likely to appear. [reference]

-
+
np.random.randn(2)
array([-0.36633217, -1.40298731])
-
+
np.random.randn(5,5)
array([[-0.45241033,  1.07491082,  1.95698188,  0.40660223, -1.50445807],
@@ -752,13 +746,13 @@ 

randn

randint

Returns random integers from low (inclusive) to high (exclusive). [reference]

-
+
np.random.randint(1,100)
61
-
+
np.random.randint(1,100,10)
array([39, 50, 72, 18, 27, 59, 15, 97, 11, 14])
@@ -768,14 +762,14 @@

randint

seed

Can be used to set the random state, so that the same “random” results can be reproduced. [reference]

-
+
np.random.seed(42)
 np.random.rand(4)
array([0.37454012, 0.95071431, 0.73199394, 0.59865848])
-
+
np.random.seed(42)
 np.random.rand(4)
@@ -787,18 +781,18 @@

seed

Array Attributes and Methods

Let’s discuss some useful attributes and methods for an array:

-
+
arr = np.arange(25)
 ranarr = np.random.randint(0,50,10)
-
+
arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24])
-
+
ranarr
array([38, 18, 22, 10, 10, 23, 35, 39, 23,  2])
@@ -808,7 +802,7 @@

Array Attribu

Reshape

Returns an array containing the same data with a new shape. [reference]

-
+
arr.reshape(5,5)
array([[ 0,  1,  2,  3,  4],
@@ -821,31 +815,31 @@ 

Reshape

max, min, argmax, argmin

These are useful methods for finding max or min values. Or to find their index locations using argmin or argmax

-
+
ranarr
array([38, 18, 22, 10, 10, 23, 35, 39, 23,  2])
-
+
ranarr.max()
39
-
+
ranarr.argmax()
7
-
+
ranarr.min()
2
-
+
ranarr.argmin()
9
@@ -856,14 +850,14 @@

max, min, argmax, ar

Shape

Shape is an attribute that arrays have (not a method): [reference]

-
+
# Vector
 arr.shape
(25,)
-
+
# Notice the two sets of brackets
 arr.reshape(1,25)
@@ -871,13 +865,13 @@

Shape

16, 17, 18, 19, 20, 21, 22, 23, 24]])

-
+
arr.reshape(1,25).shape
(1, 25)
-
+
arr.reshape(25,1)
array([[ 0],
@@ -907,7 +901,7 @@ 

Shape

[24]])
-
+
arr.reshape(25,1).shape
(25, 1)
@@ -916,13 +910,13 @@

Shape

dtype

You can also grab the data type of the object in the array: [reference]

-
+
arr.dtype
dtype('int32')
-
+
arr2 = np.array([1.2, 3.4, 5.6])
 arr2.dtype
@@ -1162,6 +1156,24 @@

Great Job!

// clear code selection e.clearSelection(); }); + var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//); + var mailtoRegex = new RegExp(/^mailto:/); + var filterRegex = new RegExp("https:\/\/nguyenngocbinh\.github\.io\/snippets"); + var isInternal = (href) => { + return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href); + } + // Inspect non-navigation links and adorn them if external + var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool)'); + for (var i=0; iGreat Job!

try { href = new URL(href).hash; } catch {} const id = href.replace(/^#\/?/, ""); const note = window.document.getElementById(id); - return note.innerHTML; + if (note) { + return note.innerHTML; + } else { + return ""; + } }); } const xrefs = window.document.querySelectorAll('a.quarto-xref'); diff --git a/Crash-Course-Numpy/01-NumPy-Indexing-and-Selection.html b/Crash-Course-Numpy/01-NumPy-Indexing-and-Selection.html index 30bd7fc..006b4da 100644 --- a/Crash-Course-Numpy/01-NumPy-Indexing-and-Selection.html +++ b/Crash-Course-Numpy/01-NumPy-Indexing-and-Selection.html @@ -2,7 +2,7 @@ - + @@ -482,14 +482,11 @@

On this page

@@ -516,17 +513,15 @@

NumPy Indexing and Selection

-
-

NumPy Indexing and Selection

In this lecture we will discuss how to select elements or groups of elements from an array.

-
+
import numpy as np
-
+
#Creating sample array
 arr = np.arange(0,11)
-
+
#Show
 arr
@@ -536,21 +531,21 @@

NumPy Indexing and Selection

Bracket Indexing and Selection

The simplest way to pick one or some elements of an array looks very similar to python lists:

-
+
#Get a value at an index
 arr[8]
8
-
+
#Get values in a range
 arr[1:5]
array([1, 2, 3, 4])
-
+
#Get values in a range
 arr[0:5]
@@ -561,7 +556,7 @@

Bracket Ind

Broadcasting

NumPy arrays differ from normal Python lists because of their ability to broadcast. With lists, you can only reassign parts of a list with new parts of the same size and shape. That is, if you wanted to replace the first 5 elements in a list with a new value, you would have to pass in a new 5 element list. With NumPy arrays, you can broadcast a single value across a larger set of values:

-
+
#Setting a value with index range (Broadcasting)
 arr[0:5]=100
 
@@ -571,7 +566,7 @@ 

Broadcasting

array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9,  10])
-
+
# Reset array, we'll see why I had to reset in  a moment
 arr = np.arange(0,11)
 
@@ -581,7 +576,7 @@ 

Broadcasting

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
-
+
#Important notes on Slices
 slice_of_arr = arr[0:6]
 
@@ -591,7 +586,7 @@ 

Broadcasting

array([0, 1, 2, 3, 4, 5])
-
+
#Change Slice
 slice_of_arr[:]=99
 
@@ -602,14 +597,14 @@ 

Broadcasting

Now note the changes also occur in our original array!

-
+
arr
array([99, 99, 99, 99, 99, 99,  6,  7,  8,  9, 10])

Data is not copied, it’s a view of the original array! This avoids memory problems!

-
+
#To get a copy, need to be explicit
 arr_copy = arr.copy()
 
@@ -622,7 +617,7 @@ 

Broadcasting

Indexing a 2D array (matrices)

The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend using the comma notation for clarity.

-
+
arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))
 
 #Show
@@ -633,14 +628,14 @@ 

Indexing a 2D [35, 40, 45]])

-
+
#Indexing row
 arr_2d[1]
array([20, 25, 30])
-
+
# Format is arr_2d[row][col] or arr_2d[row,col]
 
 # Getting individual element value
@@ -649,14 +644,14 @@ 

Indexing a 2D
20

-
+
# Getting individual element value
 arr_2d[1,0]
20
-
+
# 2D array slicing
 
 #Shape (2,2) from top right corner
@@ -666,14 +661,14 @@ 

Indexing a 2D [25, 30]])

-
+
#Shape bottom row
 arr_2d[2]
array([35, 40, 45])
-
+
#Shape bottom row
 arr_2d[2,:]
@@ -690,43 +685,43 @@

More Indexing Help

Conditional Selection

This is a very fundamental concept that will directly translate to pandas later on, make sure you understand this part!

Let’s briefly go over how to use brackets for selection based off of comparison operators.

-
+
arr = np.arange(1,11)
 arr
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
-
+
arr > 4
array([False, False, False, False,  True,  True,  True,  True,  True,
         True])
-
+
bool_arr = arr>4
-
+
bool_arr
array([False, False, False, False,  True,  True,  True,  True,  True,
         True])
-
+
arr[bool_arr]
array([ 5,  6,  7,  8,  9, 10])
-
+
arr[arr>2]
array([ 3,  4,  5,  6,  7,  8,  9, 10])
-
+
x = 2
 arr[arr>x]
@@ -734,7 +729,6 @@

Conditional Selectio

-

Great Job!

@@ -965,6 +959,24 @@

Great Job!

// clear code selection e.clearSelection(); }); + var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//); + var mailtoRegex = new RegExp(/^mailto:/); + var filterRegex = new RegExp("https:\/\/nguyenngocbinh\.github\.io\/snippets"); + var isInternal = (href) => { + return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href); + } + // Inspect non-navigation links and adorn them if external + var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool)'); + for (var i=0; iGreat Job!

try { href = new URL(href).hash; } catch {} const id = href.replace(/^#\/?/, ""); const note = window.document.getElementById(id); - return note.innerHTML; + if (note) { + return note.innerHTML; + } else { + return ""; + } }); } const xrefs = window.document.querySelectorAll('a.quarto-xref'); diff --git a/Crash-Course-Numpy/02-NumPy-Operations.html b/Crash-Course-Numpy/02-NumPy-Operations.html index 4cf5d0d..145aeb2 100644 --- a/Crash-Course-Numpy/02-NumPy-Operations.html +++ b/Crash-Course-Numpy/02-NumPy-Operations.html @@ -2,7 +2,7 @@ - + @@ -482,13 +482,10 @@

On this page

@@ -515,12 +512,10 @@

NumPy Operations

-
-

NumPy Operations

Arithmetic

You can easily perform array with array arithmetic, or scalar with array arithmetic. Let’s see some examples:

-
+
import numpy as np
 arr = np.arange(0,10)
 arr
@@ -528,25 +523,25 @@

Arithmetic

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
-
+
arr + arr
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
-
+
arr * arr
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])
-
+
arr - arr
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
-
+
# This will raise a Warning on division by zero, but not an error!
 # It just fills the spot with nan
 arr/arr
@@ -558,7 +553,7 @@

Arithmetic

array([nan,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
-
+
# Also a warning (but not an error) relating to infinity
 1/arr
@@ -570,7 +565,7 @@

Arithmetic

0.2 , 0.16666667, 0.14285714, 0.125 , 0.11111111])
-
+
arr**3
array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729], dtype=int32)
@@ -580,7 +575,7 @@

Arithmetic

Universal Array Functions

NumPy comes with many universal array functions, or ufuncs, which are essentially just mathematical operations that can be applied across the array.
Let’s show some common ones:

-
+
# Taking Square Roots
 np.sqrt(arr)
@@ -588,7 +583,7 @@

Universal Array 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])

-
+
# Calculating exponential (e^)
 np.exp(arr)
@@ -597,7 +592,7 @@

Universal Array 2.98095799e+03, 8.10308393e+03])

-
+
# Trigonometric Functions like sine
 np.sin(arr)
@@ -605,7 +600,7 @@

Universal Array -0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849])

-
+
# Taking the Natural Logarithm
 np.log(arr)
@@ -621,26 +616,26 @@

Universal Array

Summary Statistics on Arrays

NumPy also offers common summary statistics like sum, mean and max. You would call these as methods on an array.

-
+
arr = np.arange(0,10)
 arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
-
+
arr.sum()
45
-
+
arr.mean()
4.5
-
+
arr.max()
9
@@ -656,7 +651,7 @@

Summary Stati

Axis Logic

When working with 2-dimensional arrays (matrices) we have to consider rows and columns. This becomes very important when we get to the section on pandas. In array terms, axis 0 (zero) is the vertical axis (rows), and axis 1 is the horizonal axis (columns). These values (0,1) correspond to the order in which arr.shape values are returned.

Let’s see how this affects our summary statistic calculations from above.

-
+
arr_2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
 arr_2d
@@ -665,7 +660,7 @@

Axis Logic

[ 9, 10, 11, 12]])

-
+
arr_2d.sum(axis=0)
array([15, 18, 21, 24])
@@ -673,7 +668,7 @@

Axis Logic

By passing in axis=0, we’re returning an array of sums along the vertical axis, essentially [(1+5+9), (2+6+10), (3+7+11), (4+8+12)]

-
+
arr_2d.shape
(3, 4)
@@ -682,12 +677,11 @@

Axis Logic

This tells us that arr_2d has 3 rows and 4 columns.

In arr_2d.sum(axis=0) above, the first element in each row was summed, then the second element, and so forth.

So what should arr_2d.sum(axis=1) return?

-
+
# THINK ABOUT WHAT THIS WILL RETURN BEFORE RUNNING THE CELL!
 arr_2d.sum(axis=1)
-

Great Job!

That’s all we need to know for now!

@@ -919,6 +913,24 @@

Great Job!

// clear code selection e.clearSelection(); }); + var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//); + var mailtoRegex = new RegExp(/^mailto:/); + var filterRegex = new RegExp("https:\/\/nguyenngocbinh\.github\.io\/snippets"); + var isInternal = (href) => { + return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href); + } + // Inspect non-navigation links and adorn them if external + var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool)'); + for (var i=0; iGreat Job!

try { href = new URL(href).hash; } catch {} const id = href.replace(/^#\/?/, ""); const note = window.document.getElementById(id); - return note.innerHTML; + if (note) { + return note.innerHTML; + } else { + return ""; + } }); } const xrefs = window.document.querySelectorAll('a.quarto-xref'); diff --git a/Crash-Course-Numpy/03-NumPy-Exercises.html b/Crash-Course-Numpy/03-NumPy-Exercises.html index 0e33363..08a393e 100644 --- a/Crash-Course-Numpy/03-NumPy-Exercises.html +++ b/Crash-Course-Numpy/03-NumPy-Exercises.html @@ -2,7 +2,7 @@ - + @@ -482,12 +482,9 @@

On this page

@@ -514,8 +511,6 @@

NumPy Exercises

-
-

NumPy Exercises

Now that we’ve learned about NumPy let’s test your knowledge. We’ll start off with a few simple tasks and then you’ll be asked some more complicated questions.

IMPORTANT NOTE! Make sure you don’t run the cells directly above the example output shown,
otherwise you will end up writing over the example output! @@ -525,10 +520,10 @@

1. Import NumPy as np

2. Create an array of 10 zeros

-
+
# CODE HERE
-
+
# DON'T WRITE HERE
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
@@ -537,7 +532,7 @@

2. Create an a

3. Create an array of 10 ones

-
+
# DON'T WRITE HERE
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
@@ -546,7 +541,7 @@

3. Create an ar

4. Create an array of 10 fives

-
+
# DON'T WRITE HERE
array([5., 5., 5., 5., 5., 5., 5., 5., 5., 5.])
@@ -555,7 +550,7 @@

4. Create an a

5. Create an array of the integers from 10 to 50

-
+
# DON'T WRITE HERE
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
@@ -566,7 +561,7 @@ 

6. Create an array of all the even integers from 10 to 50

-
+
# DON'T WRITE HERE
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,
@@ -576,7 +571,7 @@ 

7. Create a 3x3 matrix with values ranging from 0 to 8

-
+
# DON'T WRITE HERE
array([[0, 1, 2],
@@ -587,7 +582,7 @@ 

8. Create a 3x3 identity matrix

-
+
# DON'T WRITE HERE
array([[1., 0., 0.],
@@ -598,7 +593,7 @@ 

8. Create a 3

9. Use NumPy to generate a random number between 0 and 1

 NOTE: Your result’s value should be different from the one shown below.

-
+
# DON'T WRITE HERE
array([0.65248055])
@@ -607,7 +602,7 @@

10. Use NumPy to generate an array of 25 random numbers sampled from a standard normal distribution

  NOTE: Your result’s values should be different from the ones shown below.

-
+
# DON'T WRITE HERE
array([ 1.80076712, -1.12375847, -0.98524305,  0.11673573,  1.96346762,
@@ -620,7 +615,7 @@ 

11. Create the following matrix:

-
+
# DON'T WRITE HERE
array([[0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ],
@@ -638,7 +633,7 @@ 

11. Create the

12. Create an array of 20 linearly spaced points between 0 and 1:

-
+
# DON'T WRITE HERE
array([0.        , 0.05263158, 0.10526316, 0.15789474, 0.21052632,
@@ -651,7 +646,7 @@ 

Numpy Indexing and Selection

Now you will be given a starting matrix (be sure to run the cell below!), and be asked to replicate the resulting matrix outputs:

-
+
# RUN THIS CELL - THIS IS OUR STARTING MATRIX
 mat = np.arange(1,26).reshape(5,5)
 mat
@@ -665,10 +660,10 @@

Numpy Indexin

13. Write code that reproduces the output shown below.

  Be careful not to run the cell immediately above the output, otherwise you won’t be able to see the output any more.

-
+
# CODE HERE
-
+
# DON'T WRITE HERE
array([[12, 13, 14, 15],
@@ -679,7 +674,7 @@ 

14. Write code that reproduces the output shown below.

-
+
# DON'T WRITE HERE
20
@@ -688,7 +683,7 @@

15. Write code that reproduces the output shown below.

-
+
# DON'T WRITE HERE
array([[ 2],
@@ -699,7 +694,7 @@ 

16. Write code that reproduces the output shown below.

-
+
# DON'T WRITE HERE
array([21, 22, 23, 24, 25])
@@ -708,7 +703,7 @@

17. Write code that reproduces the output shown below.

-
+
# DON'T WRITE HERE
array([[16, 17, 18, 19, 20],
@@ -721,7 +716,7 @@ 

NumPy Operations

18. Get the sum of all the values in mat

-
+
# DON'T WRITE HERE
325
@@ -730,7 +725,7 @@

18. G

19. Get the standard deviation of the values in mat

-
+
# DON'T WRITE HERE
7.211102550927978
@@ -739,7 +734,7 @@

20. Get the sum of all the columns in mat

-
+
# DON'T WRITE HERE
array([55, 60, 65, 70, 75])
@@ -751,7 +746,6 @@

20.

Bonus Question

We worked a lot with random data with numpy, but is there a way we can insure that we always get the same random numbers? Click Here for a Hint

-

Great Job!

@@ -982,6 +976,24 @@

Great Job!

// clear code selection e.clearSelection(); }); + var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//); + var mailtoRegex = new RegExp(/^mailto:/); + var filterRegex = new RegExp("https:\/\/nguyenngocbinh\.github\.io\/snippets"); + var isInternal = (href) => { + return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href); + } + // Inspect non-navigation links and adorn them if external + var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool)'); + for (var i=0; iGreat Job!

try { href = new URL(href).hash; } catch {} const id = href.replace(/^#\/?/, ""); const note = window.document.getElementById(id); - return note.innerHTML; + if (note) { + return note.innerHTML; + } else { + return ""; + } }); } const xrefs = window.document.querySelectorAll('a.quarto-xref'); diff --git a/Crash-Course-Pandas/00-Intro-to-Pandas.html b/Crash-Course-Pandas/00-Intro-to-Pandas.html index a90fd31..eb8e469 100644 --- a/Crash-Course-Pandas/00-Intro-to-Pandas.html +++ b/Crash-Course-Pandas/00-Intro-to-Pandas.html @@ -2,7 +2,7 @@ - + @@ -444,13 +444,7 @@
@@ -474,8 +468,6 @@

Introduction to Pandas

-
-

Introduction to Pandas

In this section of the course we will learn how to use pandas for data analysis. You can think of pandas as an extremely powerful version of Excel, with a lot more features. In this section of the course, you should go through the notebooks in this order:

  • Introduction to Pandas
  • @@ -490,7 +482,6 @@

    Introduction to Pandas


    -