# Essential Math for Machine Learning: Python Edition

Course source: [LinkedIn Learning](https://www.linkedin.com/learning/essential-math-for-machine-learning-python-edition)

## 1. Equations, Graphs, and Functions

### 1.1 Linear Equations

#### 1.1.1 Solving a Linear Equation
Consider the following equation:

\begin{equation}2y + 3 = 3x - 1 \end{equation}

After simplification: \begin{equation}y = \frac{3x - 4}{2} \end{equation}


```python
import pandas as pd

# Create a dataframe with an x column containing values from -10 to 10
df = pd.DataFrame ({'x': range(-10, 11)})

# Add a y column by applying the solved equation to x
df['y'] = (3*df['x'] - 4) / 2

#Display the dataframe
df
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>x</th>
      <th>y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-10</td>
      <td>-17.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-9</td>
      <td>-15.5</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-8</td>
      <td>-14.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-7</td>
      <td>-12.5</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-6</td>
      <td>-11.0</td>
    </tr>
    <tr>
      <th>5</th>
      <td>-5</td>
      <td>-9.5</td>
    </tr>
    <tr>
      <th>6</th>
      <td>-4</td>
      <td>-8.0</td>
    </tr>
    <tr>
      <th>7</th>
      <td>-3</td>
      <td>-6.5</td>
    </tr>
    <tr>
      <th>8</th>
      <td>-2</td>
      <td>-5.0</td>
    </tr>
    <tr>
      <th>9</th>
      <td>-1</td>
      <td>-3.5</td>
    </tr>
    <tr>
      <th>10</th>
      <td>0</td>
      <td>-2.0</td>
    </tr>
    <tr>
      <th>11</th>
      <td>1</td>
      <td>-0.5</td>
    </tr>
    <tr>
      <th>12</th>
      <td>2</td>
      <td>1.0</td>
    </tr>
    <tr>
      <th>13</th>
      <td>3</td>
      <td>2.5</td>
    </tr>
    <tr>
      <th>14</th>
      <td>4</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>15</th>
      <td>5</td>
      <td>5.5</td>
    </tr>
    <tr>
      <th>16</th>
      <td>6</td>
      <td>7.0</td>
    </tr>
    <tr>
      <th>17</th>
      <td>7</td>
      <td>8.5</td>
    </tr>
    <tr>
      <th>18</th>
      <td>8</td>
      <td>10.0</td>
    </tr>
    <tr>
      <th>19</th>
      <td>9</td>
      <td>11.5</td>
    </tr>
    <tr>
      <th>20</th>
      <td>10</td>
      <td>13.0</td>
    </tr>
  </tbody>
</table>
</div>



#### 1.1.2 Visualization


```python
%matplotlib inline
from matplotlib import pyplot as plt

plt.plot(df.x, df.y, color="grey", marker = "o")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.show()
```

#### 1.1.3 Intercepts

Let's take a look at the line from our linear equation with the X and Y axis shown through the origin (0,0).


```python
plt.plot(df.x, df.y, color="grey")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()

## add axis lines for 0,0
plt.axhline()
plt.axvline()
plt.show()
```


```python
plt.plot(df.x, df.y, color="grey")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()

## add axis lines for 0,0
plt.axhline()
plt.axvline()
## After calculation equation x-intercept: 4/3, y-intercept: -2
plt.annotate('x-intercept',(1.333, 0))
plt.annotate('y-intercept',(0,-2))
plt.show()
```

#### 1.1.4 Slope


```python
plt.plot(df.x, df.y, color="grey")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.axhline()
plt.axvline()

# set the slope
m = 1.5

# get the y-intercept
yInt = -2

# plot the slope from the y-intercept for 1x
mx = [0, 1]
my = [yInt, yInt + m]
plt.plot(mx,my, color='red', lw=5)

plt.show()
```

##### 1.1.4.1 Slope-Intercept Form

Now that we know the slope and y-intercept for the line that this equation defines, we can rewrite the equation as:

\begin{equation}y = 1\frac{1}{2}x + -2 \end{equation}


```python
# Create a dataframe with an x column containing values from -10 to 10
df = pd.DataFrame ({'x': range(-10, 11)})

# Define slope and y-intercept
m = 1.5
yInt = -2

# Add a y column by applying the slope-intercept equation to x
df['y'] = m*df['x'] + yInt

# Plot the line
from matplotlib import pyplot as plt

plt.plot(df.x, df.y, color="grey")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.axhline()
plt.axvline()

# label the y-intercept
plt.annotate('y-intercept',(0,yInt))

# plot the slope from the y-intercept for 1x
mx = [0, 1]
my = [yInt, yInt + m]
plt.plot(mx,my, color='red', lw=5)

plt.show()
```

### 1.2 Systems of Equations

Here are the equations

\begin{equation}x + y = 16 \end{equation}
\begin{equation}10x + 25y = 250 \end{equation}

Taken together, these equations form a *system of equations* that will enable us to determine how many of each chip denomination we have.

#### 1.2.1 Graphing Lines to Find the Intersection Point

One approach is to determine all possible values for x and y in each equation and plot them.

Let's plot each of these ranges of values as lines on a graph:


```python
# Get the extremes for number
x1 = [16, 0]
y1 = [0, 16]

# Get the extremes for values
x2 = [25,0]
y2 = [0,10]

# Plot the lines
plt.plot(x1, y1, color='blue')
plt.plot(x2, y2, color="orange")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()

plt.show()
```

### 1.3 Exponentials, Radicals, and Logs

#### 1.3.1 Exponentials

\begin{equation}2^{2} = 2 \cdot 2 = 4\end{equation}


```python
x = 2**2
print(x)
```

    4


#### 1.3.2 Radicals (Roots)

\begin{equation}\sqrt[3]{64} = 4 \end{equation}


```python
import math

# Calculate square root of 25
x = math.sqrt(25)
print (x)

# Calculate cube root of 64
cr = round(64 ** (1. / 3))
print(cr)
```

    5.0
    4


#### 1.3.3 Logarithms

\begin{equation}log_{4}(16) = 2 \end{equation}
\begin{equation}log(1000) = 3 \end{equation}
\begin{equation}log_{e}(64) = ln(64) = 4.1589 \end{equation}


```python
# log of 16 base 4
print(math.log(16, 4))

# Common log of 1000
print(math.log10(1000))

# Natural log of 64
print (math.log(64))
```

    2.0
    3.0
    4.1588830833596715


#### 1.3.4 Solving Equations with Exponentials

\begin{equation}2y = 2x^{4} ( \frac{x^{2} + 2x^{2}}{x^{3}} ) \end{equation}


\begin{equation}y = 3x^{3} \end{equation}

Now we have a solution that defines y in terms of x. We can use Python to plot the line created by this equation for a set of arbitrary *x* and *y* values:


```python
# Create a dataframe with an x column containing values from -10 to 10
df = pd.DataFrame ({'x': range(-10, 11)})

# Add a y column by applying the slope-intercept equation to x
df['y'] = 3*df['x']**3

#Display the dataframe
print(df)

plt.plot(df.x, df.y, color="magenta")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.axhline()
plt.axvline()
plt.show()
```

For Equation:
\begin{equation}
y = 2^{x}
\end{equation}


```python
# Create a dataframe with an x column containing values from -10 to 10
df = pd.DataFrame ({'x': range(-10, 11)})

# Add a y column by applying the slope-intercept equation to x
df['y'] = 2.0**df['x']

#Display the dataframe
print(df)

plt.plot(df.x, df.y, color="magenta")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.axhline()
plt.axvline()
plt.show()
```

### 1.4 Quadratic Equations

Consider the following equation:

\begin{equation}y = 2(x - 1)(x + 2)\end{equation}

If you multiply out the factored ***x*** expressions, this equates to:

\begin{equation}y = 2x^{2} + 2x - 4\end{equation}

Note that the highest ordered term includes a squared variable (x<sup>2</sup>).

Let's graph this equation for a range of ***x*** values:


```python
# Create a dataframe with an x column containing values to plot
df = pd.DataFrame ({'x': range(-9, 9)})

# Add a y column by applying the quadratic equation to x
df['y'] = 2*df['x']**2 + 2 *df['x'] - 4

plt.plot(df.x, df.y, color="grey")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.axhline()
plt.axvline()
plt.show()
```

For following **Parabola**

\begin{equation}y = -2x^{2} + 6x + 7\end{equation}


```python
# Create dataFrame for x column
df = pd.DataFrame({'x': range(-9,13)})

# Add y column
df['y'] = -2 * df['x'] ** 2 + 6 * df['x'] - 4

plt.plot(df.x, df.y, color="blue")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.axhline()
plt.axvline()
plt.show()
```

Again, the graph shows a parabola, but this time instead of being open at the top, the parabola is open at the bottom.

Equations that assign a value to ***y*** based on an expression that includes a squared value for ***x*** create parabolas. If the relationship between ***y*** and ***x*** is such that ***y*** is a *positive* multiple of the ***x<sup>2</sup>*** term, the parabola will be open at the top; when ***y*** is a *negative* multiple of the ***x<sup>2</sup>*** term, then the parabola will be open at the bottom.

These kinds of equations are known as *quadratic* equations, and they have some interesting characteristics. There are several ways quadratic equations can be written, but the *standard form* for quadratic equation is:

\begin{equation}y = ax^{2} + bx + c\end{equation}

Where ***a***, ***b***, and ***c*** are numeric coefficients or constants.

#### 1.4.1 Parabola Vertex and Line of Symmetry

Parabolas are symmetrical, with x and y values converging exponentially towards the highest point (in the case of a downward opening parabola) or lowest point (in the case of an upward opening parabola). The point where the parabola meets the line of symmetry is known as the *vertex*.


```python
import numpy as np

def plot_parabola(a, b, c):
    # get the x value for the line of symmetry
    vx = (-1*b)/(2*a)
    
    # get the y value when x is at the line of symmetry
    vy = a*vx**2 + b*vx + c

    # Create a dataframe with an x column containing values from x-10 to x+10
    minx = int(vx - 10)
    maxx = int(vx + 11)
    df = pd.DataFrame ({'x': range(minx, maxx)})

    # Add a y column by applying the quadratic equation to x
    df['y'] = a*df['x']**2 + b *df['x'] + c

    # get min and max y values
    miny = df.y.min()
    maxy = df.y.max()

    # Plot the line
    plt.plot(df.x, df.y, color="grey")
    plt.xlabel('x')
    plt.ylabel('y')
    plt.grid()
    plt.axhline()
    plt.axvline()

    # plot the line of symmetry
    sx = [vx, vx]
    sy = [miny, maxy]
    plt.plot(sx,sy, color='magenta')

    # Annotate the vertex
    plt.scatter(vx,vy, color="red")
    plt.annotate('vertex',(vx, vy), xytext=(vx - 1, (vy + 5)* np.sign(a)))

    plt.show()
```


```python
plot_parabola(2, 2, -4)   
plot_parabola(-2, 3, 5) 
```

#### 1.4.2 Parabola Intercepts

When y is 0, x is -2 or 1. Let's plot these points on our parabola:


```python
# Assign the calculated x values
x1 = -2
x2 = 1

# Create a dataframe with an x column containing some values to plot
df = pd.DataFrame ({'x': range(x1-5, x2+6)})

# Add a y column by applying the quadratic equation to x
df['y'] = 2*(df['x'] - 1) * (df['x'] + 2)

# Get x at the line of symmetry (halfway between x1 and x2)
vx = (x1 + x2) / 2

# Get y when x is at the line of symmetry
vy = 2*(vx -1)*(vx + 2)

# get min and max y values
miny = df.y.min()
maxy = df.y.max()

plt.plot(df.x, df.y, color="grey")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.axhline()
plt.axvline()

# Plot calculated x values for y = 0
plt.scatter([x1,x2],[0,0], color="green")
plt.annotate('x1',(x1, 0))
plt.annotate('x2',(x2, 0))

# plot the line of symmetry
sx = [vx, vx]
sy = [miny, maxy]
plt.plot(sx,sy, color='magenta')

# Annotate the vertex
plt.scatter(vx,vy, color="red")
plt.annotate('vertex',(vx, vy), xytext=(vx - 1, (vy - 5)))

plt.show()
```

#### 1.4.3 Solving Quadratics Using the Square Root Method

Let's consider this equation:

\begin{equation}y = 3x^{2} - 12\end{equation}

Let's restate it so we're solving for ***x*** when ***y*** is 0:

\begin{equation}3x^{2} - 12 = 0\end{equation}

\begin{equation}x = \pm\sqrt{4}\end{equation}

Let's see this in Python, and use the results to calculate and plot the parabola with its line of symmetry and vertex:


```python
y = 0
x1 = int(- math.sqrt(y + 12 / 3))
x2 = int(math.sqrt(y + 12 / 3))

# Create a dataframe with an x column containing some values to plot
df = pd.DataFrame ({'x': range(x1-10, x2+11)})

# Add a y column by applying the quadratic equation to x
df['y'] = 3*df['x']**2 - 12

# Get x at the line of symmetry (halfway between x1 and x2)
vx = (x1 + x2) / 2

# Get y when x is at the line of symmetry
vy = 3*vx**2 - 12

# get min and max y values
miny = df.y.min()
maxy = df.y.max()

plt.plot(df.x, df.y, color="grey")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.axhline()
plt.axvline()

# Plot calculated x values for y = 0
plt.scatter([x1,x2],[0,0], color="green")
plt.annotate('x1',(x1, 0))
plt.annotate('x2',(x2, 0))

# plot the line of symmetry
sx = [vx, vx]
sy = [miny, maxy]
plt.plot(sx,sy, color='magenta')

# Annotate the vertex
plt.scatter(vx,vy, color="red")
plt.annotate('vertex',(vx, vy), xytext=(vx - 1, (vy - 20)))

plt.show()
```

#### 1.4.4 Solving Quadratics Using the Completing the Square Method

Let's look at an example:

\begin{equation}y = x^{2} + 6x - 7\end{equation}

Let's start as we've always done so far by restating the equation to solve ***x*** for a ***y*** value of 0:

\begin{equation}x^{2} + 6x - 7 = 0\end{equation}

\begin{equation}x^{2} + 6x = 7\end{equation}

\begin{equation}x^{2} + 6x + 9 = 16\end{equation}

\begin{equation}(x + 3)^{2} = 16\end{equation}

\begin{equation}x + 3 =\pm\sqrt{16}\end{equation}

\begin{equation}x = -7, 1\end{equation}

Let's see what the parabola for this equation looks like in Python:


```python
x1 = int(- math.sqrt(16) - 3)
x2 = int(math.sqrt(16) - 3)

# Create a dataframe with an x column containing some values to plot
df = pd.DataFrame ({'x': range(x1-10, x2+11)})

# Add a y column by applying the quadratic equation to x
df['y'] = ((df['x'] + 3)**2) - 16

# Get x at the line of symmetry (halfway between x1 and x2)
vx = (x1 + x2) / 2

# Get y when x is at the line of symmetry
vy = ((vx + 3)**2) - 16

# get min and max y values
miny = df.y.min()
maxy = df.y.max()

plt.plot(df.x, df.y, color="grey")
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.axhline()
plt.axvline()

# Plot calculated x values for y = 0
plt.scatter([x1,x2],[0,0], color="green")
plt.annotate('x1',(x1, 0))
plt.annotate('x2',(x2, 0))

# plot the line of symmetry
sx = [vx, vx]
sy = [miny, maxy]
plt.plot(sx,sy, color='magenta')

# Annotate the vertex
plt.scatter(vx,vy, color="red")
plt.annotate('vertex',(vx, vy), xytext=(vx - 1, (vy - 10)))

plt.show()
```

#### 1.4.5 Vertex Form

Th *vertex form* of a quadratic equation which is generically described as:

\begin{equation}y = a(x - h)^{2} + k\end{equation}

The neat thing about this form of the equation is that it tells us the coordinates of the vertex - it's at ***h,k***.


```python
def plot_parabola_from_vertex_form(a, h, k):
    # Create a dataframe with an x column a range of x values to plot
    df = pd.DataFrame ({'x': range(h-10, h+11)})

    # Add a y column by applying the quadratic equation to x
    df['y'] = (a*(df['x'] - h)**2) + k

    # get min and max y values
    miny = df.y.min()
    maxy = df.y.max()

    # calculate y when x is 0 (h+-h)
    y = a*(0 - h)**2 + k

    plt.plot(df.x, df.y, color="grey")
    plt.xlabel('x')
    plt.ylabel('y')
    plt.grid()
    plt.axhline()
    plt.axvline()

    # Plot calculated y values for x = 0 (h-h and h+h)
    plt.scatter([h-h, h+h],[y,y], color="green")
    plt.annotate(str(h-h) + ',' + str(y),(h-h, y))
    plt.annotate(str(h+h) + ',' + str(y),(h+h, y))

    # plot the line of symmetry (x = h)
    sx = [h, h]
    sy = [miny, maxy]
    plt.plot(sx,sy, color='magenta')

    # Annotate the vertex (h,k)
    plt.scatter(h,k, color="red")
    plt.annotate('v=' + str(h) + ',' + str(k),(h, k), xytext=(h - 1, (k - 10)))

    plt.show()
```


```python
# Call the function for the example discussed above
plot_parabola_from_vertex_form(2, 4, -30)
```


```python
plot_parabola_from_vertex_form(3, -1, -1)
```

#### 1.4.6 Shortcuts for Solving Quadratic Equations

##### 1.4.6.1 The Quadratic Formula
Another useful formula to remember is the *quadratic formula*, which makes it easy to calculate values for ***x*** when ***y*** is **0**; or in other words:

\begin{equation}ax^{2} + bx + c = 0\end{equation}

Here's the formula:

\begin{equation}x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a}\end{equation}


```python
def plot_parabola_from_formula (a, b, c):
    # Get vertex
    print('CALCULATING THE VERTEX')
    print('vx = -b / 2a')

    nb = -b
    a2 = 2*a
    print('vx = ' + str(nb) + ' / ' + str(a2))

    vx = -b/(2*a)
    print('vx = ' + str(vx))

    print('\nvy = ax^2 + bx + c')
    print('vy = ' + str(a) + '(' + str(vx) + '^2) + ' + str(b) + '(' + str(vx) + ') + ' + str(c))

    avx2 = a*vx**2
    bvx = b*vx
    print('vy = ' + str(avx2) + ' + ' + str(bvx) + ' + ' + str(c))

    vy = avx2 + bvx + c
    print('vy = ' + str(vy))

    print ('\nv = ' + str(vx) + ',' + str(vy))

    # Get +x and -x (showing intermediate calculations)
    print('\nCALCULATING -x AND +x FOR y=0')
    print('x = -b +- sqrt(b^2 - 4ac) / 2a')


    b2 = b**2
    ac4 = 4*a*c
    print('x = ' + str(nb) + ' +- sqrt(' + str(b2) + ' - ' + str(ac4) + ') / ' + str(a2))

    sr = math.sqrt(b2 - ac4)
    print('x = ' + str(nb) + ' +- ' + str(sr) + ' / ' + str(a2))
    print('-x = ' + str(nb) + ' - ' + str(sr) + ' / ' + str(a2))
    print('+x = ' + str(nb) + ' + ' + str(sr) + ' / ' + str(a2))

    posx = (nb + sr) / a2
    negx = (nb - sr) / a2
    print('-x = ' + str(negx))
    print('+x = ' + str(posx))


    print('\nPLOTTING THE PARABOLA')

    # Create a dataframe with an x column a range of x values to plot
    df = pd.DataFrame ({'x': range(round(vx)-10, round(vx)+11)})

    # Add a y column by applying the quadratic equation to x
    df['y'] = a*df['x']**2 + b*df['x'] + c

    # get min and max y values
    miny = df.y.min()
    maxy = df.y.max()

    plt.plot(df.x, df.y, color="grey")
    plt.xlabel('x')
    plt.ylabel('y')
    plt.grid()
    plt.axhline()
    plt.axvline()

    # Plot calculated x values for y = 0
    plt.scatter([negx, posx],[0,0], color="green")
    plt.annotate('-x=' + str(negx) + ',' + str(0),(negx, 0), xytext=(negx - 3, 5))
    plt.annotate('+x=' + str(posx) + ',' + str(0),(posx, 0), xytext=(posx - 3, -10))

    # plot the line of symmetry
    sx = [vx, vx]
    sy = [miny, maxy]
    plt.plot(sx,sy, color='magenta')

    # Annotate the vertex
    plt.scatter(vx,vy, color="red")
    plt.annotate('v=' + str(vx) + ',' + str(vy),(vx, vy), xytext=(vx - 1, vy - 10))

    plt.show()
```


```python
plot_parabola_from_formula (2, -16, 2)
```

### 1.5 Functions

For example, consider the following equation:

\begin{equation}f(x) = x^{2} + 2\end{equation}


```python
def f(x):
    return x**2 + 2
```


```python
# Create an array of x values from -100 to 100
x = np.array(range(-100, 101))

# Set up the graph
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()

# Plot x against f(x)
plt.plot(x,f(x), color='purple')

plt.show()
```

#### 1.5.1 Bounds of a Function

For example, consider the function ***u*** defined here:

\begin{equation}u(x) = x + 1\end{equation}
\begin{equation}\{x \in \rm I\!R\}\end{equation}


##### 1.5.1.1 Domain of a Function

Now consider the following function ***g***:

\begin{equation}g(x) = (\frac{12}{2x})^{2}\end{equation}

This is interpreted as *Any value for x where x is in the set of real numbers such that x is not equal to 0*, and we can incorporate this into the function's definition like this:

\begin{equation}g(x) = (\frac{12}{2x})^{2}, \{x \in \rm I\!R\;\;|\;\; x \ne 0 \}\end{equation}

Or more simply:

\begin{equation}g(x) = (\frac{12}{2x})^{2},\;\; x \ne 0\end{equation}


```python
# Define function g
def g(x):
    if x != 0:
        return (12/(2*x))**2
```


```python
# Create an array of x values from -100 to 100
x = range(-100, 101)

# Get the corresponding y values from the function
y = [g(a) for a in x]

# Set up the graph
plt.xlabel('x')
plt.ylabel('g(x)')
plt.grid()

# Plot x against g(x)
plt.plot(x,y, color='purple')

# plot an empty circle to show the undefined point
plt.plot(0,g(0.0000001), color='purple', marker='o', markerfacecolor='w', markersize=8)

plt.show()
```

We can indicate the domain of this function in its definition like this:

\begin{equation}h(x) = 2\sqrt{x}, \{x \in \rm I\!R\;\;|\;\; x \ge 0 \}\end{equation}


```python
def h(x):
    if x >= 0:
        return 2 * np.sqrt(x)
```


```python
# Create an array of x values from -100 to 100
x = range(-100, 101)

# Get the corresponding y values from the function
y = [h(a) for a in x]

# Set up the graph
plt.xlabel('x')
plt.ylabel('h(x)')
plt.grid()

# Plot x against h(x)
plt.plot(x,y, color='purple')

# plot a filled circle at the end to indicate a closed interval
plt.plot(0, h(0), color='purple', marker='o', markerfacecolor='purple', markersize=8)

plt.show()
```

##### 1.5.1.2 Range of a Function

For example, consider the following function:

\begin{equation}p(x) = x^{2} + 1\end{equation}

It's range is:

\begin{equation}\{p(x) \in \rm I\!R\;\;|\;\; p(x) \ge 1 \}\end{equation}

Let's create and plot the function for a range of ***x*** values in Python:


```python
# define a function to return x^2 + 1
def p(x):
    return x**2 + 1
```


```python
# Create an array of x values from -100 to 100
x = np.array(range(-100, 101))

# Set up the graph
plt.xlabel('x')
plt.ylabel('p(x)')
plt.grid()

# Plot x against f(x)
plt.plot(x,p(x), color='purple')

plt.show()
```

d76092390a92e99052319336b188ad50dce137fa
ipynb
Jupyter Notebook
Codes/Essential Math for Machine Learning/1. Equations, Graphs, and Functions.ipynb
moni-roy/100DaysOfMLCode
5c1cfda650db20f3d08145a343b90d44252c8300
MIT
2020-05-22T02:03:32.000Z
2020-05-22T02:03:32.000Z
Codes/Essential Math for Machine Learning/1. Equations, Graphs, and Functions.ipynb
moni-roy/100DaysOfMLCode
5c1cfda650db20f3d08145a343b90d44252c8300
MIT
Codes/Essential Math for Machine Learning/1. Equations, Graphs, and Functions.ipynb
moni-roy/100DaysOfMLCode
5c1cfda650db20f3d08145a343b90d44252c8300
MIT
Qwen/Qwen-72B
1. YES
2. YES