<jupyter_start><jupyter_text>Data Science Fundamentals in Python   Softmax functionIn mathematics, the softmax function, or normalized exponential function,[1]:198 is a generalization of the logistic function that "squashes" a K-dimensional vector {\displaystyle \mathbf {z} } \mathbf {z}  of arbitrary real values to a K-dimensional vector {\displaystyle \sigma (\mathbf {z} )} \sigma (\mathbf {z} ) of real values, where each entry is in the range (0, 1], and all the entries add up to 1. https://en.wikipedia.org/wiki/Softmax_function<jupyter_code>import numpy as np
z = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0]
softmax = lambda x : np.exp(x)/np.sum(np.exp(x))
softmax(z)<jupyter_output><empty_output><jupyter_text>Monte Carlo methodMonte Carlo methods (or Monte Carlo experiments) are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. Their essential idea is using randomness to solve problems that might be deterministic in principle. They are often used in physical and mathematical problems and are most useful when it is difficult or impossible to use other approaches. Monte Carlo methods are mainly used in three problem classes: **optimization**, **numerical integration**, and **generating draws from a probability distribution**.<jupyter_code># from https://github.com/dandrewmyers/numerical/blob/master/mc_pi.py

#This is numerical estimate of pi based on Monte Carlo method using unit circle inscribed in rectangle.
#Outputs pi estimate in terminal and also plot based on number of random points inputed by user.
#This code based on previous version at https://gist.github.com/louismullie/3769218.

import numpy as np
import matplotlib.pyplot as plt
import time as time

# input total number of random points
total_random_points = int(input("\nNumber of random points for Monte Carlo estimate of Pi?\n>"))

# start time of calculation
start_time = time.time()

# number of random points inside unit cicrle and total random points
inside_circle = 0

# create empty x and y arrays for eventual scatter plot of generated random points
x_plot_array = np.empty(shape=(1,total_random_points))
y_plot_array = np.empty(shape=(1,total_random_points))

# generate random points and count points inside unit circle
# top right quadrant of unit cicrle only
for i in range(0, total_random_points):
    # print(f'\nIteration: {i + 1}')
    # generate random x, y in range [0, 1]
    x = np.random.rand()
    x_plot_array = np.append(x_plot_array, [x])
    #print(f'{x_plot_array}')
    # print(f'x value: {x}')
    y = np.random.rand()
    y_plot_array = np.append(y_plot_array, [y])
    #print(f'{y_plot_array}')
    # print(f'y value: {y}')
    # calc x^2 and y^2 values
    x_squared = x**2
    y_squared = y**2
    # count if inside unit circle, top right quadrant only
    if np.sqrt(x_squared + y_squared) < 1.0:
        inside_circle += 1
    # print(f'Points inside circle {inside_circle}')
    # print(f'Number of random points {i+1}')
    # calc approximate pi value
    pi_approx = inside_circle / (i+1) * 4
    # print(f'Approximate value for pi: {pi_approx}')

# final numeric output for pi estimate
print ("\n--------------")
print (f"\nApproximate value for pi: {pi_approx}")
print (f"Difference to exact value of pi: {pi_approx-np.pi}")
print (f"Percent Error: (approx-exact)/exact*100: {(pi_approx-np.pi)/np.pi*100}%")
print (f"Execution Time: {time.time() - start_time} seconds\n")

# plot output of random points and circle, top right quadrant only
random_points_plot = plt.scatter(x_plot_array, y_plot_array, color='blue', s=.1)
circle_plot = plt.Circle( ( 0, 0 ), 1, color='red', linewidth=2, fill=False)

ax = plt.gca()
ax.cla()

ax.add_artist(random_points_plot)
ax.add_artist(circle_plot)

plt.show()<jupyter_output>Number of random points for Monte Carlo estimate of Pi?
>5000

--------------

Approximate value for pi: 3.1344
Difference to exact value of pi: -0.007192653589793263
Percent Error: (approx-exact)/exact*100: -0.2289492745526527%
Execution Time: 0.1114964485168457 seconds
{'fraction_non_alphanumeric': 0.11042793900639448, 'fraction_numerical': 0.029758976881455977, 'mean_word_length': 4.473755047106326, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 3, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '46592', 'license': 'Apache-2.0', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1267, 'n_words': 466, 'path': 'lab/Data Science Fundamentals in Python.ipynb', 'repo_name': 'reiserwang/data-science-ipython-notebooks', 'size': '4066'}