Welcome to Day 46 of our 365-day journey to master data science and artificial intelligence, launched on February 26, 2025. Yesterday, in Day 45, we scaled Priya’s models using Dask on a simulated 33-row dataset across three cafés, achieving a mean absolute error of 3.1 with a stacked ensemble. The model predicted 644 rupees for Café 1’s Thursday 9 AM sales, guiding 32 samosas per location, while maintaining 1.0 Slow recall. Today, on May 15, 2025, we secure: What is data security, and can Priya protect her café’s data to ensure privacy?
Safeguarding the Café
Data security involves protecting datasets—like Priya’s sales, customer counts, and sentiment—from unauthorized access, breaches, or misuse. Her 33-row dataset drives 644-rupee predictions, but storing data for three cafés risks leaks—staff accessing Customer_Count or hackers targeting Sales_Lag. Security is part of the collect and deploy phases in our workflow, ensuring her Flask API and Dask pipelines remain trusted while scaling to 1000 rows.
Picture Priya locking her café’s safe. Her models stock 32 samosas, but exposed data could reveal customer patterns or sales trends to competitors. Data security keeps her 644-rupee forecasts private. This is the focus of Day 46.
Why Data Security Matters
Priya’s models—regression with 3.1 mean absolute error, classifier with 1.0 Slow recall, and ARIMA with 2.5 mean absolute error—are robust, but:
- Privacy: Customer_Count (20 at 9 AM)—sensitive if linked to individuals?
- Integrity: Sales (644 rupees)—tampering skews stock?
- Scale: 1000 rows across cafés—more data, higher risk.
Security protects her 632.5-rupee forecast, big data pipelines, and tuned models, ensuring trust. Day 46 secures this.
Priya’s Data Recap
Her scaled data from Day 45 (sample from Café 1):
Datetime,Sales,Hour_Num,Item_Code,Weather_Rainy,Rush_Hour,Weekday,Sales_Lag,Label,Sentiment,Customer_Count,RL_Stock,Cluster
2025-03-03 08:00,500,8,0,0,1,1,200,Busy,0,15,39,0
2025-03-03 09:00,600,9,1,0,1,1,500,Busy,0.6588,20,32,1
2025-03-03 10:00,500,10,1,0,0,1,600,Busy,0.4404,12,39,0
2025-03-03 11:00,400,11,1,0,0,1,500,Slow,0,8,39,2
2025-03-04 08:00,550,8,0,1,1,1,150,Busy,0.5719,16,39,0
2025-03-04 09:00,650,9,1,1,1,1,550,Busy,0.5859,22,33,1
2025-03-04 10:00,550,10,1,1,0,1,650,Busy,0,13,39,0
2025-03-04 11:00,450,11,1,1,0,1,550,Slow,0,9,39,2
2025-03-05 09:00,640,9,1,0,1,0,650,Busy,0.6369,21,32,1
2025-03-05 10:00,540,10,1,0,0,0,640,Busy,0,14,39,0
2025-03-05 11:00,440,11,1,0,0,0,540,Slow,0,10,39,2
- Models: Stacked ensemble, mean absolute error 3.1, 644 rupees for 9 AM.
- Issue: Unsecured data—risks breaches across cafés.
Goal: Secure data—protect 644-rupee predictions, 32 samosas. Day 46 begins here.
Data Security Basics
Techniques for Priya’s data:
- Encryption:
- Scramble data—Sales readable only with a key.
- Access Control:
- Limit staff access—Customer_Count for managers only.
- Anonymization:
- Mask sensitive fields—Sentiment without customer IDs.
With 33 rows, encryption and access control suit her Flask API, scalable to 1000 rows. Day 46 applies this.
Encrypting Data
Encrypt Sales and Customer_Count:
import pandas as pd
from cryptography.fernet import Fernet
data_clean = pd.DataFrame({
"Datetime": ["2025-03-03 08:00", "2025-03-03 09:00", "2025-03-03 10:00", "2025-03-03 11:00",
"2025-03-04 08:00", "2025-03-04 09:00", "2025-03-04 10:00", "2025-03-04 11:00",
"2025-03-05 09:00", "2025-03-05 10:00", "2025-03-05 11:00"],
"Sales": [500, 600, 500, 400, 550, 650, 550, 450, 640, 540, 440],
"Hour_Num": [8, 9, 10, 11, 8, 9, 10, 11, 9, 10, 11],
"Item_Code": [0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1],
"Weather_Rainy": [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
"Rush_Hour": [1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0],
"Weekday": [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
"Sales_Lag": [200, 500, 600, 500, 150, 550, 650, 550, 650, 640, 540],
"Sentiment": [0, 0.6588, 0.4404, 0, 0.5719, 0.5859, 0, 0, 0.6369, 0, 0],
"Customer_Count": [15, 20, 12, 8, 16, 22, 13, 9, 21, 14, 10],
"RL_Stock": [39, 32, 39, 39, 39, 33, 39, 39, 32, 39, 39],
"Cluster": [0, 1, 0, 2, 0, 1, 0, 2, 1, 0, 2]
})
data_clean["Datetime"] = pd.to_datetime(data_clean["Datetime"])
key = Fernet.generate_key()
cipher = Fernet(key)
data_clean["Sales_Encrypted"] = data_clean["Sales"].apply(lambda x: cipher.encrypt(str(x).encode())).astype(str)
data_clean["Customer_Count_Encrypted"] = data_clean["Customer_Count"].apply(lambda x: cipher.encrypt(str(x).encode())).astype(str)
print(data_clean[["Sales", "Sales_Encrypted", "Customer_Count", "Customer_Count_Encrypted"]].head())
Output (hypothetical):
Sales,Sales_Encrypted,Customer_Count,Customer_Count_Encrypted
500,gAAAAABk...15,gAAAAABk...
600,gAAAAABk...20,gAAAAABk...
500,gAAAAABk...12,gAAAAABk...
400,gAAAAABk...8,gAAAAABk...
550,gAAAAABk...16,gAAAAABk...
Sales, Customer_Count encrypted—secure storage. Day 46 protects this.
Decrypting for Models
Decrypt for training:
data_clean["Sales"] = data_clean["Sales_Encrypted"].apply(lambda x: float(cipher.decrypt(x.encode())))
data_clean["Customer_Count"] = data_clean["Customer_Count_Encrypted"].apply(lambda x: float(cipher.decrypt(x.encode())))
print(data_clean[["Sales", "Customer_Count"]].head())
Output:
Sales,Customer_Count
500,15
600,20
500,12
400,8
550,16
Data restored—models unaffected. Day 46 ensures this.
Securing Flask API
Add authentication:
from flask import Flask, request, jsonify
import pickle
from functools import wraps
import jwt
app = Flask(__name__)
SECRET_KEY = "priya-cafe-2025"
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = request.headers.get("Authorization")
if not token:
return jsonify({"error": "Token missing"}), 401
try:
jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
except:
return jsonify({"error": "Invalid token"}), 401
return f(*args, **kwargs)
return decorated
with open("stack_reg.pkl", "rb") as f:
model = pickle.load(f)
@app.route("/predict", methods=["POST"])
@token_required
def predict():
data = request.get_json()
df = pd.DataFrame([data], columns=["Hour_Num", "Item_Code", "Weather_Rainy", "Rush_Hour", "Weekday", "Sales_Lag", "Sentiment", "Customer_Count", "RL_Stock", "Cluster_1", "Cluster_2"])
pred = model.predict(df)[0]
stock = 32 if pred >= 500 else 15
return jsonify({"sales": float(pred), "stock": stock, "label": "Busy" if pred >= 500 else "Slow"})
if __name__ == "__main__":
app.run(debug=True)
API requires JWT—only authorized users predict 644 rupees. Day 46 secures this.
Testing Secured API
Test with token:
import requests
data = {
"Hour_Num": 9,
"Item_Code": 1,
"Weather_Rainy": 0,
"Rush_Hour": 1,
"Weekday": 1,
"Sales_Lag": 640,
"Sentiment": 0.6,
"Customer_Count": 20,
"RL_Stock": 32,
"Cluster_1": 1,
"Cluster_2": 0
}
token = jwt.encode({"user": "priya"}, "priya-cafe-2025", algorithm="HS256")
headers = {"Authorization": token}
response = requests.post("http://127.0.0.1:5000/predict", json=data, headers=headers)
print(response.json())
Output:
{"sales": 644.0, "stock": 32, "label": "Busy"}
Secure prediction—32 samosas. Day 46 tests this.
Access Control
Limit data access:
- Managers: Sales, Customer_Count—predict 644 rupees.
- Staff: RL_Stock, Cluster—stock 32 samosas.
Implement in Dask:
ddf = dd.from_pandas(data_big, npartitions=3)
manager_ddf = ddf[["Sales", "Customer_Count", "Hour_Num", "Sales_Lag", "Sentiment"]]
staff_ddf = ddf[["RL_Stock", "Cluster", "Hour_Num"]]
print(manager_ddf.head())
Output:
Sales,Customer_Count,Hour_Num,Sales_Lag,Sentiment
500,15,8,200,0
600,20,9,500,0.6588
500,12,10,600,0.4404
400,8,11,500,0
550,16,8,150,0.5719
Managers see sensitive data; staff see operational. Day 46 restricts this.
Thursday 9 AM Secure Prediction
On May 15, 2025:
- Inputs: Sales_Lag=640, Customer_Count=20, Sentiment=0.6, Cluster_1=1.
- Output: 644 rupees, Busy, 32 samosas—secure via JWT.
Café 2: ~708 rupees, Café 3: ~580 rupees. Day 46 predicts this.
Why Data Security?
- Privacy: Encrypted Sales—safe from leaks.
- Trust: JWT API—only Priya predicts 644 rupees.
- Scale: 1000 rows—secure multi-café data.
Complements 644-rupee forecast, big data—trusted café. Day 46 protects this.
Real-World Security
Banks secure transactions—fraud prevented. Retail protects sales data—trust maintained. Priya’s security is her café’s shield—small, vital. Day 46 mirrors this.
Challenges
- Small Data: 33 rows—overhead for encryption?
- Cost: JWT, cloud security—affordable for cafés?
- Compliance: India’s data laws—more rules by May 2025?
More data—Priya complies. Day 46 notes this.
Why This Matters
Securing 644 rupees—32 samosas, private—safeguards Priya’s café. Without it, data leaks; with it, she’s trusted—profit up. Scaled, security protects health records—lives safe. Day 46 guards her.
Recap Summary
Yesterday, Day 45 scaled—mean absolute error 3.1, 644 rupees. Today, Day 46 secured—644 rupees, 32 samosas, encrypted. It’s her protect step.
What’s Next
Tomorrow, in Day 47, we’ll visualize: Can Priya plot sales trends? Spot patterns? We’ll explore data visualization, illuminating her café. Join us with curiosity!

























