from pandas import read_csv
filename = 'data/sonar-data.csv'
names = list('var'+str(x) for x in range(0,60))
names.append('output')
sonar_data = read_csv(filename, header=None, names=names)
print(sonar_data.shape)
display(sonar_data.head())

"""
Explanation: Predict Objects from Sonar Data
About the Sonar Data Set
This is a standard data set available from the UCI Machine Learning Repository.
Load the Data
End of explanation
"""


sonar_data.groupby('output').size()

"""
Explanation: Explore and Data Prep
There's probably not much we need to explore in the sonar data set, since the description of the data set (above) gives us a pretty good understanding. We are definitely interested in the distribution of classes (rock vs. mine).
End of explanation
"""


values_array = sonar_data.values
x = values_array[:,0:60].astype(float)
labels = values_array[:,60]

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(labels)
print(list(le.classes_))
y = le.transform(labels)
print(y[0:10])

"""
Explanation: Let's split the data into inputs and output.  The inputs are numeric and already standardized to have min=0.0 and max=1.0. But we do need to map the output (class attribute) from character to numeric.
End of explanation
"""


zeroRuleAccuracy = len(sonar_data.loc[sonar_data.output=='M'])/len(sonar_data)
zeroRuleAccuracy

"""
Explanation: Predict Mine or Rock
Zero Rule Algorithm
The Zero Rule algorithm gives us a baseline against which we can compare our machine learning algorithms.  We ignore all input data and simply predict the majority class which is M as we saw earlier.
End of explanation
"""


from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler

# scale the input
scaler = StandardScaler().fit(x)
rescaledX = scaler.transform(x)

# convenience function to evaluate different models
def evaluate_model(model, x, y):
    kfold = KFold(n_splits=10, random_state=7)
    results = cross_val_score(model, x, y, cv=kfold, scoring='accuracy')
    print("Accuracy: %.4f (%.4f)" % (results.mean(), results.std()))

# try logistic regression
from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
evaluate_model(model, rescaledX, y)

# try a perceptron
from sklearn.linear_model import Perceptron

model = Perceptron(penalty='l2', alpha=0.001, max_iter=500)
evaluate_model(model, rescaledX, y)

"""
Explanation: Classification Using Machine Learning
End of explanation
"""


