In [2]:
import pandas as pd # to read csv file and xlsx file
import numpy as np # to perform numerical operations
from sklearn.model_selection import train_test_split #
from sklearn.tree import DecisionTreeClassifier, export_text
from sklearn.metrics import classification_report
In [3]:
# 1. Generate synthetic data
np.random.seed(42)  # Ensure reproducibility

# Generate Temperature (random float values between 15 and 40)
temperature = np.random.uniform(15, 40, 100)

# Generate Humidity (random float values between 30 and 90)
humidity = np.random.uniform(30, 90, 100)

# Generate Target based on simple rules
target = ['Yes' if t > 25 and h > 50 else 'No' for t, h in zip(temperature, humidity)]

# Create DataFrame
data = pd.DataFrame({
    'Temperature': temperature,
    'Humidity': humidity,
    'Target': target
})

#
In [4]:
print("Sample Dataset:")
print(data.head(50))
Sample Dataset:
    Temperature   Humidity Target
0     24.363503  31.885751     No
1     38.767858  68.184625    Yes
2     33.299849  48.861359     No
3     29.966462  60.514241    Yes
4     18.900466  84.453988     No
5     18.899863  44.957534     No
6     16.452090  54.622975     No
7     36.654404  75.333068    Yes
8     30.027875  43.727890     No
9     32.701814  34.618795     No
10    15.514612  47.385087     No
11    39.247746  39.673277     No
12    35.811066  85.781859    Yes
13    20.308478  78.487223     No
14    19.545624  68.004225     No
15    19.585113  82.287635     No
16    22.606056  78.220325     No
17    28.118911  41.194204     No
18    25.798625  83.553540    Yes
19    22.280729  62.360535     No
20    30.296322  78.446409    Yes
21    18.487347  83.765478     No
22    22.303616  49.080208     No
23    24.159046  36.603115     No
24    26.401750  43.676110     No
25    34.629399  55.626467    Yes
26    19.991845  79.080886     No
27    27.855861  81.643835    Yes
28    29.810364  30.417128     No
29    16.161260  60.644838     No
30    30.188621  55.044660    Yes
31    19.263103  43.326469     No
32    16.626290  37.191922     No
33    38.722138  50.256910    Yes
34    39.140801  86.574582    Yes
35    35.209934  49.392176     No
36    22.615344  61.127437     No
37    17.441803  72.181138     No
38    32.105826  51.817776    Yes
39    26.003812  88.306925    Yes
40    18.050956  87.746838     No
41    27.379423  45.106938     No
42    15.859713  59.834910     No
43    37.733010  48.052699     No
44    21.469500  47.090430     No
45    31.563057  32.213217     No
46    22.792777  66.573860     No
47    28.001701  60.160741    Yes
48    28.667757  33.088725     No
49    19.621361  46.718788     No
In [ ]:
# 2. Train a Decision Tree Classifier
# Prepare data for training
X = data[['Temperature', 'Humidity']]
y = data['Target']

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize Decision Tree Classifier
clf = DecisionTreeClassifier(random_state=42)

# Fit the model
clf.fit(X_train, y_train) # save this model

# Predict on the test set
y_pred = clf.predict(X_test)
In [7]:
# Evaluate the model
print("\nClassification Report:")
print(classification_report(y_test, y_pred))

# Visualize the decision tree rules
tree_rules = export_text(clf, feature_names=['Temperature', 'Humidity'])
print("\nDecision Tree Rules:")
print(tree_rules)
Classification Report:
              precision    recall  f1-score   support

          No       1.00      1.00      1.00        11
         Yes       1.00      1.00      1.00         9

    accuracy                           1.00        20
   macro avg       1.00      1.00      1.00        20
weighted avg       1.00      1.00      1.00        20


Decision Tree Rules:
|--- Temperature <= 25.20
|   |--- class: No
|--- Temperature >  25.20
|   |--- Humidity <= 49.87
|   |   |--- class: No
|   |--- Humidity >  49.87
|   |   |--- class: Yes

In [8]:
X_test
Out[8]:
Temperature Humidity
83 16.588959 82.640361
53 37.370684 59.367166
70 34.306119 70.653862
45 31.563057 32.213217
44 21.469500 47.090430
39 26.003812 88.306925
22 22.303616 49.080208
80 36.577586 50.463981
10 15.514612 47.385087
0 24.363503 31.885751
18 25.798625 83.553540
30 30.188621 55.044660
73 35.386536 43.589747
33 38.722138 50.256910
90 17.989856 35.586166
4 18.900466 84.453988
76 34.281759 71.456264
77 16.851116 53.204121
12 35.811066 85.781859
31 19.263103 43.326469
In [19]:
sensor_data = pd.DataFrame({'Temperature': [100.588959], 'Humidity': [30]})  # Real time sensor data
clf.predict(sensor_data)[0]
Out[19]:
'No'
In [20]:
if clf.predict(sensor_data)[0]=="Yes":
    print("Alert")
else:
    print("No Alert")
No Alert
In [1]:
pip install pyserial
Defaulting to user installation because normal site-packages is not writeable
Collecting pyserial
  Downloading pyserial-3.5-py2.py3-none-any.whl.metadata (1.6 kB)
Downloading pyserial-3.5-py2.py3-none-any.whl (90 kB)
Installing collected packages: pyserial
Successfully installed pyserial-3.5
Note: you may need to restart the kernel to use updated packages.
In [ ]:
import serial

# Initialize Serial connection
arduino = serial.Serial(port='/dev/cu.usbserial-110', baudrate=9600, timeout=1)  # Replace 'COM3' with your port

while True:
    data = arduino.readline().decode('utf-8').strip()
    if data:
        try:
            temperature, humidity = map(float, data.split(","))
            print(f"Temperature: {temperature}°C, Humidity: {humidity}%")
        except ValueError:
            print("Invalid data received:", data)
Invalid data received: DHTxx test!
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
Invalid data received: Humidity: 49.00%  Temperature: 20.70°C 69.26°F  Heat index: 20.10°C 68.19°F
In [ ]: