<jupyter_start><jupyter_text>Classification of Weather Data using scikit-learn Daily Weather Data AnalysisIn this notebook, we will use scikit-learn to perform a decision tree based classification of weather data. Importing the Necessary Libraries<jupyter_code>import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier<jupyter_output><empty_output><jupyter_text>Creating a Pandas DataFrame from a CSV file<jupyter_code>data = pd.read_csv('./weather/daily_weather.csv')<jupyter_output><empty_output><jupyter_text>Daily Weather Data DescriptionThe file **daily_weather.csv** is a comma-separated file that contains weather data.  This data comes from a weather station located in San Diego, California.  The weather station is equipped with sensors that capture weather-related measurements such as air temperature, air pressure, and relative humidity.  Data was collected for a period of three years, from September 2011 to September 2014, to ensure that sufficient data for different seasons and weather conditions is captured.Let's now check all the columns in the data.<jupyter_code>data.columns<jupyter_output><empty_output><jupyter_text>Each row in daily_weather.csv captures weather data for a separate day.  Sensor measurements from the weather station were captured at one-minute intervals.  These measurements were then processed to generate values to describe daily weather. Since this dataset was created to classify low-humidity days vs. non-low-humidity days (that is, days with normal or high humidity), the variables included are weather measurements in the morning, with one measurement, namely relatively humidity, in the afternoon.  The idea is to use the morning weather values to predict whether the day will be low-humidity or not based on the afternoon measurement of relative humidity.Each row, or sample, consists of the following variables:* **number:** unique number for each row* **air_pressure_9am:** air pressure averaged over a period from 8:55am to 9:04am (*Unit: hectopascals*)* **air_temp_9am:** air temperature averaged over a period from 8:55am to 9:04am (*Unit: degrees Fahrenheit*)* **air_wind_direction_9am:** wind direction averaged over a period from 8:55am to 9:04am (*Unit: degrees, with 0 means coming from the North, and increasing clockwise*)* **air_wind_speed_9am:** wind speed averaged over a period from 8:55am to 9:04am (*Unit: miles per hour*)* ** max_wind_direction_9am:** wind gust direction averaged over a period from 8:55am to 9:10am (*Unit: degrees, with 0 being North and increasing clockwise*)* **max_wind_speed_9am:** wind gust speed averaged over a period from 8:55am to 9:04am (*Unit: miles per hour*)* **rain_accumulation_9am:** amount of rain accumulated in the 24 hours prior to 9am (*Unit: millimeters*)* **rain_duration_9am:** amount of time rain was recorded in the 24 hours prior to 9am (*Unit: seconds*)* **relative_humidity_9am:** relative humidity averaged over a period from 8:55am to 9:04am (*Unit: percent*)* **relative_humidity_3pm:** relative humidity averaged over a period from 2:55pm to 3:04pm (*Unit: percent *)<jupyter_code>data
data[data.isnull().any(axis=1)]<jupyter_output><empty_output><jupyter_text>Data Cleaning StepsWe will not need to number for each row so we can clean it.<jupyter_code>del data['number']<jupyter_output><empty_output><jupyter_text>Now let's drop null values using the *pandas dropna* function.<jupyter_code>before_rows = data.shape[0]
print(before_rows)
data = data.dropna()
after_rows = data.shape[0]
print(after_rows)<jupyter_output><empty_output><jupyter_text>How many rows dropped due to cleaning?<jupyter_code>before_rows - after_rows<jupyter_output><empty_output><jupyter_text>Convert to a Classification Task Binarize the relative_humidity_3pm to 0 or 1.<jupyter_code>clean_data = data.copy()
clean_data['high_humidity_label'] = (clean_data['relative_humidity_3pm'] > 24.99)*1
print(clean_data['high_humidity_label'])<jupyter_output><empty_output><jupyter_text>Target is stored in 'y'.<jupyter_code>y=clean_data[['high_humidity_label']].copy()
#y
clean_data['relative_humidity_3pm'].head()
y.head()<jupyter_output><empty_output><jupyter_text>Use 9am Sensor Signals as Features to Predict Humidity at 3pm<jupyter_code>morning_features = ['air_pressure_9am','air_temp_9am','avg_wind_direction_9am','avg_wind_speed_9am',
        'max_wind_direction_9am','max_wind_speed_9am','rain_accumulation_9am',
        'rain_duration_9am']
X = clean_data[morning_features].copy()
X.columns
y.columns<jupyter_output><empty_output><jupyter_text>Perform Test and Train split  REMINDER: Training PhaseIn the **training phase**, the learning algorithm uses the training data to adjust the model’s parameters to minimize errors.  At the end of the training phase, you get the trained model.In the **testing phase**, the trained model is applied to test data.  Test data is separate from the training data, and is previously unseen by the model.  The model is then evaluated on how it performs on the test data.  The goal in building a classifier model is to have the model perform well on training as well as test data.<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=324)
#type(X_train)
#type(X_test)
#type(y_train)
#type(y_test)
#X_train.head()
#y_train.describe()<jupyter_output><empty_output><jupyter_text>Fit on Train Set<jupyter_code>humidity_classifier = DecisionTreeClassifier(max_leaf_nodes=10, random_state=0)
humidity_classifier.fit(X_train, y_train)
type(humidity_classifier)<jupyter_output><empty_output><jupyter_text>Predict on Test Set<jupyter_code>predictions = humidity_classifier.predict(X_test)
predictions[:10]
y_test['high_humidity_label'][:10]<jupyter_output><empty_output><jupyter_text>Measure Accuracy of the Classifier<jupyter_code>accuracy_score(y_true = y_test, y_pred = predictions)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08494659546061416, 'fraction_numerical': 0.018357810413885182, 'mean_word_length': 7.323611111111111, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '193089', 'license': 'MIT', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1772, 'n_words': 616, 'path': 'Final Notebook/Weather Data Classification using Decision Trees.ipynb', 'repo_name': 'musaibnazir/DataScience', 'size': '5992'}