<jupyter_start><jupyter_text>TasksThese are my solutions to the Tasks assessment. The author is <NAME>. (<EMAIL>). ***  Task 1: _Counts_A Python function that takes a list as input and returns a dictionary of unique items in the list as keys and the number of times (frequency) each item appears as values.[1]  https://www.geeksforgeeks.org/counting-the-frequencies-in-a-list-using-dictionary-in-python/[2] https://stackoverflow.com/questions/3496518/using-a-dictionary-to-count-the-items-in-a-list<jupyter_code># [2]
# Generating a list
items = [1, 1, 5, 5, 3, 1, 3, 3, 4, 4, 2, 2, 2, "A", "A", "B", "a", "red", 25.0] 

# Creating the function.
counts = dict()
for i in items:
  counts[i] = counts.get(i, 0) + 1

print("The Dictionary has the following items with number of occurences:\n",counts)
def Count(my_list): 

  # Creating an empty dictionary 
  freq = {} 
  for item in my_list: 
    if (item in freq): 
      freq[item] += 1
    else: 
      freq[item] = 1

  for key, value in freq.items(): 
    print("%s  : %d "%(key, value))
  

my_list =  [1, 1, 5, 5, 3, 1, 3, 3, 4, 4, 2, 2, 2, "A", "red", 25.0]

Count(my_list)<jupyter_output>1  : 3 
5  : 2 
3  : 3 
4  : 2 
2  : 3 
A  : 1 
red  : 1 
25.0  : 1<jupyter_text>*** Task 2: _Dice roller_A Python function called dicerolls that simulates rolling dice. The function takes two parameters: the number of dice `k = 2` andthe number of times to roll the dice `n = 1000`.[1] stack overflow; https://stackoverflow.com/questions/60343980/rolling-2-dice-1000-times-and-counting-the-number-of-times-the-sum-of-the-two-di[2] reddit;  https://www.reddit.com/r/learnpython/comments/55zdez/putting_outcomes_of_dice_roll_in_dictionary/[3] AoPS Online;  https://artofproblemsolving.com/wiki/index.php/Basic_Programming_With_Python<jupyter_code># We can simulate the Dice Roll with the following code:

from collections import defaultdict
from random import randint

# Roll the two dice how many times?
n = 1000

# Create a dictionary to store the results
results = defaultdict(int)

# Loop n times
for _ in range(n):
    # Get random numbers for the two dice
    die_1 = randint(1, 6)
    die_2 = randint(1, 6)
    # Increase the corresponding result by 1
    results[die_1 + die_2] += 1

# Print results
print("Rolling two dice for a thousand times gives us the following results:\n\n",results)
# Another way to simulate a dice roll.
# we have two dice, k= 2

import collections
import random


def diceroll(rolls, sides):
    dice = defaultdict(int)
    for x in range(rolls):
        dice[random.randint(2, sides)] += 1
    return(dice)
diceroll(1000, 12)
print("Rolling two dice for a thousand times gives us the following results:\n\n",diceroll(1000, 12))<jupyter_output>Rolling two dice for a thousand times gives us the following results:

 defaultdict(<class 'int'>, {4: 89, 6: 84, 5: 89, 7: 104, 11: 81, 3: 98, 2: 97, 10: 87, 12: 79, 8: 91, 9: 101})<jupyter_text>*** Task 3: _binomial distribution_$$P(N) = \binom{n}{N}p^N(1-p)^{n-N}$$`Where n is the number of trials, p is the probability of success, and N is the number of successes.`Definition of binomial distribution: the binomial distribution is the probability distribution of a sequence of experiments where each experiment produces a binary outcome (in this exapmle, flipping a coin), and where each of the outcomes is independent of all the others.[2][1] https://numpy.org/doc/stable/reference/random/generated/numpy.random.binomial.html[2] towards data science ; https://towardsdatascience.com/fun-with-the-binomial-distribution-96a5ecabf65b<jupyter_code># Result of flipping a coin 100 times, tested 1000 times

import numpy as np
from numpy.random import default_rng # as per latest release of numpy package
rng = default_rng()
import matplotlib.pyplot as plt 
import seaborn as sns

n = 100     # number of independent experiments in each trial
p = 0.5     # probability of success for each experiment (heads or tales)
k = 1000    # number of trials

h = rng.binomial (n, p, k)
h
# Function that runs our coin toss trials
# heads is a list of the number of successes from each trial of n experiments

def run_binom(k, n, p):
    heads = []
    for i in range(k):
        tosses = [rng.random() for i in range(n)]
        heads.append(len([i for i in tosses if i>=0.50]))
    return heads
# Run the function
heads = run_binom(k, n, p)
# Plot the results as a histogram
fig, ax = plt.subplots(figsize=(14,7))
ax = sns.distplot(heads, bins=11, label='simulation results')
ax.set_xlabel("Number of Heads",fontsize=16)
ax.set_ylabel("Frequency",fontsize=16)
# Plot the actual binomial distribution as a sanity check
from scipy.stats import binom
x = range(0,101)
ax.plot(x, binom.pmf(x, n, p), 'ro', label='actual binomial distribution')
ax.vlines(x, 0, binom.pmf(x, n, p), colors='r', lw= 3, alpha=0.5)
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>*** Task 4 : _Simpson's paradox_<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("ggplot")
plt.rcParams["figure.figsize"] = (18, 10)

# Creating a standard data set
# x ,  y = a * x + b
a = 3.0
b = 2.0
x = np.linspace (0.0, 10.0, 50)

x
len(x)
y = 3.0 * x + 2.0
y
len(y)
plt.plot(x, y, "-")
coeffs = np.polyfit(x, y, 1)
coeffs
# Add some noise
y = 3.0 * x + 2.0 + np.random.normal(0.0, 1.0, len(x))
plt.plot(x, y, ".")
# coeffecients
coeffs = np.polyfit(x, y, 1)
coeffs
plt.plot(x, y, ".", label = "Data")
plt.plot(x, coeffs[0] * x + coeffs[1], "-", label = "Best fit")
plt.legend()<jupyter_output><empty_output><jupyter_text>Creating four data sets: - **First data set x1, y1**<jupyter_code># Creating 1st data set.
# x2 ,  y2 = a * x + b

a = 3.0
b = 2.0
x1 = np.linspace(0.0, 0.5, 300)
y1 = 3.0 * x1 + 2.0 + np.random.normal(0.0, 1.0, len(x1))
coeffs1 = np.polyfit(x1, y1, 1)
coeffs1<jupyter_output><empty_output><jupyter_text>- **Second data set x2, y2**<jupyter_code># Creating 2nd data set.
# x2 ,  y2 = a * x + b

a = 6.0
b = 5.0
x2 = np.linspace(0.0, 0.5, 300)
y2 = 6.0 * x2 + 5.0 + np.random.normal(0.0, 1.0, len(x2))
coeffs2 = np.polyfit(x2, y2, 1)
coeffs2<jupyter_output><empty_output><jupyter_text>- **Third data set x3, y3**<jupyter_code># Creating 3rd data set.
# x3 ,  y3 = a * x + b

a = 50.0
b = 20.0
x3 = np.linspace(0.0, 0.3, 1000)
y3 = a * x3 + b + np.random.normal(0.0, 1.0, len(x3))
coeffs3 = np.polyfit(x3, y3, 1)
coeffs3<jupyter_output><empty_output><jupyter_text>- **Fourth data set x4, y4**<jupyter_code># Creating 4th data set.
# x4 ,  y4 = a * x + b

a = 8.0
b = 11.0
x4 = np.linspace(0.0, 0.5, 300)
y4 = a * x4 + b + np.random.normal(0.0, 1.0, len(x4))
coeffs4 = np.polyfit(x4, y4, 1)
coeffs4
# First data set.
plt.plot(x1, y1, ".", label = "Data set 1")
plt.plot(x1, coeffs1[0] * x1 + coeffs1[1], "-", label = "Best fit line 1")

# Second data set.
plt.plot(x2, y2, ".", label = "Data set 2")
plt.plot(x2, coeffs2[0] * x2 + coeffs2[1], "-", label = "Best fit line 2")

# Third data set.
plt.plot(x3, y3, ".", label = "Data set 3")
plt.plot(x3, coeffs3[0] * x3 + coeffs3[1], "-", label = "Best fit line 3")

# Forth data set.
plt.plot(x4, y4, ".", label = "Data set 4")
plt.plot(x4, coeffs4[0] * x4 + coeffs4[1], "-", label = "Best fit line 4")
plt.legend()<jupyter_output><empty_output><jupyter_text>- **Combine data sets**<jupyter_code>x = np.concatenate([x1, x2, x3, x4])
y = np.concatenate([y1, y2, y3, y4])
coeffs = np.polyfit(x, y, 1)
coeffs
# First data set.
plt.plot(x1, y1, ".", label = "Data set 1")
plt.plot(x1, coeffs1[0] * x1 + coeffs1[1], "-", label = "Best fit line 1")

# Second data set.
plt.plot(x2, y2, ".", label = "Data set 2")
plt.plot(x2, coeffs2[0] * x2 + coeffs2[1], "-", label = "Best fit line 2")

# Third data set.
plt.plot(x3, y3, ".", label = "Data set 3")
plt.plot(x3, coeffs3[0] * x3 + coeffs3[1], "-", label = "Best fit line 3")

# Fourth data set.
plt.plot(x4, y4, ".", label = "Data set 4")
plt.plot(x4, coeffs4[0] * x4 + coeffs4[1], "-", label = "Best fit line 4")

# combined data sets
# plt.plot(x, y, ".", label = " Combined data sets")
plt.plot(x, coeffs[0] * x + coeffs[1], "+", label = "Best fit line for combined data sets")
plt.xlabel("X")
plt.title("We can notice when data sets are combined that the fit -Regression- line is REVERSED, which is an evidence of the Simpson's Paradox")

plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15307101727447217, 'fraction_numerical': 0.054102687140115166, 'mean_word_length': 3.9273049645390072, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 39, 'https://': 7, 'lorem ipsum': 0, 'www.': 2, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '536938', 'license': 'MIT', 'n_tokens_mistral': 3581, 'n_tokens_neox': 3280, 'n_words': 1025, 'path': 'Tasks.ipynb', 'repo_name': 'G00387867/FDA', 'size': '8346'}