# ---
# 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
# ---

import numpy as np
from time import time

# +
num_elements = 10000000

my_list = list(range(num_elements))

my_array = np.array(range(num_elements))

# +
tick = time()

myList = [x + 1 for x in my_list]

print(f"Time taken by the list is {time()-tick}")

# +
tick = time()

my_array += 1

print(f"Time taken by the list is {time()-tick}")
# -

test_scores = [70,65,95,88]
type(test_scores)

score = np.array(test_scores)
type(score)

score.mean()

# +
import numpy as np

income = np.array([75000, 55000, 88000, 125000, 64000, 97000])
# -

income.mean()

income = np.append(income, 12000000)
income

income.mean()

np.median(income)

income.std()

test_score = [70, 65, 95, 88]

scores = np.array(test_score)
type(scores)

scores.std()

scores.max()

scores.min()

scores.sum()

np.random.seed(seed=60)
random_square = np.random.rand(5, 5)
random_square

random_square[0]

random_square[0, :]

random_square[:, 0]

random_square[0, 0]

random_square[0][0]

random_square[2, 3]

random_square.mean()

random_square[0].mean()

random_square[:, -1].mean()

# %%time
np.random.seed(seed=60)
big_matrix = np.random.rand(100000, 100)

# %%time
big_matrix = np.random.rand(100000, 100)
big_matrix.mean()

np.arange(1, 101)

np.arange(1, 101).reshape(20, 5)

mat1 = np.arange(1, 101).reshape(20, 5)
mat1 - 50

mat1 * 10

mat1 + mat1

mat1 * mat1

np.dot(mat1, mat1.T)

import pandas as pd

test_dict = {'Corey':[63,75,88], 'Kevin':[48,98,92], 'Akshay': [87, 86, 85]}

df = pd.DataFrame(test_dict)
df

df = df.T
df

df.columns = ['Quiz_1', 'Quiz_2', 'Quiz_3']
df

df.iloc[0]

df.iloc[0,:]

df['Quiz_1']

df.Quiz_1

df.iloc[:, 0]

df = pd.DataFrame(test_dict)
df

df[0:2]

df = df.T
df

df.columns = ['Quiz_1', 'Quiz_2', 'Quiz_3']
df

# +
rows = ['Corey', 'Kevin']
cols = ['Quiz_2', 'Quiz_3']

df_spring = df.loc[rows, cols]
df_spring
# -

df.iloc[[0,1], [1,2]]

df['Quiz_Avg'] = df.mean(axis=1)
df

df['Quiz_4'] = [92, 95, 88]
df

del df['Quiz_Avg']
df



df

# +
df_new = pd.DataFrame({
    'Quiz_1':[np.NaN], 
    'Quiz_2':[np.NaN], 
    'Quiz_3': [np.NaN], 
    'Quiz_4':[71]}, index=['Adrian'])

df_new
# -

df = pd.concat([df, df_new])
df

df['Quiz_Avg'] = df.mean(axis=1, skipna=True)
df

df.describe()

df.info()

df['Quiz_4'].astype(float)



import pandas as pd

housing_df = pd.read_csv('boston_housing.csv')

housing_df

housing_df.info()

housing_df.describe()

housing_df.shape

housing_df.isnull().any()

housing_df.loc[:, housing_df.isnull().any()]

housing_df.loc[:, housing_df.isnull().any()].describe()

housing_df['AGE'] = housing_df['AGE'].fillna(housing_df.mean())

housing_df['CHAS'] = housing_df['CHAS'].fillna(0)

housing_df = housing_df.fillna(housing_df.median())

housing_df.info()



# +
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# %matplotlib inline
# -

housing_df = pd.read_csv('boston_housing.csv')

sns.set()

title = 'Median Boston Housing Prices'
plt.figure(figsize=(10,6))
plt.hist(housing_df['MEDV'])
plt.title(title, fontsize=15)
plt.xlabel('1980 Median Value in Thousands')
plt.ylabel('Count')
plt.savefig(title, dpi=300)
plt.show()


def my_hist(column, title, xlab, ylab):
    plt.figure(figsize=(10,6))
    plt.hist(column)
    plt.title(title, fontsize=15)
    plt.xlabel(xlab)
    plt.ylabel(ylab)
    plt.savefig(title, dpi=300)
    plt.show()


my_hist(housing_df['RM'], 'Average Number of Rooms in Boston Households', 'Average Number of Rooms', 'Count')


def my_hist(column, title, xlab, ylab, bins=10, alpha=0.7, color='c'):
    title = title
    plt.figure(figsize=(10,6))
    plt.hist(column, bins=bins, range=(3,9), alpha=alpha, color=color)
    plt.title(title, fontsize=15)
    plt.xlabel(xlab)
    plt.ylabel(ylab)
    plt.savefig(title, dpi=300)
    plt.show()


my_hist(housing_df['RM'], 'Average Number of Rooms in Boston', 'Average Number of Rooms', 'Count', bins=6)

x = housing_df['RM']
y = housing_df['MEDV']
plt.scatter(x, y)
plt.show()

# +
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# %matplotlib inline
# -

housing_df = pd.read_csv('boston_housing.csv')

housing_df.corr()

sns.set()

corr = housing_df.corr()
plt.figure(figsize=(8,6))
sns.heatmap(
    corr, 
    xticklabels=corr.columns.values,
    yticklabels=corr.columns.values,
    cmap='Blues',
    linewidths=1.25,
    alpha=0.8
)
plt.show()

plt.figure(figsize=(10,7))
sns.regplot(x,y)
plt.show()

# +
import statsmodels.api as sm


X = sm.add_constant(x)
model = sm.OLS(y, x)
est = model.fit()

print(est.summary())
# -



x = housing_df['RM']
y = housing_df['MEDV']
plt.boxplot(x)
plt.show()

plt.violinplot(x)
plt.show()

# ### Activity 24

# +
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# %matplotlib inline
sns.set()
# -

ukstats_df = pd.read_csv('UKStatistics.csv')

ukstats_df.head()

ukstats_df.info()

ukstats_df.describe()

ukstats_df.shape

plt.hist(ukstats_df['Actual Pay Floor (£)'])
plt.show()

x = ukstats_df['Salary Cost of Reports (£)']
y = ukstats_df['Actual Pay Floor (£)']
plt.scatter(x, y)
plt.show()

plt.violinplot(x)
plt.show()

plt.boxplot(x)
plt.show()



StarcoderdataJupyterScriptsDedupFiltered