Esempio n. 1
0
def random_sampling(file_name):
	main = eda.eda()

	# Path and file name of the data file
	# Uncomment the following code segment for CSV data file -- start
	''' column (aka. feature) seperator, i.e, sequence of characters (or a regex) that seperate two features of a data sample '''

	seperator = ',' # default
	''' number of initial lines <int> or list of line numbers <list> of the data file to skip before start of data samples 
	Note: include the commented and blank lines in the count of intial lines to skip. Don't skip over header line (with column names) if any '''

	skiplines = None # default: no lines to skip (aka. skiplines = 0)

	''' relative zero-index based line number of header line containing column names to use
	Note: 
		* The indexing starts (from line number=0) for lines immediately following the skipped lines.
		* Blank lines are ignored in the indexing.
		* All lines following the skipped lines until the header line are ignored.
	'''

	headerline = 0 # default: No header lines (containing column names) to use.
	''' List of columns to use from the data file 
	Note: 
		* Use list of column names (as inferred from header line) or zero-based column indices (only if no header line)
		* Include the 'target' value column in the list of columns to use
	'''

	usecolumns = None # default: Use all columns from data file
	''' relative zero-based index (among selected columns) or column name (as inferred from header line) of 'target' values column '''

	targetcol = -1 # default: last column among list of selected columns

	''' Should target values be treated as nominal values and be encoded (with zero-indexed integers) '''

	encode_target = True # default: Encode target values

	''' List of column names (as inferred from header line) or absolute column indices (index of column as in data file) if no header line
	of nominal categorical columns to encode. 

	Note:
		* nominal_cols='infer' infers all string dtype columns with relatively large number of unique entries as 'string' or 'date' features and drops them from the dataset.
	'''
	nominal_cols='infer'
	#nominal_cols = None # No nominal categorical columns
	''' List of strings to be inferred as NA (Not Available) or NaN (Not a Number) in addition to the default NA string values.
	Default NA values :  ‘’, ‘#N/A’, ‘#N/A N/A’, ‘#NA’, ‘-1.#IND’, ‘-1.#QNAN’, ‘-NaN’, ‘-nan’, ‘1.#IND’, ‘1.#QNAN’, ‘N/A’, ‘NA’, ‘NULL’, ‘NaN’, ‘n/a’, ‘nan’, ‘null’
	'''
	na_values = None # default: no additional strings (asides default strings) to infer as NA/NaN

	''' Dictionary of other keyword arguments accepted by :func:`pandas.read_csv` (Keyword Arguments: comment, lineterminator, ...) '''
	kargs = {}
	curr_pwd = str(os.getcwd())
	data_pwd = curr_pwd + str('/data/')
	main.read_data_csv(data_pwd+file_name, sep=seperator, skiprows=skiplines, header_row=headerline, usecols=usecolumns, target_col=targetcol, encode_target=encode_target, categorical_cols=nominal_cols, na_values=na_values, nrows=None, **kargs)
	
	#main.load_data(data,target)

	""" Perform dummy coding of nominal columns (or features) """
	nominal_columns = 'infer' # Default: Use list of nominal columns supplied to (or inferred by) :func:`read_data_csv` or :func:`read_data_arff`
	#print(main.dat)
	main.dummy_coding(nominal_columns=nominal_columns)
	#main.standardize_data()
	""" Perform repeated stratified  sampling (with replacement across samplings) of dataset into bags """
	# Name of folder that contains sampled bags and metadata files





	bags_setting  = int(input("Enter setting {0:Oneshot , 1:Sub-sampled bags} :"))

	if(bags_setting == 0):
		print("Experiment carried out in oneshot setting ")
		sample_size = main.n_samples

	else:
		print("Experiment carried out in Random sub-sampled bags setting ")
		if(main.n_samples > 1000):
			print('choosing default bags_size=500')
			sample_size = 500
		else:
			if(main.n_samples >500 ):
				print("dataset size less than 1000")	
				print("bag size is fixed to be half of dataset size ")
				sample_size = int(main.n_samples/2)
			else:
				print("the dataset size is less than 500")
				sample_size  = int(input("please enter the desired bag size:")) 




	# """applying thumb rule for n_bags and sample_size"""
	# if("oneshot" in file_name):
	# 	sample_size = main.n_samples
	# else:
	# 	if(main.n_samples > 1000):
	# 			print('choosing default bags_size=500')
	# 			sample_size = 500
	# 	else:
	# 		print("dataset size less than 1000")	
	# 		print("bag size is fixed to be half of dataset size ")
	# 		sample_size = int(main.n_samples/2)	
				





	"""Determining number of bags to be drawn from the dataset """
	# print("bag n_samples :{}".format(main.n_samples))
	unique_data_pts = 0.63 * sample_size 
	no_partitions = main.n_samples / unique_data_pts
	n_bags = np.round(5 * no_partitions)
	# if('oneshot' in file_name):
	# 	n_bags=1

	if(bags_setting==0):
		n_bags = 1




	# Display bag size and number of bags
	print("Bag size :{}".format(sample_size))
	print("number of bags:{}".format(np.round(n_bags)))


	# unique bag name for bags (after sampling)
	bag_name= file_name.split('.')[0] + '_' + 'stratified'
	
	try:
		bags_pwd  = curr_pwd + str('/bags/') 
		os.chdir(str(bags_pwd))        
		os.mkdir(file_name.split('.')[0])

	except OSError as err:
		print("error: Unable to write sampled data bags to disk.\n{0}".format(err))
		print("Over-writing to  the same folder ",file_name.split('.')[0])
		shutil.rmtree(bags_pwd+ file_name.split('.')[0])
		
		os.mkdir(file_name.split('.')[0])

		# sys.exit(1)
	

	# Validating the sample size parameter
	if isinstance(sample_size, int) and (sample_size>0 and sample_size<=main.n_samples):
		pass

	elif isinstance(sample_size, float) and (sample_size>0.0 and sample_size<=1.0):
		pass

	else:
		print("error: Invalid sampling size encountered")
		sys.exit(1)



	file_prefix = None

	if file_prefix is None:
		file_prefix = ''

	else:
		file_prefix = bag_name + '/'



	# Random sampling  procedure 
	bincount = np.bincount(main.target)
	# print("bincount:{}".format(bincount))
	
	#count of data points belonging to classes
	class0_count = bincount[0]
	class1_count = bincount[1]
	# no.of.random samples to be derived from each class
	n_class0 = round((bincount[0]/np.sum(bincount))*sample_size)
	n_class1 = round((bincount[1]/np.sum(bincount))*sample_size)

	#getting all index of datapoints belonging to class
	class0_index = np.where(main.target == 0)
	class1_index = np.where(main.target == 1)
	bag_number = 0
	tot_rep = 0 
	sample_bags =[]
	for i in range(int(n_bags)):
	#creating random index numbers
		random.seed()
		prob0 = 1 / class0_count
		prob1 = 1 / class1_count

		rand_class0_index=[]
		for j in range (int(n_class0)):
				rand_num = random.uniform(0,1)
				cdf_val = int(rand_num * class0_count) 
				rand_class0_index.append(cdf_val)


		#  repetations in class 0 
		rep0= [item for item, count in Counter(rand_class0_index).items() if count > 1]

		rand_class1_index=[]
		for j in range (int(n_class1)):
			rand_num = random.uniform(0,1)
			cdf_val = int(rand_num * class1_count) 
			rand_class1_index.append(cdf_val)

		#  repetations in class 1
		rep1= [item for item, count in Counter(rand_class1_index).items() if count > 1]

		sampled_class0 = [class0_index[0][x] for x in rand_class0_index]
		sampled_class1 = [class1_index[0][x] for x in rand_class1_index]

		#print( sampled_class0 in sampled_class1)

		final_indices = sampled_class0 + sampled_class1

		rep= [item for item, count in Counter(final_indices).items() if count > 1]
		tot_rep =  tot_rep + len(rep)

		#fetching datapoints to the corresponding index numbers
		sampled_data = dict.fromkeys(['data', 'target','classes','n_samples','n_features','column_names','column_categories','dataset_name','bag_number'])
		sampled_data['data'], sampled_data['target'] = main.data[final_indices], main.target[final_indices]	
		sampled_data['classes']=label_cnt_dict(main.target) if main.target is not None else None
		sampled_data['n_samples']=main.n_samples # Not inferrable from classes, if target=None
		sampled_data['n_features']=main.n_features,
		sampled_data['column_names']=main.columns_,
		sampled_data['column_categories']=main.columns_categories_ if hasattr(main, 'columns_categories_') else None	
		sampled_data['dataset_name']= file_name
		sampled_data['bag_number']= bag_number
        
		os.chdir(bags_pwd+file_name.split('.')[0])


		df = DataFrame.from_records(main.data[final_indices])
		df[''] = main.target[final_indices]


		# saving bags in the data folder
		df.to_csv(str(file_prefix +file_name.split('.')[0] +"_bag"+str(bag_number+1)+".csv"),header=None,index=False,index_label=False)
		bag_number = bag_number + 1
		sample_bags.append(sampled_data) 
		
	os.chdir(curr_pwd)
	return sample_bags
Esempio n. 2
0
def svc(sampled_data):

    bag_name = sampled_data[0]['dataset_name'].split('.')[0]
    n_bags = len(sampled_data)
    curr_pwd = str(os.getcwd())
    indices_pwd = curr_pwd + '/true_f1_scores/'
    os.chdir(indices_pwd)
    file_name = bag_name + '_svc_f1_weighted' + '.csv'
    results_file = open(file_name, 'a')
    tot_svc_result = []
    for x in range(len(sampled_data)):
        print("Processing '{file_name}'".format(
            file_name=bag_name + "_bag_" + str(sampled_data[x]['bag_number'])))
        bag_name_x = sampled_data[0]['dataset_name'].split('.')[0] + '_' + str(
            sampled_data[x]['bag_number'])
        dataset = sampled_data[x]
        data, target = dataset['data'], dataset['target']

        main = eda.eda()
        main.load_data(data, target)

        # print("\n  bag  dist\n")
        # print(np.count_nonzero(target==0))
        # print(np.count_nonzero(target==1))

        X_train, X_test, y_train, y_test = train_test_split(data,
                                                            target,
                                                            test_size=0.3,
                                                            stratify=target)

        f1_minority_score = 0
        f1_majority_score = 0
        counter = 0

        parameters = {
            'C': [10**-2, 10**-1, 10**0, 10**1, 10**2],
            'gamma': ['scale']
        }

        while ((f1_minority_score == 0 or f1_majority_score == 0)):
            # print("counter:",counter)

            # print("--------------------------------imbalance_ratio 1:",imbalance_ratio(y_train))
            # print("\n--------------------------------train-dist--------------------------------\n")
            # print(np.count_nonzero(y_train==0))
            # print(np.count_nonzero(y_train==1))

            class_dist = np.bincount(y_test).tolist()
            majority_class_label = class_dist.index(max(class_dist))
            minority_class_label = class_dist.index(min(class_dist))

            if (counter > 1):
                if (counter > 1 and counter < 5):
                    # print("****balanced***")
                    svc_clf = GridSearchCV(SVC(class_weight='balanced',
                                               cache_size=3000,
                                               shrinking=True,
                                               max_iter=15000),
                                           parameters,
                                           cv=5,
                                           n_jobs=20,
                                           verbose=False,
                                           refit=True,
                                           scoring=custom_f1)
                    svc_clf.fit(X_train, y_train)
                if (counter >= 5 and counter <= 7):
                    # print("****class_weight_dict")
                    class_weight_dict = dict()
                    class_weight_dict[majority_class_label] = float(
                        imbalance_ratio(y_train) / counter)
                    class_weight_dict[minority_class_label] = (
                        imbalance_ratio(y_train) + (counter * 2))
                    # print("****class_weight_dict:",class_weight_dict)

                    svc_clf = GridSearchCV(SVC(class_weight=class_weight_dict,
                                               cache_size=3000,
                                               max_iter=15000,
                                               shrinking=True),
                                           parameters,
                                           cv=5,
                                           n_jobs=20,
                                           verbose=False,
                                           refit=True,
                                           scoring=custom_f1)
                    svc_clf.fit(X_train, y_train)
                if (counter >= 8 and counter < 12):
                    # print("****sample_weight")
                    weight_ratio = float(
                        len(y_train[y_train == majority_class_label])) / float(
                            len(y_train[y_train == minority_class_label]))
                    w_array = np.array([1] * y_train.shape[0])
                    w_array[y_train == minority_class_label] = weight_ratio + (
                        counter * 2)
                    w_array[y_train == majority_class_label] = -(weight_ratio)
                    fit_parameters = {'sample_weight': w_array}

                    # print('****sample_weight: {}'.format(w_array))

                    svc_clf = GridSearchCV(SVC(cache_size=3000,
                                               shrinking=True,
                                               max_iter=15000),
                                           parameters,
                                           cv=5,
                                           n_jobs=20,
                                           verbose=False,
                                           refit=True,
                                           scoring=custom_f1)
                    svc_clf.fit(X_train, y_train, **fit_parameters)
                if (counter >= 12):
                    parameters = {
                        'C': [10**-2, 10**-1, 10**0, 10**1, 10**2],
                        'kernel': ['rbf', 'poly'],
                        'degree': [2, 3, 4, 5]
                    }
                    svc_clf = GridSearchCV(SVC(cache_size=15000,
                                               tol=1e-1,
                                               shrinking=True,
                                               max_iter=150000),
                                           parameters,
                                           cv=5,
                                           n_jobs=20,
                                           verbose=False,
                                           refit=True,
                                           scoring=custom_f1)
                    svc_clf.fit(X_train, y_train)

            else:
                svc_clf = GridSearchCV(SVC(cache_size=3000),
                                       parameters,
                                       cv=5,
                                       n_jobs=20,
                                       verbose=False,
                                       refit=True,
                                       scoring=custom_f1)
                svc_clf.fit(X_train, y_train)

            y_pred = svc_clf.predict(X_test)

            svc_f1_score = f1_ratio(y_test, y_pred)
            # print("majority_class_f1:{}".format(f1_majority(y_test,y_pred)))
            # print("minority_class_f1:{}".format(f1_minority(y_test,y_pred)))
            # print("\n--------------------------------after fit test  dist--------------------------------\n")
            # print(np.count_nonzero(y_pred==0))
            # print(np.count_nonzero(y_pred==1))
            if (f1_majority(y_test, y_pred) == 0
                    and f1_minority(y_test, y_pred) > 0):
                if (counter >= 5 and counter <= 7):
                    # print("****balanced re-fit")
                    class_weight_dict = dict()
                    class_weight_dict[majority_class_label] = float(
                        imbalance_ratio(y_train) / (counter) + 0.5)
                    class_weight_dict[minority_class_label] = (
                        imbalance_ratio(y_train) + (counter * 2))
                    # print("****class_weight_dict:",class_weight_dict)
                    svc_clf = GridSearchCV(SVC(class_weight=class_weight_dict,
                                               cache_size=3000,
                                               max_iter=13000,
                                               shrinking=True),
                                           parameters,
                                           cv=5,
                                           n_jobs=20,
                                           verbose=False,
                                           refit=True,
                                           scoring=custom_f1)
                    svc_clf.fit(X_train, y_train)

                    # print("majority_class_f1:{}".format(f1_majority(y_test,y_pred)))
                    # print("minority_class_f1:{}".format(f1_minority(y_test,y_pred)))

            f1_minority_score = f1_minority(y_test, y_pred)
            f1_majority_score = f1_majority(y_test, y_pred)
            counter = counter + 1
            if (counter > 15):

                # print("XXXXXX tried 15 times XXXXXX")
                return
            if (f1_minority_score == 0 or f1_majority_score == 0):
                # print("---------re-fitting------")
                X_train, X_test, y_train, y_test = train_test_split(
                    data,
                    target,
                    test_size=0.3,
                    stratify=target,
                    random_state=counter**2)

        results_file.write(
            "\"{file_name}\",{sample_size},\"{algorithm}\",\"{parameters}\",\"{majority_class_f1}\",\"{minority_class_f1}\",\"{svc_f1_score}\","
            .format(file_name=bag_name_x,
                    sample_size=main.n_samples,
                    algorithm='SVC(linear/rbf)',
                    parameters=svc_clf.best_estimator_,
                    majority_class_f1=f1_majority(y_test, y_pred),
                    minority_class_f1=f1_minority(y_test, y_pred),
                    svc_f1_score=f1_ratio(y_test, y_pred)))
        results_file.write('\n')

        svc_result = {
            "file_name": str(bag_name + '_' + str(x) + '_bag'),
            "sample_size": main.n_samples,
            "algorithm": 'svc(linear/rbf)',
            "parameters": svc_clf.best_estimator_,
            "majority_class_f1": f1_majority(y_test, y_pred),
            "minority_class_f1": f1_minority(y_test, y_pred),
            "svc_f1_score": f1_ratio(y_test, y_pred)
        }
        tot_svc_result.append(svc_result)

    results_file.close()
    os.chdir(curr_pwd)
    return "SVC classifier building done using 5 fold validation"
Esempio n. 3
0
def main():
    """Wrapper function which calls all the other functions"""
    rolled_df = deriving_features.create_dataframe_with_features()
    print(rolled_df.columns)
    target = input("Enter the column name of y variable:")
    e = eda(rolled_df, target)
    event = input("Enter the event:")
    er = e.eventRatio(event)
    print(er)
    stat = e.impstat()
    rng = e.range()
    rng = rng.rename('Range')
    print("Size is")
    print(rng.size)
    iq = e.iqr()
    iq = iq.rename('IQR')
    cor = e.corr()
    print(cor)
    ske = e.skew()
    print(ske)
    ske = ske.rename('Skewness')
    kur = e.kurt()
    print(kur)
    kur = kur.rename('Kurtosis')
    [mi, mi1] = e.missinginfo()
    print("missing value is")
    print(pd.Series(mi1[1:]))
    mi1 = mi1.rename('Missing values')
    e.missingplot()
    b = e.bin('woe', 'y', 'yes', 'pdays', 'day')
    print(b)
    try:
        os.remove('D:/Other projects/python modules/Report.xlsx')
        engine = 'xlsxwriter'
        writer = pd.ExcelWriter('Report.xlsx', engine=engine)
        stat1 = pd.DataFrame(stat.T)
        print(stat1)
        stat1.to_csv(writer, startcol=0, startrow=5)
        ws = writer.sheets['Sheet1']
        ws.write_string(1, 4, 'DataDescription')
        rng.to_excel(writer, startcol=9, startrow=5, index=False)
        iq.to_excel(writer, startcol=10, startrow=5, index=False)
        ske.to_excel(writer, startcol=11, startrow=5, index=False)
        kur.to_excel(writer, startcol=12, startrow=5, index=False)
        ws.write_string(5 + rng.size + 2, 5, 'Correlation')
        cor.to_excel(writer, startcol=0, startrow=5 + rng.size + 4)
        #        mi1[1:].to_excel(writer,startcol=rng.size+2,startrow=5+rng.size+4)
        b.to_excel(writer, startcol=12, startrow=5 + rng.size + 4)
        #        ws.write_string(5+rng.size+2,14,'Binning')
        #        misplot.to_excel(writer,startcol=0,startrow=rng.size+rng.size+5+3+5)
        writer.close()
    except:
        engine = 'xlsxwriter'
        writer = pd.ExcelWriter('Report.xlsx', engine=engine)
        stat1 = pd.DataFrame(stat.T)
        print(stat1)
        stat1.to_excel(writer, startcol=0, startrow=5)
        ws = writer.sheets['Sheet1']
        ws.write_string(1, 4, 'DataDescription')
        rng.to_excel(writer, startcol=9, startrow=5, index=False)
        iq.to_excel(writer, startcol=10, startrow=5, index=False)
        ske.to_excel(writer, startcol=11, startrow=5, index=False)
        kur.to_excel(writer, startcol=12, startrow=5, index=False)
        ws.write_string(5 + rng.size + 2, 5, 'Correlation')
        cor.to_excel(writer, startcol=0, startrow=5 + rng.size + 4)
        mi1[1:].to_excel(writer,
                         startcol=rng.size + 2,
                         startrow=5 + rng.size + 4)
        writer.close()
    ft = feature_transformation(rolled_df, target)
    p = True
    while (p):
        degree = input(
            "Enter the degree of the polynomial features you want to derive")
        try:
            degree = int(degree)
            p = False
        except:
            print("You did not enter correct value. Try again")
            p = True

    poly_feature_set = ft.poly_features()
    feature_transformed_df = ft.transformation()
    cols_to_use = poly_feature_set.columns.difference(
        feature_transformed_df.columns)
    final_df = pd.merge(feature_transformed_df,
                        poly_feature_set[cols_to_use],
                        left_index=True,
                        right_index=True,
                        how='outer')
    print(final_df.columns)
    cat = [x for x in final_df.columns
           if final_df[x].dtypes == 'object'].copy()
    label_encod = ft.label_encoding(final_df)
    one_hot = ft.one_hot_encoding(label_encod, cat)
    cols_use = one_hot.columns.difference(final_df.columns)
    final_f = pd.merge(final_df,
                       one_hot[cols_use],
                       left_index=True,
                       right_index=True,
                       how='outer')
    nystroem_rbf_dataframe = ft.kernel_transformation_using_nystroem_rbf(
        final_f, cat)
    cols_needed = nystroem_rbf_dataframe.columns.difference(final_df)
    final_data = pd.merge(final_df,
                          nystroem_rbf_dataframe[cols_needed],
                          left_index=True,
                          right_index=True,
                          how='outer')
    f = 0
    p1 = True
    while (p1):
        try:
            p1 = False
            f = input(
                "Enter 1 if you want to write the dataframe into a csv file else enter 0:"
            )
            if (int(f) == 1):
                path = input("Enter the path where you want to save:")
                final_data.to_csv(path, index=False)
        except:
            p1 = True
            print("You have entered wrong value. Please try again.")

#    ml=machine_learning(final_data,target)
    datecol = [
        x for x in final_data.columns
        if final_data[x].dtypes == 'datetime64[ns]'
    ]
    X1 = [
        x for x in final_data.columns if final_data[x].dtypes != 'object'
        and x not in datecol and x not in target
    ]
    X = [x for x in X1 if x not in cat]
    fs = feature_selection(final_data, target)
    fs.recursive_feature_elimination(X)
Esempio n. 4
0
def xg(sampled_data):

    bag_name = sampled_data[0]['dataset_name'].split('.')[0]
    n_bags = len(sampled_data)
    curr_pwd = str(os.getcwd())
    indices_pwd = curr_pwd + '/true_f1_scores/'
    os.chdir(indices_pwd)
    file_name = bag_name + '_xg_f1_weighted' + '.csv'
    results_file = open(file_name, 'a')
    tot_dt_result = []
    for x in range(len(sampled_data)):
        print("Processing '{file_name}'".format(
            file_name=bag_name + "_bag_" + str(sampled_data[x]['bag_number'])))
        bag_name_x = sampled_data[0]['dataset_name'].split('.')[0] + '_' + str(
            sampled_data[x]['bag_number'])
        dataset = sampled_data[x]
        data, target = dataset['data'], dataset['target']

        main = eda.eda()
        main.load_data(data, target)

        # print("\n  bag  dist\n")
        # print(np.count_nonzero(target==0))
        # print(np.count_nonzero(target==1))

        X_train, X_test, y_train, y_test = train_test_split(data,
                                                            target,
                                                            test_size=0.3,
                                                            stratify=target)
        class_dist = np.bincount(y_test).tolist()
        majority_class_label = class_dist.index(max(class_dist))
        minority_class_label = class_dist.index(min(class_dist))
        f1_minority_score = 0
        while (f1_minority_score == 0):

            weight_ratio = float(len(
                y_train[y_train == majority_class_label])) / float(
                    len(y_train[y_train == minority_class_label]))
            w_array = np.array([1] * y_train.shape[0])
            w_array[y_train == minority_class_label] = weight_ratio
            w_array[y_train == majority_class_label] = 1
            fit_parameters = {'sample_weight': w_array}

            parameters = {
                'learning_rate': np.linspace(0.05, 0.3, 3),
                'max_depth': [2**0, 2**1, 2**2, 2**3, 2**4, 2**5, 2**6]
            }
            xg_clf = GridSearchCV(xgboost.XGBClassifier(),
                                  parameters,
                                  cv=5,
                                  n_jobs=20,
                                  refit=True,
                                  scoring=custom_f1)
            xg_clf.fit(X_train, y_train, **fit_parameters)

            y_pred = xg_clf.predict(X_test)

            xg_f1_score = f1_ratio(y_test, y_pred)
            # print("majority_class_f1:{}".format(f1_majority(y_test,y_pred)))
            # print("minority_class_f1:{}".format(f1_minority(y_test,y_pred)))

            f1_minority_score = f1_minority(y_test, y_pred)
            if (f1_minority_score == 0):
                # print("---------re-fitting------")
                X_train, X_test, y_train, y_test = train_test_split(
                    data, target, test_size=0.3, stratify=target)

        results_file.write(
            "\"{file_name}\",{sample_size},\"{algorithm}\",\"{parameters}\",\"{majority_class_f1}\",\"{minority_class_f1}\",\"{rf_f1_score}\","
            .format(file_name=bag_name_x,
                    sample_size=main.n_samples,
                    algorithm='xgboost',
                    parameters=xg_clf.best_estimator_,
                    majority_class_f1=f1_majority(y_test, y_pred),
                    minority_class_f1=f1_minority(y_test, y_pred),
                    rf_f1_score=xg_f1_score))
        results_file.write('\n')

        rf_result = {
            "file_name": str(bag_name + '_' + str(x) + '_bag'),
            "sample_size": main.n_samples,
            "algorithm": 'xgboost',
            "parameters": xg_clf.best_estimator_,
            "majority_class_f1": f1_majority(y_test, y_pred),
            "minority_class_f1": f1_minority(y_test, y_pred),
            "rf_f1_score": xg_f1_score
        }
        tot_dt_result.append(rf_result)

    results_file.close()
    os.chdir(curr_pwd)
    return "XG classifier building done using 5 fold validation"
Esempio n. 5
0
def knn(sampled_data):
    bag_name = sampled_data[0]['dataset_name'].split('.')[0]
    n_bags = len(sampled_data)
    curr_pwd = str(os.getcwd())
    indices_pwd = curr_pwd + '/true_f1_scores/'
    os.chdir(indices_pwd)
    file_name = bag_name + '_knn_f1_weighted' + '.csv'
    results_file = open(file_name, 'a')
    tot_knn_result = []

    for x in range(len(sampled_data)):
        print("Processing '{file_name}'".format(
            file_name=bag_name + "_bag_" + str(sampled_data[x]['bag_number'])))
        bag_name_x = sampled_data[0]['dataset_name'].split('.')[0] + '_' + str(
            sampled_data[x]['bag_number'])
        dataset = sampled_data[x]
        data, target = dataset['data'], dataset['target']

        main = eda.eda()
        main.load_data(data, target)

        # print("\n  bag  dist\n")
        # print(np.count_nonzero(target==0))
        # print(np.count_nonzero(target==1))

        X_train, X_test, y_train, y_test = train_test_split(data,
                                                            target,
                                                            test_size=0.3,
                                                            stratify=target)
        class_dist = np.bincount(y_test).tolist()
        majority_class_label = class_dist.index(max(class_dist))
        minority_class_label = class_dist.index(min(class_dist))

        if (imbalance_ratio(y_test) >= 10):
            print("-----entering imbalace class----")
            k_value = imbalance_ratio(y_test)
            f1_minority_score = 0
            while (f1_minority_score == 0):

                parameters = {
                    'weights': ['uniform', 'distance'],
                    'leaf_size': [2, 5, 10, 15, 20, 25, 30]
                }
                neigh_clf = GridSearchCV(
                    KNeighborsClassifier(n_neighbors=k_value),
                    parameters,
                    cv=5,
                    n_jobs=20,
                    refit=True,
                    scoring=custom_f1)
                neigh_clf.fit(X_train, y_train)

                y_pred_prob = neigh_clf.predict_proba(X_test)
                y_pred = []
                if (minority_class_label == 1):

                    for i in range(len(y_pred_prob)):
                        if y_pred_prob[i, 1] > (1 / imbalance_ratio(y_test)):
                            y_pred.append(1)
                        else:
                            y_pred.append(0)
                else:
                    for i in range(len(y_pred_prob)):
                        if y_pred_prob[i, 0] > (1 / imbalance_ratio(y_test)):
                            y_pred.append(0)
                        else:
                            y_pred.append(1)
                # print("\n  train set dist\n")
                # print(np.count_nonzero(y_test==0))
                # print(np.count_nonzero(y_test==1))
                # print("\n prediction dist ")
                # print(np.count_nonzero(y_pred==0))
                # print(np.count_nonzero(y_pred==1))
                knn_f1_score = f1_ratio(y_test, y_pred)
                # print("majority_class_f1:{}".format(f1_majority(y_test,y_pred)))
                # print("minority_class_f1:{}".format(f1_minority(y_test,y_pred)))
                f1_minority_score = f1_minority(y_test, y_pred)
                if (f1_minority_score == 0):
                    print("---------re-fitting------")
                    X_train, X_test, y_train, y_test = train_test_split(
                        data, target, test_size=0.3, stratify=target)

            results_file.write(
                "\"{file_name}\",{sample_size},\"{algorithm}\",\"{parameters}\",\"{majority_class_f1}\",\"{minority_class_f1}\",\"{knn_f1_score}\","
                .format(file_name=bag_name_x,
                        sample_size=main.n_samples,
                        algorithm='KNearestNeighbour-imbalanced',
                        parameters=neigh_clf.best_estimator_,
                        majority_class_f1=f1_majority(y_test, y_pred),
                        minority_class_f1=f1_minority(y_test, y_pred),
                        knn_f1_score=knn_f1_score))
            results_file.write('\n')

            knn_result = {
                "file_name": str(bag_name + '_' + str(x) + '_bag'),
                "sample_size": main.n_samples,
                "algorithm": 'KNeighborsClassifier-imbalances',
                "parameters": neigh_clf.best_estimator_,
                "majority_class_f1": f1_majority(y_test, y_pred),
                "minority_class_f1": f1_minority(y_test, y_pred),
                "knn_f1_score": knn_f1_score
            }
            tot_knn_result.append(knn_result)
        else:

            f1_minority_score = 0
            while (f1_minority_score == 0):
                parameters = {
                    'n_neighbors': [3, 5, 7, 9],
                    'weights': ['uniform', 'distance'],
                    'leaf_size': [2, 5, 10, 15, 20, 25, 30]
                }
                neigh_clf = GridSearchCV(KNeighborsClassifier(),
                                         parameters,
                                         cv=5,
                                         n_jobs=20,
                                         refit=True,
                                         scoring=custom_f1)
                neigh_clf.fit(X_train, y_train)

                y_pred = neigh_clf.predict(X_test)
                # print("\n  train set dist\n")
                # print(np.count_nonzero(y_test==0))
                # print(np.count_nonzero(y_test==1))
                # print("\n prediction dist ")
                # print(np.count_nonzero(y_pred==0))
                # print(np.count_nonzero(y_pred==1))
                knn_f1_score = f1_ratio(y_test, y_pred)
                # print("majority_class_f1:{}".format(f1_majority(y_test,y_pred)))
                # print("minority_class_f1:{}".format(f1_minority(y_test,y_pred)))
                f1_minority_score = f1_minority(y_test, y_pred)
                if (f1_minority_score == 0):
                    # print("---------re-fitting------")
                    X_train, X_test, y_train, y_test = train_test_split(
                        data, target, test_size=0.3, stratify=target)

            results_file.write(
                "\"{file_name}\",{sample_size},\"{algorithm}\",\"{parameters}\",\"{majority_class_f1}\",\"{minority_class_f1}\",\"{knn_f1_score}\","
                .format(file_name=bag_name_x,
                        sample_size=main.n_samples,
                        algorithm='KNearestNeighbour',
                        parameters=neigh_clf.best_estimator_,
                        majority_class_f1=f1_majority(y_test, y_pred),
                        minority_class_f1=f1_minority(y_test, y_pred),
                        knn_f1_score=knn_f1_score))
            results_file.write('\n')

            knn_result = {
                "file_name": str(bag_name + '_' + str(x) + '_bag'),
                "sample_size": main.n_samples,
                "algorithm": 'KNeighborsClassifier',
                "parameters": neigh_clf.best_estimator_,
                "majority_class_f1": f1_majority(y_test, y_pred),
                "minority_class_f1": f1_minority(y_test, y_pred),
                "knn_f1_score": knn_f1_score
            }
            tot_knn_result.append(knn_result)

    results_file.close()
    os.chdir(curr_pwd)
    return "KNN classifier building done using 5 fold validation"
Esempio n. 6
0
def lr(sampled_data):
    bag_name = sampled_data[0]['dataset_name'].split('.')[0]
    n_bags = len(sampled_data)
    curr_pwd = str(os.getcwd())
    indices_pwd = curr_pwd + '/true_f1_scores/'
    os.chdir(indices_pwd)
    file_name = bag_name + '_lr_f1_weighted' + '.csv'
    results_file = open(file_name, 'a')
    tot_lr_result = []

    for x in range(len(sampled_data)):
        print("Processing '{file_name}'".format(
            file_name=bag_name + "_bag_" + str(sampled_data[x]['bag_number'])))
        bag_name_x = sampled_data[0]['dataset_name'].split('.')[0] + '_' + str(
            sampled_data[x]['bag_number'])
        dataset = sampled_data[x]
        data, target = dataset['data'], dataset['target']

        main = eda.eda()
        main.load_data(data, target)

        # print("\n  bag  dist\n")
        # print(np.count_nonzero(target==0))
        # print(np.count_nonzero(target==1))

        X_train, X_test, y_train, y_test = train_test_split(data,
                                                            target,
                                                            test_size=0.3,
                                                            stratify=target)

        f1_minority_score = 0
        while (f1_minority_score == 0):
            class_dist = np.bincount(y_test).tolist()
            majority_class_label = class_dist.index(max(class_dist))
            minority_class_label = class_dist.index(min(class_dist))
            weight_ratio = float(len(
                y_train[y_train == majority_class_label])) / float(
                    len(y_train[y_train == minority_class_label]))
            w_array = np.array([1] * y_train.shape[0])
            w_array[y_train == minority_class_label] = weight_ratio
            w_array[y_train == majority_class_label] = 1
            fit_parameters = {'sample_weight': w_array}
            parameters = {
                'C': [10**-3, 10**-2, 10**-1, 10**0, 10**1, 10**2, 10**3],
                'solver': ['saga']
            }

            log_reg_clf = GridSearchCV(LogisticRegression(
                penalty='l1',
                class_weight='balanced',
                tol=0.01,
                max_iter=500,
                l1_ratio=weight_ratio),
                                       parameters,
                                       cv=5,
                                       n_jobs=20,
                                       refit=True,
                                       scoring=custom_f1)
            log_reg_clf.fit(X_train, y_train)

            y_pred = log_reg_clf.predict(X_test)
            # print("majority_class_f1:{}".format(f1_majority(y_test,y_pred)))
            # print("minority_class_f1:{}".format(f1_minority(y_test,y_pred)))

            # print("\n  train set dist\n")
            # print(np.count_nonzero(y_test==0))
            # print(np.count_nonzero(y_test==1))
            # print("\n prediction dist ")
            # print(np.count_nonzero(y_pred==0))
            # print(np.count_nonzero(y_pred==1))
            lr_f1_score = f1_ratio(y_test, y_pred)
            f1_minority_score = f1_minority(y_test, y_pred)
            if (f1_minority_score == 0):
                print("---------re-fitting------")
                X_train, X_test, y_train, y_test = train_test_split(
                    data, target, test_size=0.3, stratify=target)

        results_file.write(
            "\"{file_name}\",{sample_size},\"{algorithm}\",\"{parameters}\",\"{majority_class_f1}\",\"{minority_class_f1}\",\"{lr_f1_score}\","
            .format(file_name=bag_name_x,
                    sample_size=main.n_samples,
                    algorithm='LogisticRegression(l1)',
                    parameters=log_reg_clf.best_estimator_,
                    majority_class_f1=f1_majority(y_test, y_pred),
                    minority_class_f1=f1_minority(y_test, y_pred),
                    lr_f1_score=lr_f1_score))

        results_file.write('\n')

        lr_result = {
            "file_name": str(bag_name + '_' + str(x) + '_bag'),
            "sample_size": main.n_samples,
            "algorithm": 'LogisticRegression',
            "parameters": log_reg_clf.best_estimator_,
            "majority_class_f1": f1_majority(y_test, y_pred),
            "minority_class_f1": f1_minority(y_test, y_pred),
            "lr_f1_score": lr_f1_score
        }
        tot_lr_result.append(lr_result)

    results_file.close()
    os.chdir(curr_pwd)
    return "LR classifier building done using 5 fold validation"
Esempio n. 7
0
def dt(sampled_data):

    bag_name = sampled_data[0]['dataset_name'].split('.')[0]
    n_bags = len(sampled_data)
    curr_pwd = str(os.getcwd())
    indices_pwd = curr_pwd + '/true_f1_scores/'
    os.chdir(indices_pwd)
    file_name = bag_name + '_dt_f1_weighted' + '.csv'
    results_file = open(file_name, 'a')
    tot_dt_result = []
    for x in range(len(sampled_data)):
        print("Processing '{file_name}'".format(
            file_name=bag_name + "_bag_" + str(sampled_data[x]['bag_number'])))
        bag_name_x = sampled_data[0]['dataset_name'].split('.')[0] + '_' + str(
            sampled_data[x]['bag_number'])
        dataset = sampled_data[x]
        data, target = dataset['data'], dataset['target']

        main = eda.eda()
        main.load_data(data, target)
        X_train, X_test, y_train, y_test = train_test_split(data,
                                                            target,
                                                            test_size=0.3,
                                                            stratify=target)

        # print("\n  bag  dist\n")
        # print(np.count_nonzero(target==0))
        # print(np.count_nonzero(target==1))

        # print("\n  train set dist\n")
        # print(np.count_nonzero(y_test==0))
        # print(np.count_nonzero(y_test==1))
        # print("\n prediction dist ")
        # print(np.count_nonzero(y_pred==0))
        # print(np.count_nonzero(y_pred==1))

        # f1_scores = []
        # final_parameters = []

        # rf_f1_score = f1_ratio(y_test,y_pred)
        # f1_scores.append(rf_f1_score)
        # final_parameters.append(Desc_tree_clf.get_params)

        # for i in range(1,4):
        # 	if(depth[i] <1 ):
        # 		depth[i]  = 1
        # 		print("\nINFO: fixing depth = 1 \n")
        f1_minority_score = 0
        while (f1_minority_score == 0):

            class_dist = np.bincount(y_test).tolist()
            majority_class_label = class_dist.index(max(class_dist))
            minority_class_label = class_dist.index(min(class_dist))

            weight_ratio = float(len(
                y_train[y_train == majority_class_label])) / float(
                    len(y_train[y_train == minority_class_label]))
            w_array = np.array([1] * y_train.shape[0])
            w_array[y_train == minority_class_label] = weight_ratio
            w_array[y_train == majority_class_label] = 1
            fit_parameters = {'sample_weight': w_array}

            Desc_tree_clf = DecisionTreeClassifier(class_weight='balanced')
            Desc_tree_clf.fit(X_train, y_train, **fit_parameters)

            depth = []
            max_tree_depth = Desc_tree_clf.tree_.max_depth
            # print("maxdepth:{}".format(max_tree_depth))
            depth.append(max_tree_depth)
            depth.append(int(max_tree_depth * 0.75))
            depth.append(int(max_tree_depth * 0.5))
            depth.append(int(max_tree_depth * 0.25))
            #removing zeros
            depth = [x for x in depth if x != 0]
            # print("depths:")
            # print(depth)
            parameters = {'max_depth': depth}

            y_pred = Desc_tree_clf.predict(X_test)
            Desc_tree_clf = GridSearchCV(
                DecisionTreeClassifier(class_weight='balanced'),
                parameters,
                cv=5,
                n_jobs=20,
                refit=True,
                scoring=custom_f1)
            Desc_tree_clf.fit(X_train, y_train, **fit_parameters)

            y_pred = Desc_tree_clf.predict(X_test)

            dt_f1_score = f1_ratio(y_test, y_pred)
            # print("majority_class_f1:{}".format(f1_majority(y_test,y_pred)))
            # print("minority_class_f1:{}".format(f1_minority(y_test,y_pred)))
            f1_minority_score = f1_minority(y_test, y_pred)
            if (f1_minority_score == 0):
                print("---------re-fitting------")
                X_train, X_test, y_train, y_test = train_test_split(
                    data, target, test_size=0.3, stratify=target)
        # f1_scores.append(rf_f1_score)
        # final_parameters.append(Desc_tree_clf.get_params)

        # print("f1_scores")
        # print(f1_scores)
        # index_high_score = f1_scores.index(max(f1_scores))

        # print("index_high_score:{}".format(index_high_score))

        results_file.write(
            "\"{file_name}\",{sample_size},\"{algorithm}\",\"{parameters}\",\"{majority_class_f1}\",\"{minority_class_f1}\",\"{dt_f1_score}\","
            .format(file_name=bag_name_x,
                    sample_size=main.n_samples,
                    algorithm='Desc_tree(Prunned)',
                    parameters=Desc_tree_clf.best_estimator_,
                    majority_class_f1=f1_majority(y_test, y_pred),
                    minority_class_f1=f1_minority(y_test, y_pred),
                    dt_f1_score=dt_f1_score))
        results_file.write('\n')

        dt_result = {
            "file_name": str(bag_name + '_' + str(x) + '_bag'),
            "sample_size": main.n_samples,
            "algorithm": 'Desc_tree(Prunned)',
            #"parameters":final_parameters[index_high_score],
            "parameters": Desc_tree_clf.best_estimator_,
            #"rf_f1_score":f1_scores[index_high_score],
            "majority_class_f1": f1_majority(y_test, y_pred),
            "minority_class_f1": f1_minority(y_test, y_pred),
            "dt_f1_score": dt_f1_score
        }
        tot_dt_result.append(dt_result)

    results_file.close()
    os.chdir(curr_pwd)
    return "DT classifier building done using 5 fold validation"
Esempio n. 8
0
def rf(sampled_data):

    bag_name = sampled_data[0]['dataset_name'].split('.')[0]
    n_bags = len(sampled_data)
    curr_pwd = str(os.getcwd())
    indices_pwd = curr_pwd + '/true_f1_scores/'
    os.chdir(indices_pwd)
    file_name = bag_name + '_rf_f1_weighted' + '.csv'
    results_file = open(file_name, 'a')
    tot_rf_result = []
    for x in range(len(sampled_data)):
        print("Processing '{file_name}'".format(
            file_name=bag_name + "_bag_" + str(sampled_data[x]['bag_number'])))
        bag_name_x = sampled_data[0]['dataset_name'].split('.')[0] + '_' + str(
            sampled_data[x]['bag_number'])
        dataset = sampled_data[x]
        data, target = dataset['data'], dataset['target']

        main = eda.eda()
        main.load_data(data, target)

        # print("\n  bag  dist\n")
        # print(np.count_nonzero(target==0))
        # print(np.count_nonzero(target==1))

        X_train, X_test, y_train, y_test = train_test_split(data,
                                                            target,
                                                            test_size=0.3,
                                                            stratify=target)
        class_dist = np.bincount(y_test).tolist()
        majority_class_label = class_dist.index(max(class_dist))
        minority_class_label = class_dist.index(min(class_dist))

        f1_minority_score = 0
        while (f1_minority_score == 0):

            random_forest_clf = RandomForestClassifier(
                class_weight='balanced_subsample')
            random_forest_clf.fit(X_train, y_train)

            y_pred = random_forest_clf.predict(X_test)
            # print("\n  train set dist\n")
            # print(np.count_nonzero(y_test==0))
            # print(np.count_nonzero(y_test==1))
            # print("\n prediction dist ")
            # print(np.count_nonzero(y_pred==0))
            # print(np.count_nonzero(y_pred==1))

            #f1_scores = []
            #final_parameters = []

            #rf_f1_score = f1_ratio(y_test,y_pred)
            #f1_scores.append(rf_f1_score)
            #final_parameters.append(random_forest_clf.get_params)

            depth = []
            max_tree_depth = max([
                estimator.tree_.max_depth
                for estimator in random_forest_clf.estimators_
            ])
            # print("maxdepth:{}".format(max_tree_depth))
            depth.append(max_tree_depth)
            depth.append(int(max_tree_depth * 0.75))
            depth.append(int(max_tree_depth * 0.5))
            depth.append(int(max_tree_depth * 0.25))
            #removing zeros
            depth = [x for x in depth if x != 0]
            # print("depths:")
            # print(depth)
            parameters = {'n_estimators': [1, 3, 7, 10], 'max_depth': depth}
            rf_f1_ratio_average = []
            f1_majority_score_average = []
            f1_minority_score_average = []
            for i in range(1, 10):
                random_forest_clf = GridSearchCV(
                    RandomForestClassifier(class_weight='balanced_subsample'),
                    parameters,
                    cv=5,
                    n_jobs=20,
                    refit=True,
                    scoring=custom_f1)
                random_forest_clf.fit(X_train, y_train)

                y_pred = random_forest_clf.predict(X_test)

                rf_f1_score = f1_ratio(y_test, y_pred)
                # f1_scores.append(rf_f1_score)
                # final_parameters.append(random_forest_clf.get_params)

                # print("f1_scores")
                # print(f1_scores)
                # index_high_score = f1_scores.index(max(f1_scores))

                # print("index_high_score:{}".format(index_high_score))
                # print("majority_class_f1:{}".format(f1_majority(y_test,y_pred)))
                # print("minority_class_f1:{}".format(f1_minority(y_test,y_pred)))
                rf_f1_ratio_average.append(rf_f1_score)
                f1_minority_score_average.append(f1_minority(y_test, y_pred))
                f1_majority_score_average.append(f1_majority(y_test, y_pred))

            # print("avg_majority_class_f1:{}".format(sum(f1_majority_score_average)/len(f1_majority_score_average)))
            # print("avg_minority_class_f1:{}".format(sum(f1_minority_score_average)/len(f1_minority_score_average)))
            f1_minority_score = sum(f1_minority_score_average) / len(
                f1_minority_score_average)
            if (f1_minority_score == 0):
                print("---------re-fitting------")
                X_train, X_test, y_train, y_test = train_test_split(
                    data, target, test_size=0.3, stratify=target)

        results_file.write(
            "\"{file_name}\",{sample_size},\"{algorithm}\",\"{parameters}\",\"{majority_class_f1}\",\"{minority_class_f1}\",\"{rf_f1_score}\","
            .format(file_name=bag_name_x,
                    sample_size=main.n_samples,
                    algorithm='Random_forest',
                    parameters=random_forest_clf.best_estimator_,
                    majority_class_f1=sum(f1_majority_score_average) /
                    len(f1_majority_score_average),
                    minority_class_f1=sum(f1_minority_score_average) /
                    len(f1_minority_score_average),
                    rf_f1_score=sum(rf_f1_ratio_average) /
                    len(rf_f1_ratio_average)))
        results_file.write('\n')

        rf_result = {
            "file_name":
            str(bag_name + '_' + str(x) + '_bag'),
            "sample_size":
            main.n_samples,
            "algorithm":
            'Random_forest',
            #"parameters":final_parameters[index_high_score],
            "parameters":
            random_forest_clf.best_estimator_,
            #"rf_f1_score":f1_scores[index_high_score],
            "majority_class_f1":
            sum(f1_majority_score_average) / len(f1_majority_score_average),
            "minority_class_f1":
            sum(f1_minority_score_average) / len(f1_minority_score_average),
            "rf_f1_score":
            rf_f1_score
        }
        tot_rf_result.append(rf_result)

    results_file.close()
    os.chdir(curr_pwd)
    return "RF classifier building done using 5 fold validation"