# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Importing packages

# Throughout this tutorial, we will use the following common Python packages:

# Use these packages to easily access files on your hard drive
import os, sys, glob
# The Numpy package allows you to manipulate data (mainly numerical)
import numpy as np
# The Pandas package allows more advanced data manipulation e.g. in structured data frames
import pandas as pd
# The Matplotlib package is for plotting - uses the same syntax as plotting in Matlab (figures, axes etc)
import matplotlib.pyplot as plt
# Seaborn is a higher-level package for plotting that calls functions in Matplotlib,
# you can usually input your Pandas dataframes to get pretty plots in 1 or 2 lines
import seaborn as sns
# We will use Scipy for advanced computation like model fitting
import scipy

# ## Solutions

# #### 1. Create two lists that separate numbers (eg. from 1-100) divisible by 3 and numbers not divisible by 3.

y1 = []
y2 = []
for x in np.arange(1,100):
    if x%3 == 0:
        #if divisible by 3, put the number in y1
        y1.append(x)
    else:
        #if not divisible by 3, put the number in y1
        y2.append(x)
# display(y1)
# display(y2)

# #### 2. Keep generating random numbers until a generated number is greater than 0.8 and store the number of times it takes you to get this number

u = np.random.rand()
n = 1
while u < 0.8:
    u = np.random.rand()
    n = n+1
display(u)
display(n)

# #### 3. Generate some random data in two variables of equal length and make a scatter plot using matplotlib

var1 = np.random.rand(100)
var2 = np.random.rand(100)
plt.scatter(var1,var2)

# #### 4. Generate some data for a linear relationship between two variables (e.g. age and height of schoolchildren), put them in a Pandas dataframe with 2 named columns, and use Seaborn to create a scatterplot with regression line

age = 5 + np.random.rand(100)*7
height = 108 + (152-108)*((age-5)/7) + np.random.randn(100)*20
age_height = pd.DataFrame.from_dict({'age':age,'height':height}).sort_values(by=['age','height'])
display(age_height.head())

sns.regplot(data = age_height, x = 'age', y = 'height')

# #### 5. Create a Pandas dataframe with height data for 5 age groups and use Seaborn to turn this into a barplot with errorbars and an overlaid stripplot or swarmplot.

age_height['group'] = age_height['age'].apply(lambda x: np.floor(x)-4)
age_height

sns.barplot(data = age_height.query('group < 6'), x = 'group', y = 'height', alpha = .5)
sns.swarmplot(data = age_height.query('group < 6'), x = 'group', y = 'height')

StarcoderdataJupyterScriptsDedupFiltered