Ejemplo n.º 1
0
def train(FIS_name,
          data,
          target_col,
          mf,
          Ncentroids,
          overlap,
          alpha=0.5,
          iterations=50,
          sa=False,
          sa_plot=False):
    '''
	Trains a FIS, writes all the properties of this FIS to a FIS file
	using the write function.

	Inputs: 
		data: nummpy array of size > number of centroids x 2
		target_col: integer index of the target column
		Ncentroids: either an integer (for each feature te same)
	          or an array size = number of features
		mf:  'triangle', 'trapezoid' or 'Gaussian'
		overlap: number between 0 and 1, 
					when gaussian mf overlap is the variance
					when triangle/trapezoid overlap is half of the base 
		iterations: number of iterations for the simulated annealing

	Outputs:
		RB: list of lists of integer rules
		target_centroids: list with scaled target centroids
		feature_centroids: the other feature centroids
	'''
    # scale the data
    data, min_x, max_x = scale(data)
    # get centroids
    centroids = cluster(data, target_col, Ncentroids, plot=False)
    # learn WM rules
    RB = WM.learn(data, centroids, overlap, mf, target_col)
    # return everything needed for testing
    target_centroids = centroids[target_col]
    # delete target centroid for testing
    feature_centroids = np.delete(centroids, target_col, 0)
    # delete target values for testing
    targets = data[:, target_col]
    data = np.delete(data, target_col, 1)
    method = 'WM'
    # for simulated annealing, get the new rule base
    if sa:
        method = 'WM+SA'
        RB = SA.search(data,
                       targets,
                       RB,
                       alpha,
                       feature_centroids,
                       overlap,
                       mf,
                       target_centroids,
                       min_x[target_col],
                       max_x[target_col],
                       plot=sa_plot,
                       iterations=iterations)
    # Write FIS file in the format:
    # FIS_name.FIS
    with open(FIS_name + '.FIS', "w") as fis_file:
        write(fis_file, method, mf, overlap, target_centroids,
              feature_centroids, RB)