Skip to content

Commit

Permalink
added neural, code from a nice machine learning tutorial by Jason Bro…
Browse files Browse the repository at this point in the history
…wnlee
  • Loading branch information
jimmygizmo committed Aug 9, 2018
1 parent 8b0980b commit b8a538a
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
48 changes: 48 additions & 0 deletions neural/PythonMachineLearningExampleNotes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
This code comes from the tutorial by Jason Brownlee:
Your First Machine Learning Project in Python Step-By-Step
https://machinelearningmastery.com/machine-learning-in-python-step-by-step/


I assempled the code in IntelliJ and built a virtualenv inside the project directory
and installed the following modules inside the venv:
scipy, numpy, matplotlib, pandas and sklearn.

I did not need to nor did I include access to all global Python modules in the
IntelliJ project structure options.

When using IntelliJ IDEA Ultimate, you enjoy the great benefit of the SciView,
which works with Brownlee's code, matplotlib and numpy to render wonderful
graphical plots right in IntelliJ. I was amazed when I first saw this right in the IDE.

Check out the above URL. It's a great tutorial and the code works great, especially
in IntelliJ IDEA Ultimate.

On a side note, I started the project and the venv on iCloud Drive, as I use multiple
Macs against a common cloud-synced dev area, usually before I decide something is ready
to go into git. So, when I first tried opening the project (and hence the venv) on
another Mac which is running an identical set of tools like IntelliJ, I ran into a small issue.

The venv has never been activate on that Mac. Initially, IntelliJ gave me a big error about an
invalid Python interpreter. I could not solve the problem by adjusting things in the Project
Structure dialog, by trying to configure the -existing- venv. Still throwing invalid interpreter
error and none of the module imports were good.

SOLUTION: I quit IntelliJ and I opened up a regular terminal. I activate the virtualen in that terminal
thereby setting the environment variables etc. THEN: I LAUNCHED INTELLIJ FROM THIS TERMINAL:
] open /path/to/intellij/inside/the/mac/application/folder

It worked.

So, I don't know at this point if once the venv is deactivate this problem will return.
I am looking for a better solution, like how can I get IntelliJ to just automatically
activate a venv inside a project.

Not that one wants to store all that python venv stuff with the project, especially in a repository,
but it would be nice to be able to download the code and maybe also intellij project settings
and then just open the project on perhaps one of many different workstations and have
IntelliJ automatically activate the venv and NOT throw this invalid interpretor error, which
is especially troubling at first since it is not apparent how to resolve it from within IntelliJ
itself, even though it seems like a straightforword Project Structure issue.

Jimmy Gizmo

98 changes: 98 additions & 0 deletions neural/irisnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#This code comes from the tutorial by Jason Brownlee:
#Your First Machine Learning Project in Python Step-By-Step
#https://machinelearningmastery.com/machine-learning-in-python-step-by-step/

# Load libraries
import pandas
from pandas.plotting import scatter_matrix
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
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

# Load dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = pandas.read_csv(url, names=names)

# shape
print(dataset.shape)

# head
print(dataset.head(20))

# descriptions
print(dataset.describe())

# class distribution
print(dataset.groupby('class').size())

# box and whisker plots
dataset.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)
plt.show()

# histograms
dataset.hist()
plt.show()

# scatter plot matrix
scatter_matrix(dataset)
plt.show()

# Split-out validation dataset
array = dataset.values
X = array[:,0:4]
Y = array[:,4]
validation_size = 0.20
seed = 7
X_train, X_validation, Y_train, Y_validation = \
model_selection.train_test_split(X, Y, test_size=validation_size, random_state=seed)

# Test options and evaluation metric
seed = 7
scoring = 'accuracy'

# Spot Check Algorithms
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))
# evaluate each model in turn
results = []
names = []
for name, model in models:
kfold = model_selection.KFold(n_splits=10, random_state=seed)
cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)

# Compare Algorithms
fig = plt.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()

# Make predictions on validation dataset
knn = KNeighborsClassifier()
knn.fit(X_train, Y_train)
predictions = knn.predict(X_validation)
print(accuracy_score(Y_validation, predictions))
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))



0 comments on commit b8a538a

Please sign in to comment.