<jupyter_start><jupyter_text>Exercise 4.12    This problem involves writing functions.            Write a function, Power(), that prints out the result of raising $2$ to the $3$rd power. In other words, your function should compute $2^3$ and print out the results. Hint: Recall that $x^a$ raises $x$ to the power $a$. Use the print() function to output the result.        Create a new function, Power2(), that allows you to pass anytwo numbers, $x$ and $a$, and prints out the value of $x^a$. You cando this by beginning your function with the line > Power2=function(x,a) {You should be able to call your function by entering, for instance,> Power2(3,8)on the command line. This should output the value of $3^8$, namely, $6,561$.        Using the Power2() function that you just wrote, compute $10^3$, $8^{17}$, and $131^3$.        Now create a new function, Power3(), that actually returns the result $x^a$ as an R object, rather than simply printing it to the screen. That is, if you store the value $x^a$ in an object called result within your function, then you can simply return() this return() result, using the following line:return(result)The line above should be the last line in your function, before the } symbol.        Now using the Power3() function, create a plot of $f(x)=x^2$. The $x$-axis should display a range of integers from $1$ to $10$, and the $y$-axis should display $x^2$. Label the axes appropriately, and use an appropriate title for the figure. Consider displaying either the $x$-axis, the $y$-axis, or both on the log-scale. You can do this by using log=‘‘x’’, log=‘‘y’’, or log=‘‘xy’’ as arguments to the plot() function.        Create a function, PlotPower(), that allows you to create a plot of $x$ against $x^a$ for a fixed $a$ and for a range of values of $x$. For instance, if you call> PlotPower(1:10,3)then a plot should be created with an $x$-axis taking on values $1, 2, \ldots, 10$, and a $y$-axis taking on values $1^3, 2^3, \ldots, 10^3$.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

# https://stackoverflow.com/questions/34398054/ipython-notebook-cell-multiple-outputs
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"<jupyter_output><empty_output><jupyter_text>Exercise 4.12.1    Write a function, Power(), that prints out the result of raising $2$ to the $3$rd power. In other words, your function should compute $2^3$ and print out the results. Hint: Recall that $x^a$ raises $x$ to the power $a$. Use the print() function to output the result.<jupyter_code>def power():
    print(f'2^3 = {2**3}')
    
power()<jupyter_output>2^3 = 8<jupyter_text>Exercise 4.12.2    Create a new function, Power2(), that allows you to pass anytwo numbers, $x$ and $a$, and prints out the value of $x^a$. You cando this by beginning your function with the line > Power2=function(x,a) {You should be able to call your function by entering, for instance,> Power2(3,8)on the command line. This should output the value of $3^8$, namely, $6,561$.<jupyter_code>def power2(base, exponent):
    print(f'{base}^{exponent} = {base**exponent}')
    
power2(3, 8)<jupyter_output>3^8 = 6561<jupyter_text>Exercise 4.12.3    Using the Power2() function that you just wrote, compute $10^3$, $8^{17}$, and $131^3$.<jupyter_code>power2(10, 3)
power2(8, 17)
power2(131, 3)<jupyter_output>10^3 = 1000
8^17 = 2251799813685248
131^3 = 2248091<jupyter_text>Exercise 4.12.4    Now create a new function, Power3(), that actually returns the result $x^a$ as an R object, rather than simply printing it to the screen. That is, if you store the value $x^a$ in an object called result within your function, then you can simply return() this return() result, using the following line:return(result)The line above should be the last line in your function, before the } symbol.<jupyter_code>def power3(base, exponent):
    return base**exponent

result = power3(3, 8)
print(result)<jupyter_output>6561<jupyter_text>Exercise 4.12.5    Now using the Power3() function, create a plot of $f(x)=x^2$. The $x$-axis should display a range of integers from $1$ to $10$, and the $y$-axis should display $x^2$. Label the axes appropriately, and use an appropriate title for the figure. Consider displaying either the $x$-axis, the $y$-axis, or both on the log-scale. You can do this by using log=‘‘x’’, log=‘‘y’’, or log=‘‘xy’’ as arguments to the plot() function.<jupyter_code>x = np.arange(1, 11)
y = power3(x, 2)
fig, ax = plt.subplots(constrained_layout=True, figsize=(9, 6))
_ = ax.plot(x, y, '-', linewidth=2)
_ = ax.set_xlabel(r'$x$')
_ = ax.set_ylabel(r'$x^2$')
_ = ax.set_title(r'$f(x) = x^2$')
_ = ax.grid(True)

fig, ax = plt.subplots(constrained_layout=True, figsize=(9, 6))
_ = ax.plot(x, np.log(y), '-', linewidth=2)
_ = ax.set_xlabel(r'$x$')
_ = ax.set_ylabel(r'$\ln(x^2)$')
_ = ax.set_title(r'$f(x) = \ln(x^2)$')
_ = ax.grid(True)<jupyter_output><empty_output><jupyter_text>Exercise 4.12.6    Create a function, PlotPower(), that allows you to create a plot of $x$ against $x^a$ for a fixed $a$ and for a range of values of $x$. For instance, if you call> PlotPower(1:10,3)then a plot should be created with an $x$-axis taking on values $1, 2, \ldots, 10$, and a $y$-axis taking on values $1^3, 2^3, \ldots, 10^3$.<jupyter_code>def plotPower(x_begin, x_end, exponent):
    x = np.arange(x_begin, x_end + 1)
    y = x**exponent
    fig, ax = plt.subplots(constrained_layout=True, figsize=(9, 6))
    _ = ax.plot(x, y, '-', linewidth=2)
    _ = ax.set_xlabel(r'$x$')
    _ = ax.set_ylabel(f'x^{exponent}')
    _ = ax.set_title(f'f(x) = x^{exponent}')
    _ = ax.grid(True)

plotPower(1, 10, 3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1331345378003859, 'fraction_numerical': 0.04016839151026136, 'mean_word_length': 4.590196078431372, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '434353', 'license': 'MIT', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1928, 'n_words': 731, 'path': 'Chapter04/Exercise12/4_12.ipynb', 'repo_name': 'HerrBroekman/Applied-solution-of-An-Introduction-to-Statistical-Learning-written-in-Python', 'size': '5701'}