Cross-testing is not implemented for neat python, so please tell me the implementation code.Previous
I was advised that it is possible with Scikit-learn, but I don't think NEAT was implemented in Scikit-learn, so I would appreciate it if you could tell me about the implementation using Scikit-learn.
You may not have seen it anymore, but xor2
on the neat-python
official page seems to be implemented as follows.
from NEAT import nn, population
import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.cross_validation import cross_val_score
classneatXorClassifier (BaseEstimator, ClassifierMixin):
def__init__(self):
self.pop=population.Population('xor2_config')
@staticmethod
default_fitness(genomes):
Forging genomes:
net=nn.create_feed_forward_phenotype(g)
sum_square_error = 0.0
for inputs, expected in zip (xor_inputs, xor_outputs):
# Serial activation propagates the inputs through the entire network.
output = net.serial_activate(inputs)
sum_square_error+=(output[0]-expected)**2
# When the output matches expected for all inputs, fitness will reach
# its maximum value of 1.0.
g.fitness=1-sum_square_error
default(self,X,y):
self.pop.run(self.eval_fitness,300)
def predict (self, X):
winner=self.pop.statistics.best_genome()
winner_net=nn.create_feed_forward_phenotype(winner)
output = [ ]
for inputs in X:
output.append(round(winner_net.serial_activate(inputs)[0]))
return output
# def score(self,X,y):
# iflen(X) == 0:
# return1
# pred=self.predict(X)
# return len(list(filter(lambdax:x[0]==x[1], zip(pred,y)))))/len(pred)
if__name__=='__main__':
xor_inputs=[0,0],[0,1],[1,0],[1,1]]
xor_outputs=[0,1,1,0]
nxc=neatXorClassifier()
print(cross_val_score(nxc,xor_inputs,y=xor_outputs,cv=2))
Is your question a cross-test implementation in general or a Neat specific issue?
If you're just going to cross-test K division with scikit-learn,
from sklearn import cross_validation
import numpy
X = numpy.array([1,2], [0,4], [4,5], [2,3])# feature
y = numpy.array([0.9, -0.1, 1.2, 15.3])# answer
kf=cross_validation.KFold(len(X), n_folds=2)#2-validation
for train_index, test_index inkf:
X_train, X_test=X[train_index], X[test_index]
y_train, y_test=y[train_index], y[test_index]
Now you should be able to do it. It will be divided into learning data (x_train, y_train) and test data (x_test, y_test) in the loop. You can learn with learning data and measure your score with test data.
I have a personal question, but what score do you rate on Neat? Because I don't know how to predict from test data after looking at the Neat Python page a little bit.
548 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
725 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
547 Who developed the "avformat-59.dll" that comes with FFmpeg?
539 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.