1
def CreateData(self, n_samples, seed_in=5, 
        train_prop=0.9, bound_limit=6., n_std_devs=1.96,**kwargs):

        np.random.seed(seed_in)
        scale_c=1.0 # default
        shift_c=1.0
        
        # for ideal boundary
        X_ideal = np.linspace(start=-bound_limit,stop=bound_limit, num=50000)
        y_ideal_U = np.ones_like(X_ideal)+1. # default
        y_ideal_L = np.ones_like(X_ideal)-1.
        y_ideal_mean = np.ones_like(X_ideal)+0.5

        if self.type_in[:1] == '~':
            if self.type_in=="~boston":
                path = 'boston_housing_data.csv'
                data = np.loadtxt(path,skiprows=0)
            elif self.type_in=="~concrete":
                path = 'Concrete_Data.csv'
                data = np.loadtxt(path, delimiter=',',skiprows=1)
            elif self.type_in=="~wind":
                path = '/content/Deep_Learning_Prediction_Intervals/code/canada_CSV.csv'
                data = np.loadtxt(path,delimiter=',',skiprows=1,usecols = (1,2)) ## CHECK WHTHER TO HAVE LOADTXT OR ANYTHING ELSE PARUL
            
            
            # work out normalisation constants (need when unnormalising later)
            scale_c = np.std(data[:,-1])
            shift_c = np.mean(data[:,-1])

            # normalise data for ALL COLUMNS
            for i in range(0,data.shape[1]): ## i varies from 0 to number of columns ,means it reads one by one the columns
                # avoid zero variance features (exist one or two)
#               nonlocal X_train, y_train, X_val, y_val ## ADDED BY PARUL
                sdev_norm = np.std(data[:,i])
                sdev_norm = 0.001 if sdev_norm == 0 else sdev_norm
                data[:,i] = (data[:,i] - np.mean(data[:,i]) )/sdev_norm

            # split into train/test
            perm = np.random.permutation(data.shape[0]) ## DO THE DATA PERMUTATION OF ALL THE ROWS (shuffle)
            train_size = int(round(train_prop*data.shape[0]))
            train = data[perm[:train_size],:]
            test = data[perm[train_size:],:]
            
            
            y_train = train[:,-1].reshape(-1,1) ## LAST COLUMN IS CONSIDERED AS THE TARGET AND RESHAPED IN BETWEEN -1,1
            X_train = train[:,:-1] ## INPUTS ARE ALL EXCEPT LAST COLUMN
            y_val = test[:,-1].reshape(-1,1)
            X_val = test[:,:-1]

            # save important stuff
            self.X_train = X_train
            self.y_train = y_train
            self.X_val = X_val
            self.y_val = y_val
            self.X_ideal = X_ideal
            self.y_ideal_U = y_ideal_U
            self.y_ideal_L = y_ideal_L
            self.y_ideal_mean = y_ideal_mean
            self.scale_c = scale_c
            self.shift_c = shift_c

        return X_train, y_train, X_val, y_val

It gives me an error 'UnboundLocalError: local variable 'X_train' referenced before assignment'

Any help will be appreciated. I am stuck at this. I have tried initialising X_train with X_train=[] and also tried making them global, but didn't get any result. Please help me so that I could move forward.

  • `X_train` is set `if self.type_in[:1] == '~'`. But you try to return it even if this is not the case. Maybe it's enough if you initialize it at the start of the function with an appropriate value. – Matthias Apr 08 '21 at 09:12
  • Thanks a lot.I removed this line and now code is working fine. – Parul Arora Apr 09 '21 at 04:47

0 Answers0