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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
st.title(":green[Types of Data Analysis]")
|
| 5 |
+
|
| 6 |
+
text= """There are four types of data analysis that are in use across all industries.
|
| 7 |
+
While we separate these into categories, they are all linked together and build upon each other.
|
| 8 |
+
As you begin moving from the simplest type of analytics to more complex, the degree of difficulty and resources required increases.
|
| 9 |
+
At the same time, the level of added insight and value also increases."""
|
| 10 |
+
st.write(text)
|
| 11 |
+
st.write("The four types of data analysis are:")
|
| 12 |
+
st.graphviz_chart('''
|
| 13 |
+
digraph {
|
| 14 |
+
node [shape=rect]
|
| 15 |
+
Data Analysis -> Descriptive Analysis
|
| 16 |
+
Data Analysis -> Diagnostic Analysis
|
| 17 |
+
Data Analysis -> Predictive Analysis
|
| 18 |
+
Data Analysis -> Prescriptive Analysis
|
| 19 |
+
}
|
| 20 |
+
''')
|
| 21 |
+
|
| 22 |
+
text1="""The first type of data analysis is descriptive analysis. It is at the foundation of all data insight,
|
| 23 |
+
Descriptive analysis answers the :blue-background[“what happened”] by summarizing past data, usually in the form of dashboards. """
|
| 24 |
+
st.subheader(":red[Descriptive Analysis:]")
|
| 25 |
+
st.write(text1)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
st.write("**Example:** Sales Data Analysis")
|
| 29 |
+
|
| 30 |
+
st.write("**Purpose:** Understand what has happened in the past.")
|
| 31 |
+
|
| 32 |
+
st.write("**Process:**")
|
| 33 |
+
text2=""" - Collect sales data for the past year.
|
| 34 |
+
- Summarize key metrics such as total sales, average sales per month, and sales distribution across different regions."""
|
| 35 |
+
st.write(text2)
|
| 36 |
+
st.write("**steps:**")
|
| 37 |
+
st.write("Gather data:")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
code=(''' import pandas as pd
|
| 41 |
+
# Sample sales data
|
| 42 |
+
data = {
|
| 43 |
+
'Month': ['January', 'February', 'March', 'April', 'May'],
|
| 44 |
+
'Sales': [200, 220, 250, 210, 270]
|
| 45 |
+
}
|
| 46 |
+
df = pd.DataFrame(data) ''')
|
| 47 |
+
st.code(code,language='python')
|
| 48 |
+
|
| 49 |
+
st.write("Calculate summary statistics:")
|
| 50 |
+
st.code(''' total_sales = df['Sales'].sum()
|
| 51 |
+
average_sales = df['Sales'].mean()
|
| 52 |
+
monthly_distribution = df['Sales']
|
| 53 |
+
print(f"Total Sales: {total_sales}")
|
| 54 |
+
print(f"Average Sales per Month: {average_sales}")
|
| 55 |
+
print("Monthly Sales Distribution:")
|
| 56 |
+
print(monthly_distribution)''')
|
| 57 |
+
|
| 58 |
+
st.write("**Output:**")
|
| 59 |
+
st.code(''' Total Sales: 1150
|
| 60 |
+
Average Sales per Month: 230.0
|
| 61 |
+
Monthly Sales Distribution:
|
| 62 |
+
0 200
|
| 63 |
+
1 220
|
| 64 |
+
2 250
|
| 65 |
+
3 210
|
| 66 |
+
4 270
|
| 67 |
+
Name: Sales, dtype: int64''')
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# Diagnostic Analysis
|
| 71 |
+
st.subheader(":gray[Diagnostic Analysis:]")
|
| 72 |
+
text3="""After asking the main question of “what happened”, the next step is to dive deeper and
|
| 73 |
+
ask :blue-background[why did it happen?] This is where diagnostic analysis comes in """
|
| 74 |
+
text4=""" Diagnostic analysis takes the insights found from descriptive analytics and drills down to find the causes of those outcomes.
|
| 75 |
+
Organizations make use of this type of analytics as it creates more connections between data and identifies patterns of behavior."""
|
| 76 |
+
text5="""By already having the data at your disposal, it ends having to repeat work and makes all problems interconnected."""
|
| 77 |
+
st.write(text3)
|
| 78 |
+
st.write(text4)
|
| 79 |
+
st.write(text5)
|
| 80 |
+
|
| 81 |
+
st.write("**Example:** Website Traffic Drop Investigation")
|
| 82 |
+
st.write("**Purpose:** Understand why something happened.")
|
| 83 |
+
st.write("**Process:**")
|
| 84 |
+
st.write("Identify the drop in website traffic.")
|
| 85 |
+
st.write("Analyze various factors such as website changes, marketing campaigns, or external events.")
|
| 86 |
+
|
| 87 |
+
st.write("**Steps:**")
|
| 88 |
+
st.write("Compare traffic before and after the drop: ")
|
| 89 |
+
code=('''# Sample website traffic data
|
| 90 |
+
traffic_data = {
|
| 91 |
+
'Date': ['2023-07-01', '2023-07-02', '2023-07-03', '2023-07-04', '2023-07-05'],
|
| 92 |
+
'Traffic': [500, 480, 450, 300, 280]
|
| 93 |
+
}
|
| 94 |
+
traffic_df = pd.DataFrame(traffic_data)
|
| 95 |
+
before_drop = traffic_df[traffic_df['Date'] <= '2023-07-03']['Traffic'].mean()
|
| 96 |
+
after_drop = traffic_df[traffic_df['Date'] > '2023-07-03']['Traffic'].mean()
|
| 97 |
+
print(f"Average Traffic Before Drop: {before_drop}")
|
| 98 |
+
print(f"Average Traffic After Drop: {after_drop}") ''')
|
| 99 |
+
|
| 100 |
+
st.code(code,language='python')
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
st.write("Investigate possible reasons:")
|
| 104 |
+
st.code(''' # Hypothetical reasons
|
| 105 |
+
website_changes = "New layout introduced on 2023-07-03"
|
| 106 |
+
marketing_campaigns = "No active campaigns during the drop period"
|
| 107 |
+
external_events = "Major holiday on 2023-07-04"
|
| 108 |
+
print(f"Website Changes: {website_changes}")
|
| 109 |
+
print(f"Marketing Campaigns: {marketing_campaigns}")
|
| 110 |
+
print(f"External Events: {external_events}")''')
|
| 111 |
+
|
| 112 |
+
st.write("**Output:**")
|
| 113 |
+
st.code('''Average Traffic Before Drop: 476.6666666666667
|
| 114 |
+
Average Traffic After Drop: 290.0
|
| 115 |
+
Website Changes: New layout introduced on 2023-07-03
|
| 116 |
+
Marketing Campaigns: No active campaigns during the drop period
|
| 117 |
+
External Events: Major holiday on 2023-07-04 ''')
|
| 118 |
+
|
| 119 |
+
st.subheader(":orange[Predictive Analysis:]")
|
| 120 |
+
text6 = """ Predictive analysis attempts to answer the question :blue-background[“what is likely to happen”].
|
| 121 |
+
This type of analytics utilizes previous data to make predictions about future outcomes"""
|
| 122 |
+
text7=""" This type of analysis is another step up from the descriptive and diagnostic analyses.
|
| 123 |
+
Predictive analysis uses the data we have summarized to make logical predictions of the outcomes of events.
|
| 124 |
+
This analysis relies on statistical modeling, which requires added technology and manpower to forecast.
|
| 125 |
+
It is also important to understand that forecasting is only an estimate; the accuracy of predictions relies on quality and detailed data"""
|
| 126 |
+
st.write(text6)
|
| 127 |
+
st.write(text7)
|
| 128 |
+
|
| 129 |
+
st.write("**Example:** Forecasting Next Month's Sales")
|
| 130 |
+
st.write("**Purpose:** Predict what might happen in the future.")
|
| 131 |
+
st.write("**Process:**")
|
| 132 |
+
st.write('''Use past sales data to forecast future sales.
|
| 133 |
+
Apply a simple linear regression mode''')
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
st.write("**Steps:**")
|
| 138 |
+
st.write("Prepare data and fit a linear regression model:")
|
| 139 |
+
|
| 140 |
+
code= '''from sklearn.linear_model import LinearRegression
|
| 141 |
+
import numpy as np
|
| 142 |
+
# Use the same sales data
|
| 143 |
+
X = np.arange(len(df)).reshape(-1, 1)
|
| 144 |
+
y = df['Sales']
|
| 145 |
+
model = LinearRegression()
|
| 146 |
+
model.fit(X, y) '''
|
| 147 |
+
st.code(code,language='python')
|
| 148 |
+
|
| 149 |
+
st.write("Make predictions for the next month:")
|
| 150 |
+
st.code('''next_month = np.array([[len(df)]])
|
| 151 |
+
next_month_sales = model.predict(next_month)
|
| 152 |
+
print(f"Predicted Sales for Next Month: {next_month_sales[0]}") ''')
|
| 153 |
+
|
| 154 |
+
st.write("**Output:**")
|
| 155 |
+
st.code('''Predicted Sales for Next Month: 280.0 ''')
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
st.subheader(":violet[Prescriptive Analysis:]")
|
| 159 |
+
|
| 160 |
+
text8=""" The final type of data analysis is the most sought after, but few organizations are truly equipped to perform it.
|
| 161 |
+
Prescriptive analysis is the frontier of data analysis, combining the insight from all previous analyses to determine the course of action to
|
| 162 |
+
take in a current problem or decision. Prescriptive analysis utilizes state of the art technology and data practices."""
|
| 163 |
+
st.write(text8)
|
| 164 |
+
st.write("**Example:** Optimizing Marketing Spend")
|
| 165 |
+
st.write("**Purpose:** Provide recommendations on what actions to take.")
|
| 166 |
+
st.write("**Process:**")
|
| 167 |
+
st.write('''Determine the best allocation of marketing budget to maximize sales.
|
| 168 |
+
Use optimization techniques to find the optimal solution.''')
|
| 169 |
+
|
| 170 |
+
st.write("**Steps:**")
|
| 171 |
+
st.write("Define the objective and constraints:")
|
| 172 |
+
code=("""from scipy.optimize import linprog
|
| 173 |
+
# Objective: Maximize sales (simplified as a linear function of spend)
|
| 174 |
+
c = [-0.4, -0.3] # Coefficients for sales increase per unit spend on two channels
|
| 175 |
+
# Constraints: Total budget and minimum spend per channel
|
| 176 |
+
A = [[1, 1], [1, 0], [0, 1]]
|
| 177 |
+
b = [1000, 300, 300]
|
| 178 |
+
# Bounds for each channel
|
| 179 |
+
x_bounds = (0, None)
|
| 180 |
+
y_bounds = (0, None)
|
| 181 |
+
# Solve the linear programming problem
|
| 182 |
+
result = linprog(c, A_ub=A, b_ub=b, bounds=[x_bounds, y_bounds], method='highs') """)
|
| 183 |
+
st.code(code,language='python')
|
| 184 |
+
|
| 185 |
+
st.write("Display the recommended spend allocation:")
|
| 186 |
+
st.code("""optimal_spend = result.x
|
| 187 |
+
print(f"Optimal Spend on Channel 1: {optimal_spend[0]}")
|
| 188 |
+
print(f"Optimal Spend on Channel 2: {optimal_spend[1]}") """)
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
st.write("**Output:**")
|
| 192 |
+
st.code("""Optimal Spend on Channel 1: 700.0
|
| 193 |
+
Optimal Spend on Channel 2: 300.0 """)
|
| 194 |
+
|
| 195 |
+
|