<jupyter_start><jupyter_text>EECS 731 Climate Changes **Author: <NAME>Date: 11-23-2020 M.S in Computer Science University of Kansas**  Classification of Temperature and Conditions in Australia  Importing Libraries<jupyter_code>#importing required libraries
import pandas as pd
import numpy as np

#For visualizations
import matplotlib.pyplot as plt
import seaborn as sns

#For Cross-Validation
from sklearn.model_selection import KFold
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score

#For Classifier models
from sklearn.svm import SVC
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron<jupyter_output><empty_output><jupyter_text>Importing the data<jupyter_code>df = pd.read_csv('australia.csv')
df.head()
#dropping the columns that are not required for classification
df = df.drop(columns = ['Name', 'Date time'])
df.head()<jupyter_output><empty_output><jupyter_text>To remove the redundancy in the Conditions, I seperated the text based on the comma and created new column to predict the Conditions<jupyter_code>df["Conditions"]= df["Conditions"].str.split(",", n = 1, expand = True) 
#checking for the null values
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Note: As we have huge number of null values in Wind Chill, Heat Index, Snow Depth, Visibility and Wind Gust we are dropping these columns<jupyter_code>#removing columns with most null values
df = df.drop(columns = ['Wind Chill', 'Heat Index', 'Snow Depth', 'Wind Gust', 'Visibility'])
df.head()
#checking for the null values
df.isnull().sum()
#For classification, categorizing the temperature data into 3 categories
tg_labels = [1,2,3]
df['Australia_Temp_bins'] = pd.cut(df['Temperature'], 3, labels=tg_labels, include_lowest=True)

df_temp = df[['Maximum Temperature', 'Minimum Temperature', 'Temperature', 'Precipitation', 'Wind Speed', 'Cloud Cover', 'Relative Humidity', 'Australia_Temp_bins']]

df_temp.head()<jupyter_output><empty_output><jupyter_text>Temperature is running value, so I splitted the temperature into three Classes - low, medium and high.<jupyter_code>#low = (30.337, 51.5]
#medium = (51.5, 72.6]
#high = (72.6, 93.7]

pd.cut(df['Temperature'], 3).head()
df_temp.head()
df_temp.shape
df_temp.isnull().sum()
# Pair plot to get the distributions of temp_bins
pair_plot = sns.pairplot(df_temp, hue='Australia_Temp_bins',vars=['Precipitation', 'Wind Speed', 'Cloud Cover', 'Relative Humidity'], height=2)<jupyter_output><empty_output><jupyter_text>Pair plots are simple way to visualize relationships between each variable.Here, we could see that precipitation has nothing much to do with temperature, we can drop  that column as it doesn't show much affect  Classification of Temperature<jupyter_code>df_temp.head()
#removing columns which are not required
df_temp = df_temp.drop(columns = ['Maximum Temperature', 'Minimum Temperature', 'Temperature', 'Precipitation'])
df_temp.head()<jupyter_output><empty_output><jupyter_text>For using cross validation, we divide the data into arrays specifically X having the features and y being the temperature column.<jupyter_code># Split-out dataset
array = df_temp.values
X = array[:,0:3]
print(X)
y = array[:,3]
print(y)

# 10 fold cross validation

kf = KFold(n_splits=10, random_state=None, shuffle=False)
scaler = MinMaxScaler(feature_range=(0, 1))
X = scaler.fit_transform(X)
#1. Linear SVM

ypred_outputs_SVC = []
yrandom_outputs_SVC = []

for train_index, test_index in kf.split(X,y):
    xtrain,xtest,ytrain,ytest=X[train_index],X[test_index],y[train_index],y[test_index]
    model = SVC(kernel='linear')
    model.fit(xtrain, ytrain)
    ypred = model.predict(xtest)
    
    ypred_outputs_SVC.append(ypred)
    yrandom_outputs_SVC.append(ytest)

pSVCtotal = np.concatenate(ypred_outputs_SVC)
ySVCtotal = np.concatenate(yrandom_outputs_SVC)


acc_svc = round((accuracy_score(ySVCtotal, pSVCtotal)*100), 2)

print('Accuracy for SVC:', acc_svc)
#2. Decision Tree Classifier

ypred_outputs_DT = []
yrandom_outputs_DT = []

for train_index, test_index in kf.split(X,y):
    xtrain,xtest,ytrain,ytest=X[train_index],X[test_index],y[train_index],y[test_index]
    model = tree.DecisionTreeClassifier()
    model.fit(xtrain, ytrain)
    ypred = model.predict(xtest)
    
    ypred_outputs_DT.append(ypred)
    yrandom_outputs_DT.append(ytest)

pDTtotal = np.concatenate(ypred_outputs_DT)
yDTtotal = np.concatenate(yrandom_outputs_DT)


acc_dt = round((accuracy_score(yDTtotal, pDTtotal)*100), 2)

print('Accuracy for Decision Tree Classifier:', acc_dt)
#3. KNN Classifier

ypred_outputs_KNN = []
yrandom_outputs_KNN = []

for train_index, test_index in kf.split(X,y):
    xtrain,xtest,ytrain,ytest=X[train_index],X[test_index],y[train_index],y[test_index]
    model = KNeighborsClassifier(n_neighbors = 12)
    model.fit(xtrain, ytrain)
    ypred = model.predict(xtest)
    
    ypred_outputs_KNN.append(ypred)
    yrandom_outputs_KNN.append(ytest)

pKNNtotal = np.concatenate(ypred_outputs_KNN)
yKNNtotal = np.concatenate(yrandom_outputs_KNN)


acc_knn = round((accuracy_score(yKNNtotal, pKNNtotal)*100), 2)

print('Accuracy for KNN Classifier:', acc_knn)
#4. Random Forest Classifier

ypred_outputs_RF = []
yrandom_outputs_RF = []

for train_index, test_index in kf.split(X,y):
    xtrain,xtest,ytrain,ytest=X[train_index],X[test_index],y[train_index],y[test_index]
    model = RandomForestClassifier(n_estimators=100)
    model.fit(xtrain, ytrain)
    ypred = model.predict(xtest)
    
    ypred_outputs_RF.append(ypred)
    yrandom_outputs_RF.append(ytest)

pRFtotal = np.concatenate(ypred_outputs_RF)
yRFtotal = np.concatenate(yrandom_outputs_RF)


acc_rf = round((accuracy_score(yRFtotal, pRFtotal)*100), 2)

print('Accuracy for Random Forest Classifier:', acc_rf)
#5. Gaussian NB

ypred_outputs_NBC = []
yrandom_outputs_NBC = []

for train_index, test_index in kf.split(X,y):
    xtrain,xtest,ytrain,ytest=X[train_index],X[test_index],y[train_index],y[test_index]
    model = GaussianNB()
    model.fit(xtrain, ytrain)
    ypred = model.predict(xtest)
    
    ypred_outputs_NBC.append(ypred)
    yrandom_outputs_NBC.append(ytest)

pNBCtotal = np.concatenate(ypred_outputs_NBC)
yNBCtotal = np.concatenate(yrandom_outputs_NBC)


acc_gaussian = round((accuracy_score(yNBCtotal, pNBCtotal)*100), 2)

print('Accuracy for Gaussian NB:', acc_gaussian)
#Comparing models to get the best suitable model
models = pd.DataFrame({
    'Model': ['Linear SVC', 'Decision Tree','KNN', 'Random Forest', 'Gaussian Naive Bayes'],
    'Accuracy': [acc_svc, acc_dt, acc_knn, acc_rf, acc_gaussian]})
models.sort_values(by='Accuracy', ascending=False)
sns.catplot(data=models, y='Accuracy',x='Model',kind='bar',height=7,aspect=1.2)<jupyter_output><empty_output><jupyter_text>On comparing models, we observe that Linear SVC performed well rather than other models. There was not huge difference between  KNN and random forest too.  Classification of Conditions<jupyter_code>## Classifying conditions
df_conditions = df.copy(deep=True)
df_conditions.head()
df_conditions.shape
#removing columns which are not required
df_conditions = df_conditions.drop(columns = ['Maximum Temperature', 'Minimum Temperature', 'Australia_Temp_bins'])
df_conditions.isnull().sum()
df_conditions.describe()
# Pair plot to get the distributions of Conditions
import warnings
warnings.simplefilter('ignore')
pair_plot = sns.pairplot(df_conditions, hue='Conditions',vars=['Precipitation', 'Wind Speed','Cloud Cover','Relative Humidity', 'Temperature'], height=2)
df_conditions.head()
# Split-out dataset
array = df_conditions.values
X = array[:,0:5]
print(X)
y = array[:,5]
print(y)

kf = KFold(n_splits=10, random_state=None, shuffle=False)
scaler = MinMaxScaler(feature_range=(0, 1))
X = scaler.fit_transform(X)
#1. Linear SVM

ypred_outputs_SVC = []
yrandom_outputs_SVC = []

for train_index, test_index in kf.split(X,y):
    xtrain,xtest,ytrain,ytest=X[train_index],X[test_index],y[train_index],y[test_index]
    model = SVC(kernel='linear')
    model.fit(xtrain, ytrain)
    ypred = model.predict(xtest)
    
    ypred_outputs_SVC.append(ypred)
    yrandom_outputs_SVC.append(ytest)

pSVCtotal = np.concatenate(ypred_outputs_SVC)
ySVCtotal = np.concatenate(yrandom_outputs_SVC)


acc_svc = round((accuracy_score(ySVCtotal, pSVCtotal)*100), 2)

print('Accuracy for SVC:', acc_svc)
#2. Decision Tree Classifier

ypred_outputs_DT = []
yrandom_outputs_DT = []

for train_index, test_index in kf.split(X,y):
    xtrain,xtest,ytrain,ytest=X[train_index],X[test_index],y[train_index],y[test_index]
    model = tree.DecisionTreeClassifier()
    model.fit(xtrain, ytrain)
    ypred = model.predict(xtest)
    
    ypred_outputs_DT.append(ypred)
    yrandom_outputs_DT.append(ytest)

pDTtotal = np.concatenate(ypred_outputs_DT)
yDTtotal = np.concatenate(yrandom_outputs_DT)


acc_dt = round((accuracy_score(yDTtotal, pDTtotal)*100), 2)

print('Accuracy for Decision Tree Classifier:', acc_dt)
#3. KNN Classifier

ypred_outputs_KNN = []
yrandom_outputs_KNN = []

for train_index, test_index in kf.split(X,y):
    xtrain,xtest,ytrain,ytest=X[train_index],X[test_index],y[train_index],y[test_index]
    model = KNeighborsClassifier(n_neighbors = 5)
    model.fit(xtrain, ytrain)
    ypred = model.predict(xtest)
    
    ypred_outputs_KNN.append(ypred)
    yrandom_outputs_KNN.append(ytest)

pKNNtotal = np.concatenate(ypred_outputs_KNN)
yKNNtotal = np.concatenate(yrandom_outputs_KNN)


acc_knn = round((accuracy_score(yKNNtotal, pKNNtotal)*100), 2)

print('Accuracy for KNN Classifier:', acc_knn)
#4. Random Forest Classifier

ypred_outputs_RF = []
yrandom_outputs_RF = []

for train_index, test_index in kf.split(X,y):
    xtrain,xtest,ytrain,ytest=X[train_index],X[test_index],y[train_index],y[test_index]
    model = RandomForestClassifier(n_estimators=100)
    model.fit(xtrain, ytrain)
    ypred = model.predict(xtest)
    
    ypred_outputs_RF.append(ypred)
    yrandom_outputs_RF.append(ytest)

pRFtotal = np.concatenate(ypred_outputs_RF)
yRFtotal = np.concatenate(yrandom_outputs_RF)


acc_rf = round((accuracy_score(yRFtotal, pRFtotal)*100), 2)

print('Accuracy for Random Forest Classifier:', acc_rf)
#5. Gaussian NB

ypred_outputs_NBC = []
yrandom_outputs_NBC = []

for train_index, test_index in kf.split(X,y):
    xtrain,xtest,ytrain,ytest=X[train_index],X[test_index],y[train_index],y[test_index]
    model = GaussianNB()
    model.fit(xtrain, ytrain)
    ypred = model.predict(xtest)
    
    ypred_outputs_NBC.append(ypred)
    yrandom_outputs_NBC.append(ytest)

pNBCtotal = np.concatenate(ypred_outputs_NBC)
yNBCtotal = np.concatenate(yrandom_outputs_NBC)


acc_gaussian = round((accuracy_score(yNBCtotal, pNBCtotal)*100), 2)

print('Accuracy for Gaussian NB:', acc_gaussian)
#Comparing models to get the best suitable model
models = pd.DataFrame({
    'Model': ['Linear SVC', 'Decision Tree','KNN', 'Random Forest', 'Gaussian Naive Bayes'],
    'Accuracy': [acc_svc, acc_dt, acc_knn, acc_rf, acc_gaussian]})
models.sort_values(by='Accuracy', ascending=False)
sns.catplot(data=models, y='Accuracy',x='Model',kind='bar',height=7,aspect=1.2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11776640790402258, 'fraction_numerical': 0.010673959068454482, 'mean_word_length': 6.537898936170213, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '86043', 'license': 'MIT', 'n_tokens_mistral': 4246, 'n_tokens_neox': 4041, 'n_words': 834, 'path': 'Notebooks/Classification/Classification_Australia.ipynb', 'repo_name': 'SFLazarus/731_Final_Project', 'size': '11349'}