Elementary Linear Algebra

Mathematics 204 University of Liberia (Math 204)

Sir Calvin A. Gaye (Course Lecturer)

2026-03-28

Vectors and Vector Operation

Chapter 1. Overview

  • Python Basics
  • Introduction to Vectors
  • Visualizing Vectors
  • Special Vectors
  • Vector Operations
  • Vector Correlation and Projection
  • Chapter Summary
  • Chapter Exam

Python Basics

  • We’ll start with Python’s workhorse data structures: tuples, lists, dictionaries, and sets.

  • Then, we’ll discuss creating your own reusable Python functions.

  • Finally, we’ll look at the mechanics of Python file objects and interacting with your local hard drive.

Python Basics | f-string

  • An f-string is a special string that is created by prefixing the first string-delimiter with the letter f

  • Any part of an f-string contained within curly braces { } will be evaluated before the string is used.

python code
```{python}
#| code-fold: true
import numpy as np
x = 10
print(f'The square of x is {x**2}')
```
The square of x is 100

Python Basics | Tuple

  • A tuple is a fixed-length, immutable sequence of Python objects which, once assigned, cannot be changed.

  • The easiest way to create one is with a comma-separated sequence of values wrapped in parentheses ( ):

Example 1.1.1 | Tuple

  • Tuple 4, 5, and 6
python code
```{python}
#| code-fold: true
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from plotvec import plotvecR

tup = (4, 5, 6)
print(tup)
```
(4, 5, 6)

Example 1.1.2 | Tuple without parentheses

python code
```{python}
#| code-fold: true
tup1 = 4, 5, 6
print(tup1)
```
(4, 5, 6)

Example 1.1.3. | More on Tuples

  • You can convert any sequence or iterator to a tuple by invoking tuple:
python code
```{python}
#| code-fold: true
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from plotvec import plotvecR

tup2 = tuple([4, 8, 2])
print(tup2)
```
(4, 8, 2)

Example 1.1.4 | More on Tuple

python code
```{python}
#| code-fold: true
tup3 = tuple('string')
print(tup3)
```
('s', 't', 'r', 'i', 'n', 'g')

Python Basics | Lists

  • In contrast with tuples, lists are variable length and their contents can be modified in place.

  • Lists are mutable.

  • You can define them using square brackets [ ] or using the list type function.

Example 1.1.5 | Lists

python code
```{python}
#| code-fold: true

l1 = [2, 3, 7, None]
tup = ('foo', 'peekaboo', 'baz')
l2 = list(tup)
print(l2)
```
['foo', 'peekaboo', 'baz']

Python Basics | Lists

  • The list built-in function is frequently used in data processing as a way to materialize an iterator or generator expression:
python code
```{python}
#| code-fold: true

gen = range(10)
print(gen)

gen1 = list(gen)
print(gen1)
```
range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Adding and removing elements | Lists

  • Elements can be appended to the end of the list with the append method:
python code
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import statsmodels as sm
l2 = ['foo', 'peekaboo', 'baz']
l2.append('foo')
print(l2)
['foo', 'peekaboo', 'baz', 'foo']

Python Basics | Lists

  • Using insert you can insert an element at a specific location in the list:
python code
```{python}
#| code-fold: true

l2 = ['foo', 'peekaboo', 'baz']
l2.append('foo')
l2.insert(1, 'red')
print(l2)

```
['foo', 'red', 'peekaboo', 'baz', 'foo']

Adding and removing elements | Lists

  • The inverse operation to insert is pop, which removes and returns an element at a particular index:
python code
l2 = ['foo', 'peekaboo', 'baz']
l2.append('foo')
l2.pop(0)
'foo'

Python Basics | Lists

  • Elements can be removed by value with remove, which locates the first such value and removes it from the list:
python code
```{python}
#| code-fold: true

l2 = ['foo', 'peekaboo', 'baz']
l2.append('foo')
l2.insert(1, 'red')
l2.remove('foo')
print(l2)

```
['red', 'peekaboo', 'baz', 'foo']

Checking | Lists

  • Check if a list contains a value using the in keyword:
python code
l2 = ['foo', 'peekaboo', 'baz']
l2.append('foo')
"foo" in l2
True

Concatenating and combining | Lists

  • Similar to tuples, adding two lists together with + concatenates them:
python code
```{python}
#| code-fold: true

l4 = [4, None, 'foo'] + [7, 8, (2, 3)]
print(l4)
```
[4, None, 'foo', 7, 8, (2, 3)]

Extend | Lists

  • If you have a list already defined, you can append multiple elements to it using the extend method:
python code
l5 = [4, None, 'foo']
l5.extend([7, 8, (2, 3)])
print(l5)
[4, None, 'foo', 7, 8, (2, 3)]

Sorting | Lists

  • You can sort a list in place (without creating a new object) by calling its sort function:
python code
```{python}
#| code-fold: true

l6 = [7, 2, 5, 1, 3, 4]
l6.sort()
print(l6)
```
[1, 2, 3, 4, 5, 7]

Sorting | Lists

  • We could sort a collection of strings by their lengths:
python code
l7 = ['saw', 'small', 'He', 'foxes', 'six']
l7.sort(key=len)
print(l7)
['He', 'saw', 'six', 'small', 'foxes']

Slicing | Lists

  • You can select sections of most sequence types by using slice notation, which in its basic form consists of start:stop passed to the indexing operator [ ]:
python code
```{python}
#| code-fold: true

l8 = [7, 2, 3, 7, 5, 6, 0, 1]
l9 = l8[1:5]
print(l9)
```
[2, 3, 7, 5]

Slicing | Lists

  • Either the start or stop can be omitted, in which case they default to the start of the sequence and the end of the sequence, respectively:
python code
l8 = [7, 2, 3, 7, 5, 6, 0, 1]
s1 = l8[:5]
s2 = l8[3:]
print(f's1 is {s1} and s2 is {s2}')
s1 is [7, 2, 3, 7, 5] and s2 is [7, 5, 6, 0, 1]

Slicing | Lists

  • Negative indices slice the sequence relative to the end:
python code
```{python}
#| code-fold: true

l8 = [7, 2, 3, 7, 5, 6, 0, 1]
s3 = l8[-4:]
print(f's3 is {s3}')
```
s3 is [5, 6, 0, 1]

Slicing | Lists

  • A step can also be used after a second colon to, say, take every other element:
python code
l8 = [7, 2, 3, 7, 5, 6, 0, 1]
s4 = l8[::2]
print(f's4 is {s4}')
s4 is [7, 3, 5, 0]

Python Basics | Dictionary

  • The dictionary or dict may be the most important built-in Python data structure.

  • A dictionary stores a collection of key-value pairs, where key and value are Python objects.

  • Each key is associated with a value so that a value can be conveniently retrieved, inserted, modified, or deleted given a particular key.

Python Basics | Dictionary

  • One approach for creating a dictionary is to use curly braces { } and colons to separate keys and values:

  • Create a dict with two keys a and b

python code
d1 = {'a': 'some value', 'b': [1, 2, 3, 4]}
print(f'd1 is {d1}')
d1 is {'a': 'some value', 'b': [1, 2, 3, 4]}

Python Basics | Dictionary

  • You can access, insert, or set elements using the same syntax as for accessing elements of a list or tuple:
python code
d1 = {'a': 'some value', 'b': [1, 2, 3, 4]}
d1[7] = 'an integer'
print(d1)
d1['b']
{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}
[1, 2, 3, 4]

Python Basics | Dictionary

  • You can check if a dictionary contains a key using the same syntax used for checking whether a list or tuple contains a value:
python code
d1 = {'a': 'some value', 'b': [1, 2, 3, 4]}
'b' in d1
True

Python Basics | Dictionary

  • You can delete values using either the del keyword or the pop method (which simultaneously returns the value and deletes the key):
python code
d1 = {'a': 'some value', 'b': [1, 2, 3, 4]}
d1[5] = 'some value'
print(d1)
{'a': 'some value', 'b': [1, 2, 3, 4], 5: 'some value'}

Python Basics | Dictionary

  • del keyword
python code
d1 = {'a': 'some value', 'b': [1, 2, 3, 4]}
d1[5] = 'some value'
del d1['a']
print(d1)
{'b': [1, 2, 3, 4], 5: 'some value'}

Python Basics | Set

  • A set is an unordered collection of unique elements.

  • A set can be created in two ways: via the set function or via a set literal with curly braces.

python code
z = set([2, 2, 2, 1, 3, 3])
print(z)
{1, 2, 3}

Python Basics | Set

  • Sets support mathematical set operations like union, intersection, difference, and symmetric difference.

  • Consider these two example sets:

python code
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}
print(f'set a = {a} amd b = {b}')
set a = {1, 2, 3, 4, 5} amd b = {3, 4, 5, 6, 7, 8}

Python Basics | Union of Sets

  • The union of these two sets is the set of distinct elements occurring in either set.

  • This can be computed with either the union method or the | binary operator:

python code
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}
c = a.union(b)
d = a | b
print(f'set c = {c} and set d = {d}')
set c = {1, 2, 3, 4, 5, 6, 7, 8} and set d = {1, 2, 3, 4, 5, 6, 7, 8}

Python Basics | Intersection of Sets

  • The intersection contains the elements occurring in both sets.

  • The & operator or the intersection method can be used:

python code
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}
e = a.intersection(b)
f = a & b
print(f'set e = {e} amd f = {f}')
set e = {3, 4, 5} amd f = {3, 4, 5}

Loops and Conditionals | The for … in Statements

  • The forin compound statement takes a variable after the for keyword.

  • This variable will hold the current iteration value.

  • After the in keyword there needs to be an iterable object, which is any object that can be iterated over.

  • The typical one we will use is the range object that we introduced:

Loops and Conditionals | The for … in Statements

python code
#import sys
#sys.getdefaultencoding()
for i in range(4):
  print(i)
0
1
2
3
python code
for j in range(2, 4):
  print(j)
2
3
python code
for k in range(2, 10, 2):
  print(k)
2
4
6
8

Loops and Conditionals | The for … in Statements

  • Another example of an iterable object is a list:
python code
fruit = ['apples', 'bananas', 'oranges']
for kind in fruit:
  print(kind)
apples
bananas
oranges

Loops and Conditionals | The for … in Statements

  • When iterating over objects like a list, it is often helpful to keep track of the iteration index.

  • One easy way to do this is to use the enumerate() function that returns a tuple of the current iteration index and the item:

python code
fruit = ['apples', 'bananas', 'oranges']
for i, kind in enumerate(fruit):
  print(i)
  print(kind)
  print( )
0
apples

1
bananas

2
oranges

Loops and Conditionals | The for … in Statements

  • We can nest ‘for’ statements, which means that one for statement is inside of another for statement.

  • For each iteration of the outer loop, the inner loop will run through all its iterations:

python code
for i in range(3):
  for j in range(2):
    print(i, j)
  print( )
0 0
0 1

1 0
1 1

2 0
2 1

Loops and Conditionals | if Statements

  • The if statement is used to run code conditionally.

  • It will often be the case that if statements are used inside loop or other conditional statements.

python code
for i in range(1, 11):
  if i % 2 == 0:
    print(i)
2
4
6
8
10

Loops and Conditionals | if Statements

  • More generally, an if statement may also have elif and else clauses.

  • Elif is short for “else if”, and these headers act like if headers but will be evaluated only if the above if or elif headers did not have their conditions satisfied.

  • There can only be one else clause, and its suite will be executed if the if and elif clauses did not have their conditions satisfied.

Loops and Conditionals | if Statements

  • Note that the elif and else headers must be at the same indentation level as the corresponding if header, and these headers must also end with a colon (:).

Example 1.1.6 | if Statements

  • Suppose we want to identify the even numbers from 1 to 10, but if the number is NOT even, we wish to determine if it is divisible by 3. Otherwise, we just want to print an asterisk:
python code
```{python}
#| code-fold: true
for i in range(1, 11):
  if i % 2 == 0:
    print(f'{i} is even')
  elif i % 3 == 0:
    print(f'{i} is not even')
  else:
    print('*')

```
*
2 is even
3 is not even
4 is even
*
6 is even
*
8 is even
9 is not even
10 is even

Example 1.1.7 | if Statements

  • We will generally iterate over dictionaries by iterating over their keys:
python code
```{python}
#| code-fold: true
squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

for key in squares.keys():
  print(f'{key}**2 = {squares[key]}')
```
1**2 = 1
2**2 = 4
3**2 = 9
4**2 = 16
5**2 = 25

Loops and Conditionals | while Statements

  • A while loop combines looping and a conditional statement for determining whether looping should continue.

  • The loop will continue as long as the condition specified in the while statement is satisfied:

Loops and Conditionals | while Statements

python code
```{python}
#| code-fold: true
i = 1
while i < 10:
  if i % 2 == 0:
    print(i)
  i = i + 1
```
2
4
6
8

Python Basics | Functions

  • Functions are the primary and most important method of code organization and reuse in Python.

  • Functions are declared with the def keyword.

  • A function contains a block of code with an optional use of the return keyword:

python code
#import sys
#sys.getdefaultencoding()

Python Basics | Functions

python code
def my_function(x, y):
  return x + y
result = my_function(1, 3)
print(result)
4

Functions | Generator

  • A generator is a convenient way, similar to writing a normal function, to construct anew iterable object.

  • Generators can return a sequence of multiple values

  • To create a generator, use the yield keyword instead of return in a function:

python code
#import sys
#sys.getdefaultencoding()

Python Basics | Functions

python code
def squares(n=10):
  print(f"Generating squares from 1 to {n ** 2}")
  for i in range(1, n + 1):
    yield i ** 2
python code
gen = squares()
for x in gen:
  print(x, end=" ")
Generating squares from 1 to 100
1 4 9 16 25 36 49 64 81 100 

Introduction to Vectors

  • Vectors provide a way to collect and operate on multiple pieces of numerical data.

  • In this chapter, we define vectors and introduce different ways to visualize vector data. Then we introduce the most common vector operations and their properties.

  • The chapter ends with a discussion of vector projection, which introduces concepts of how we can approximate a vector as a scaled version of another vector.

Introduction to Vectors | vectors, order, scalar

  • vector: an ordered collection of numbers that has an accompanying set of mathematical operations.

  • order: the number of axes used to index the contents of a mathematical object, such as a vector, matrix, or tensor.

  • scalar: a single numerical value.

vectors, order, scalar

  • Vectors are usually represented mathematically as a column of numbers enclosed in large square brackets, like \[\textbf{u} = \begin{bmatrix} 0.75 \\ -1 \\ 1.75\\ 2.5 \end{bmatrix}\]

  • To save space, we can also write a column vector like \[\textbf{u} = \begin{bmatrix} 0.75 & -1 & 1.75 & 2.5 \end{bmatrix}^T\]

  • where the superscript\(^T\) indicates that the vector should be “transposed” from a row to a column.

Example 2.1 | Vector in NumPy

  • Create a Python object that represents \(\textbf{u} = \begin{bmatrix} 0.75 & -1 & 1.75 & 2.5 \end{bmatrix}^T\) using NumPy’s array class.
python code
import numpy as np
u = np.array([ 0.75, -1, 1.75, 2.5])
#print(u)

vectors, order, scalar

  • In NumPy, the number of dimensions of the array corresponds to the order of the mathematical object that the array represents.
python code
u.ndim
1

Component (vector) | Element (vector)

  • (Component (vector) or Element (vector):) one of the numerical values that make up the vector.

Example 2.2 | Accessing a Component

  • For the vector \(\textbf{u} = \begin{bmatrix} 0.75 & -1 & 1.75 & 2.5 \end{bmatrix}^T\), find \(u_2\).
python code
u2 = u[2]
#print(u2)

Example 2.3 | NumPy Indexing by Range

  • Get the firs to third elements of \(\textbf{u} = \begin{bmatrix} 0.75 & -1 & 1.75 & 2.5 \end{bmatrix}^T\) using NumPy’s array class.
python code
import numpy as np
u = np.array([ 0.75, -1, 1.75, 2.5])
u02 = u[0:3]
#print(u02)

vectors, order, scalar

  • size or dimension (vector): the number of components a vector contains.

Example 2.4 | Size of a Vector in NumPy

  • What is the dimension of \(\textbf{u} = \begin{bmatrix} 0.75 & -1 & 1.75 & 2.5 \end{bmatrix}^T\) using NumPy’s array class.
python code
du = np.size(u)
#print(f'The size of u is {du}')

Visualizing Vectors

  • Vectors are often visualized as displacements from a point, meaning an indication of movement from some starting point to an ending point.

  • If no starting point is given, then the displacement is measured from the origin.

  • One of the most common ways to illustrate a vector is the use of 2-vectors.

  • To illustrate 2-vectors, draw each 2-vector as an arrow from the origin to the coordinates given in the vector.

  • This is a special case of a quiver plot:

Visualizing Vectors | Quiver Plot

  • Quiver Plot: a (two-dimensional) plot that illustrates one or more vectors as arrows that are typically specified by a location, which determines the coordinates of the tail of the vector, and some specification of the direction and magnitude of the vector.

  • If no location is provided, then the origin (0, 0) is used.

Example 1.2.1 | Plotting a Vector with plotvecR( )

  • Consider the vector \(\textbf{a} = [2, 3]^T\). Visualize this vector using the following interpretation: Since no initial location is specified, start at the origin. Then move 2 in the x direction (to the right) and 3 in the y direction (up). Draw an arrow from (0,0) to (2,3) to represent this Vector:
python code
from plotvec import plotvecR
a = np.array([2, 3])
plotvecR(a)

Visualizing Vectors | Head and Tail

  • Tail (vector): the tail of a vector is the starting point of a vector (the initial point from which the displacement is measured).

  • Head (vector): the head of a vector is the ending point of a vector (the point at which the vector terminates after the specified displacement from the tail)

Example 1.2.2 | Plotting Two Vectors with Tails at the Origin

  • Consider the vector \(\textbf{a} = [2, 3]^T\) and \(\textbf{a} = [2, 3]^T\). Visualize these vectors with their tails emanating from the origin.
python code
from plotvec import plotvecR
a = np.array([2, 3])
b = np.array([1, -2])
plotvecR(a, b)

Applications

  • Vectors are used in many different ways.

  • Below are some of the ways that vectors are used, along with an example of each type of use:

Multi-dimensional numerical data

  • We often represent a data set as a table.

  • For instance, each row may represent one data point, and each column may represent one feature.

  • If all of the data features are numeric or encoded as numerical values, then we can use vectors to represent such data in different ways.

Example 1.3.1 | Multi-dimensional numerical data

  • HTIN4: A computed variable that lists height in inches

  • WEIGHT2: The reported weight in pounds.

python code
import pandas as pd
brfss = pd.read_csv('/Users/calvina.gaye/Downloads/Quarto2026/MATH204UL2025_26S2/mdata1.csv')
brfss.head()
HTIN4 WEIGHT2
0 59.0 72.0
1 65.0 170.0
2 64.0 195.0
3 71.0 206.0
4 75.0 195.0

Multi-dimensional numerical data

  • Because the data occupies a two-dimensional table, we can decompose it into vectors in two different ways:

    • we can treat each row (i.e., data point) as a vector or

    • each column (i.e.,feature) as a vector.

Example 1.3.2 | Multi-dimensional numerical data

  • Plot the arrow representations for the first 50 rows in the dataset.
python code
import pandas as pd
from plotvec import plotvec

# I use a for loop to iterate over the first 50 rows and call plotvec() for each one:

brfss = pd.read_csv('/Users/calvina.gaye/Downloads/Quarto2026/MATH204UL2025_26S2/mdata1.csv')

for i in range(50):
  plotvec(brfss.iloc[i], color_offset=2*i, square_aspect_ratio=False, newfig=False)
plt.xlim(0,80)
plt.ylim(0,350)
plt.xlabel('Height (in)')
plt.ylabel('Weight (lbs)')
Text(0, 0.5, 'Weight (lbs)')

Example 1.3.2 | Major takeaways

  • Do you notice any trend the arrow?

    • All arrows point up and to the right

      • because both height and weight are positive quantities
    • Most arrows point in the same general direction

      • two features are not independent of each other
    • General trend by intuition: taller people are more likely to be heavier than shorter people.

Visualizing more data as points | Scatter Plot

  • A better approach to visualize more numerical data is

    • to use a scatter plot
  • A scatter plot takes a sequence of two-dimensional data points \((x_0, y_0), (x_1, y_1) \dots, (x_{n-1}, y_{n-1})\) and plots symbols (called markers) that represent the locations of the points in a rectangular region of a plane.

Example 1.3.3 | Scatter Plot

python code
import pandas as pd
import polars as pl
import matplotlib.pyplot as plt
from plotnine import *

plt.scatter(brfss['HTIN4'][::100], brfss['WEIGHT2'][::100], 4,
alpha=0.7)
plt.xlabel('Height (in)')
plt.ylabel('Weight (lbs)')
Text(0, 0.5, 'Weight (lbs)')

Visualizing more data as points | Time-series data

  • Time-series data: data that is collected over time, usually at regular intervals.

  • Each data point is associated with a timestamp indicating when the data was collected.

Example 1.3.4 | Annual Temp Data for Liberia

python code
import pandas as pd
import polars as pl
import matplotlib.pyplot as plt
from plotnine import *

libtemp = pd.read_csv('/Users/calvina.gaye/Downloads/Quarto2026/Python_venv2026/ULSem2_202526/annual_temp_anom/annual_temp_anom.csv')
libtemp.head()
Entity Code Year TempAnomaly
0 Liberia LBR 1940 -0.922306
1 Liberia LBR 1941 -0.904504
2 Liberia LBR 1942 -1.037940
3 Liberia LBR 1943 -1.083066
4 Liberia LBR 1944 -0.992378

Visualizing Liberia Temperature Anomaly | Time-Series Plot

python code
import pandas as pd
import polars as pl
import matplotlib.pyplot as plt
from plotnine import *

libtemp = pd.read_csv('/Users/calvina.gaye/Downloads/Quarto2026/Python_venv2026/ULSem2_202526/annual_temp_anom/ave_annual_surface_temp.csv')

plt.scatter(libtemp['Year'], libtemp['Temperature'], 15)
plt.xlabel('Year')
plt.ylabel('Annual Surface Temperature ($\circ$C)')
Text(0, 0.5, 'Annual Surface Temperature ($\\circ$C)')

python code
import pandas as pd
import polars as pl
import matplotlib.pyplot as plt
from plotnine import *
libtemp = pl.read_csv('/Users/calvina.gaye/Downloads/Quarto2026/Python_venv2026/ULSem2_202526/annual_temp_anom/ave_annual_surface_temp.csv')

temp = ggplot(libtemp, aes(x="Year", y="Temperature")) + geom_point() + labs(title = "Liberia Average Annual Surface Temperature", x = "Year (1940 - 2025)", y = "Annual Surface Temperature", caption="Data source: Contains modified Copernicus Climate Change Service information (2026)")
temp

Scatter Plot and Time Series | Major takeaways

  • Scatter Plot

    • The scatter plot shows a similar trend to what we saw in the previous quiver plot.
    • The general trend is that larger heights are generally associated with larger weights.
  • Time Series

    • The results seem to show an even stronger relation between annual temperature and year than the relationship between height and weight seen in the BRFSS data.

Visualizing Vectors | Distributional Data

  • Vectors may be used to indicate distributions or allocations across categories.

  • For example, an investor’s current net worth across different categories (such as stocks, bonds, and real estate) can be represented as a vector.

Visualizing Vectors | Distributional Data

python code
import pandas as pd
import polars as pl
import matplotlib.pyplot as plt
from plotnine import *

jones = pl.read_csv('/Users/calvina.gaye/Downloads/FfDSdata/dowjones112624.csv')

print(jones.head())
shape: (5, 4)
┌─────────────────────────┬────────┬──────────┬─────────────┐
│ a                       ┆ Symbol ┆ Weight   ┆       Price │
│ ---                     ┆ ---    ┆ ---      ┆ ---         │
│ str                     ┆ str    ┆ f64      ┆ f64         │
╞═════════════════════════╪════════╪══════════╪═════════════╡
│ Unitedhealth Group Inc  ┆ UNH    ┆ 8.315803 ┆ 606.79      │
│ Goldman Sachs Group Inc ┆ GS     ┆ 8.27737  ┆ 605.5       │
│ Home Depot Inc          ┆ HD     ┆ 5.884052 ┆ 429.52      │
│ Microsoft Corp          ┆ MSFT   ┆ 5.748436 ┆ 427.99      │
│ Caterpillar Inc         ┆ CAT    ┆ 5.568073 ┆ 407.83      │
└─────────────────────────┴────────┴──────────┴─────────────┘

Distributional Data | Bar Graph

  • Bar Graph: most commonly used with categorical data for which each category has an associated quantity or measurement, a bar is drawn for each category, where the height or width of the bar is proportional to the associated quantity or measurement.

Visualizing Vectors | Distributional Data

  • Vectors may be used to indicate distributions or allocations across categories.

  • For example, an investor’s current net worth across different categories (such as stocks, bonds, and real estate) can be represented as a vector.

python code
import pandas as pd
import polars as pl
from great_tables import GT

animals_pd = pd.read_csv("polars/data/animals.csv", sep=",", header=0)
animals_pl = pl.read_csv("polars/data/animals.csv", separator=",", has_header=True)

animals_pd = animals_pd.drop(columns=["habitat", "diet", "features"])
animals_pd
animal class lifespan status weight
0 dolphin mammal 40 least concern 150.0
1 duck bird 8 least concern 3.0
2 elephant mammal 60 endangered 8000.0
3 ibis bird 16 least concern 1.0
4 impala mammal 12 least concern 70.0
5 kudu mammal 15 least concern 250.0
6 narwhal mammal 40 near threatened NaN
7 panda mammal 20 vulnerable 100.0
8 polar bear mammal 25 vulnerable 720.0
9 ray fish 20 NaN 90.0