-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vol2: Python, more work on genetic algo
- Loading branch information
1 parent
ad41001
commit 1197e96
Showing
9 changed files
with
1,071 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
Artificial Intelligence for Humans | ||
Volume 2: Nature-Inspired Algorithms | ||
Python Version | ||
http://www.aifh.org | ||
http://www.jeffheaton.com | ||
Code repository: | ||
https://github.com/jeffheaton/aifh | ||
Copyright 2014 by Jeff Heaton | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
For more information on Heaton Research copyrights, licenses | ||
and trademarks visit: | ||
http://www.heatonresearch.com/copyright | ||
""" | ||
__author__ = 'jheaton' | ||
|
||
import math | ||
import numpy as np | ||
from scipy.spatial import distance | ||
|
||
|
||
class Equilateral(object): | ||
def __init__(self, class_count, normalized_low, normalized_high): | ||
""" Create a lookup table that will be used for both Equilateral encoding and decoding. | ||
""" | ||
# Allocate a matrix to hold the lookup table. | ||
|
||
self.encoded = np.ndarray(shape=(class_count, class_count - 1), dtype=float) | ||
|
||
# Seed the result. | ||
self.encoded[0][0] = -1 | ||
self.encoded[1][0] = 1.0 | ||
|
||
for k in range(2, class_count): | ||
# scale the matrix so far | ||
r = k | ||
f = math.sqrt(r * r - 1.0) / r | ||
for i in range(0, k): | ||
for j in range(0, k - 1): | ||
self.encoded[i][j] *= f | ||
|
||
r = -1.0 / r | ||
for i in range(0, k): | ||
self.encoded[i][k - 1] = r | ||
|
||
for i in range(0, k - 1): | ||
self.encoded[k][i] = 0.0 | ||
|
||
self.encoded[k][k - 1] = 1.0 | ||
|
||
# Scale it. | ||
min_eq = -1 | ||
max_eq = 1 | ||
for row in range(0, len(self.encoded)): | ||
for col in range(0, len(self.encoded[row])): | ||
self.encoded[row][col] = ((self.encoded[row][col] - min_eq) / (max_eq - min_eq)) \ | ||
* (normalized_high - normalized_low) + normalized_low | ||
|
||
def encode(self, class_num): | ||
""" Provide the equilateral encoded vector for the specified class. | ||
""" | ||
return self.encoded[class_num] | ||
|
||
def decode(self, vec): | ||
""" Match the specified vector to the class that it most closely fits. | ||
""" | ||
min_dist = float('inf') | ||
result = -1 | ||
|
||
for i in range(0, len(self.encoded)): | ||
dist = distance.euclidean(vec, self.encoded[i]) | ||
if dist < min_dist: | ||
result = i | ||
min_dist = dist | ||
|
||
return result |
389 changes: 389 additions & 0 deletions
389
vol2/vol2-python-examples/examples/example_genetic_iris.py
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Artificial Intelligence for Humans | ||
Volume 2: Nature-Inspired Algorithms | ||
Python Version | ||
http://www.aifh.org | ||
http://www.jeffheaton.com | ||
Code repository: | ||
https://github.com/jeffheaton/aifh | ||
Copyright 2013 by Jeff Heaton | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
For more information on Heaton Research copyrights, licenses | ||
and trademarks visit: | ||
http://www.heatonresearch.com/copyright | ||
""" | ||
__author__ = 'jheaton' | ||
|
||
|
||
class AIFHError(Exception): | ||
"""An error was raised. This is used for several purposes, see individual error messages.""" | ||
|
||
def __init__(self, value): | ||
self.value = value | ||
|
||
def __str__(self): | ||
return repr(self.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
Artificial Intelligence for Humans | ||
Volume 4: Nature-Inspired Algorithms | ||
Python Version | ||
http://www.aifh.org | ||
http://www.jeffheaton.com | ||
Code repository: | ||
https://github.com/jeffheaton/aifh | ||
Copyright 2014 by Jeff Heaton | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
For more information on Heaton Research copyrights, licenses | ||
and trademarks visit: | ||
http://www.heatonresearch.com/copyright | ||
""" | ||
__author__ = 'jheaton' | ||
|
||
import numpy as np | ||
|
||
|
||
class ErrorCalculation: | ||
def __init__(self): | ||
self.global_error = 0 | ||
self.count = 0 | ||
|
||
@staticmethod | ||
def rms(actual, ideal): | ||
return np.sqrt(np.mean((actual[:, :] - ideal[:, :]) ** 2)) | ||
|
||
@staticmethod | ||
def sse(actual, ideal): | ||
return np.sum((actual[:, :] - ideal[:, :]) ** 2) | ||
|
||
@staticmethod | ||
def mse(actual, ideal): | ||
return np.mean((actual[:, :] - ideal[:, :]) ** 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.