# Load libraries
import numpy
from matplotlib import pyplot
from pandas import read_csv
from pandas import set_option
from pandas.tools.plotting import scatter_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier

# Load dataset
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data'
dataset = read_csv(url, header=None)

# Summarize Data

# Descriptive statistics
# shape
# conﬁrming the dimensions of the dataset (number of rows and columns)
print(dataset.shape)

# types
print(dataset.dtypes)

"""
Explanation: The Problem
The dataset: https://archive.ics.uci.edu/ml/datasets/Connectionist+Bench+(Sonar,+Mines+vs.+Rocks)
Problem:  to predict metal or rock objects from sonar return data. Each pattern is a set of 60 numbers in the range 0.0 to 1.0. Each number represents the energy within a particular frequency band, integrated over a certain period of time. The label associated with each record contains the letter R if the object is a rock and M if it is a mine (metal cylinder). The numbers in the labels are in increasing order of aspect angle, but they do not encode the angle directly.
End of explanation
"""


# head
# Let’s now take a peek at the ﬁrst 20 rows of the data
print(dataset.head(20))

"""
Explanation: We can see that all of the attributes are numeric (ﬂoat) and that the class value has been read in as an object.
End of explanation
"""


# descriptions, change precision to 3 places
# Let’s summarize the distribution of each attribute.
# Print the statistical descriptions of the dataset
set_option('precision', 3)
print(dataset.describe())

"""
Explanation: This does not show all of the columns, but we can see all of the data has the same scale. 
We can also see that the class attribute (60) has string values.
End of explanation
"""


# class distribution
# Let’s take a quick look at the breakdown of class values.
print(dataset.groupby(60).size())

"""
Explanation: Again, as we expect, the data has the same range, but interestingly diﬀering mean values. There may be some beneﬁt from standardizing the data.
End of explanation
"""


dataset.hist(sharex=False, sharey=False, xlabelsize=1, ylabelsize=1)
pyplot.show()

"""
Explanation: We can see that the classes are reasonably balanced between M (mines) and R (rocks).
Data Visualizations
Let’s look at visualizations of individual attributes. Let’s look at histograms of each attribute to get a sense of the data distributions.
Unimodal Data Visualizations
End of explanation
"""


# density
dataset.plot(kind='density', subplots=True, layout=(8,8), sharex=False, legend=False)
pyplot.show()

"""
Explanation: We can see that there are a lot of Gaussian-like distributions and perhaps some exponentiallike distributions for other attributes.
Let’s take a look at the same perspective of the data using density plots.
End of explanation
"""


# correlation matrix
fig = pyplot.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(dataset.corr(), vmin=-1, vmax=1, interpolation='none')
fig.colorbar(cax)
pyplot.show()

"""
Explanation: This is useful, you can see that many of the attributes have a skewed distribution. A power transform like a Box-Cox transform that can correct for the skew in distributions might be useful.
Multimodal Data Visualizations
Let’s visualize the correlations between the attributes
End of explanation
"""


