示例#1
0
def PLR(training_data,test_data,K_FOLD_SIZE,check):
	SSR_list = []
	SSE_list = []
	SST_list = []
	# ro=0
	RMSE=[]
	MSR_list = []
	MSE_list = []
	R_sqaured_list = []
	F_list = []
	Adjusted_R_squared_list = []
	SSR_list1 = []
	SSE_list1 = []
	SST_list1 = []
	# ro=0
	MSR_list1 = []
	MSE_list1 = []
	R_sqaured_list1 = []
	F_list1 = []
	Adjusted_R_squared_list1 = []
	LSE_list = []
	res_list_test=[]
	predicted_test=[]
	res_list_train=[]
	predicted_train=[]
	y_actual_train=[]
	y_actual_test=[]
	d=[]
	R1=[]

	for i in range(0,K_FOLD_SIZE):
		LSE, Y_actual_, data = LSE_Calculation(training_data[i],check)
		LSE_list.append(LSE)
		row, column = np.shape(data)
		Y_pre = np.matmul(data, LSE)

		if (check==1):
			Y_pre = np.power(Y_pre, 10)
		res = prediction(Y_pre, Y_actual_)
		res_list_train.append(res)
		predicted_train.append(res)
		y_actual_train.append(Y_actual_)
		# print(Y_pre)
		# print(Y_pre)
		# print(Y_pre)
		# print(res)
		# print(predict(res, Y_actual_))
		# mp.scatter(Y_pre, res, marker='o', c='b')
		# mp.title('Predicted VS residual')
		# mp.xlabel('Y_pre')
		# mp.ylabel('res')
		# mp.show()
		# mp.scatter(Y_pre, Y_actual, marker='o', c='b')
		# mp.title('Predicted VS actual')
		# mp.xlabel('Y_pre')
		# mp.ylabel('Y_actual')
		# mp.show()

		SSE = np.matmul(res, np.transpose(res))
		# print(SSE)#sum of squares of residual
		SSE_list1.append(SSE)
		# print(SSE)
		SSR = SSR_Calc(Y_actual_, Y_pre, row)

		SSR_list1.append(SSR)

		# temp_SSM=np.subtract(Y_pre,Y_avg)
		# SSM=abs(temp_SSM)*abs(temp_SSM)
		# print(SSM)
		SST = SSR + SSE
		SST_list1.append(SST)

		# ro=ro+row
		R_sqaured = SSR / SST
		# Adjusted_R_squared_list.append(Adjusted_R_squared)
		R_sqaured_list1.append(R_sqaured)

		# print(R_sqaured)
		MSE = SSE / ((row - column - 1))
		MSE_list1.append(MSE)
		R1.append(math.sqrt(MSE))
		MSR = SSR / (column)
		MSR_list1.append(MSR)
		MST = SST / float(row - 1)
		Adjusted_R_squared = 1 - MSE / MST
		Adjusted_R_squared_list1.append(Adjusted_R_squared)
		# MST_list.append(MST)
		F = MSR / MSE
		F_list1.append(F)
		# print("For Training Data")


		# data = teat_data[i].drop(['log_commercial-price'], axis=1)
		# print(data)
		data = test_data[i].drop(['Commercial-rate'], axis=1)
		if check == 1:
			data = data.drop(['log_commercial_rate'], axis=1)
		row, column = data.shape
		# print(data)
		# print(row, column)
		Y_actual_ = test_data[i]['Commercial-rate']
		# print(Y_actual_)
		Y_actual_ = Y_actual_.values
		weight = np.identity(row)
		# Y_actual = x[i]['log_commercial-price']
		# Y_actual = Y_actual.values
		# print(Y_actual)
		# norm, data = Normalization(data)
		# print(Y_actual)
		# print(data)
		Y_pre = np.matmul(data, LSE)

		if (check == 1):
			Y_pre = np.power(Y_pre, 10)
		res = prediction(Y_pre, Y_actual_)
		res_list_test.append(res)
		d.append(data)
		predicted_test.append(Y_pre)
		y_actual_test.append(Y_actual_)
		# Y_pre = np.power(Y_pre, 10)
		# print(Y_pre)
		# print(Y_pre)
		# print(Y_pre)
		# print(res)
		# print(predict(res, Y_actual_))
		# mp.scatter(Y_pre, res, marker='o', c='b')
		# mp.title('Predicted VS residual')
		# mp.xlabel('Y_pre')
		# mp.ylabel('res')
		# mp.show()
		# mp.scatter(Y_pre, Y_actual, marker='o', c='b')
		# mp.title('Predicted VS actual')
		# mp.xlabel('Y_pre')
		# mp.ylabel('Y_actual')
		# mp.show()

		SSE = np.matmul(res, np.transpose(res))
		# print(SSE)#sum of squares of residual
		SSE_list.append(SSE)
		# print(SSE)
		SSR = SSR_Calc(Y_actual_, Y_pre, row)

		SSR_list.append(SSR)

		# temp_SSM=np.subtract(Y_pre,Y_avg)
		# SSM=abs(temp_SSM)*abs(temp_SSM)
		# print(SSM)
		SST = SSR + SSE
		SST_list.append(SST)

		# ro=ro+row
		R_sqaured = SSR / SST
		# Adjusted_R_squared_list.append(Adjusted_R_squared)
		R_sqaured_list.append(R_sqaured)

		# print(R_sqaured)
		MSE = SSE / ((row - column - 1))
		MSE_list.append(MSE)
		RMSE.append(math.sqrt(MSE))

		MSR = SSR / (column)
		MSR_list.append(MSR)
		MST = SST / float(row - 1)
		Adjusted_R_squared = 1 - MSE / MST
		Adjusted_R_squared_list.append(Adjusted_R_squared)
		# MST_list.append(MST)
		F = MSR / MSE
		F_list.append(F)

	print("For Training Data")
	k=ANOVA_CALC(SSE_list1, SST_list1, SSR_list1, R_sqaured_list1,R1, MSE_list1, MSR_list1, F_list1,
			   Adjusted_R_squared_list1,K_FOLD_SIZE)

	print("For Test Data")
	k_test=ANOVA_CALC(SSE_list, SST_list, SSR_list, R_sqaured_list, MSE_list, RMSE,MSR_list, F_list, Adjusted_R_squared_list,K_FOLD_SIZE)
	return LSE_list, column,k,k_test,d,res_list_test,res_list_train,predicted_test,predicted_train,y_actual_test,y_actual_train,MSE_list
示例#2
0
def speech_recognize():
    '''So far, this code is able to store a file into file name.
    audio_file has the name of the file, e.g. ./audio.wav, which is saved locally by urlretrieve
    '''
    args = request.args
    uri = json.loads(args['uri'])['uri']
    # return uri
    model = args['model']
    mix = args['mix']
    audio_file = url_to_audio_file(uri)
    #return audio_file
    '''
    currently we only support tone
    '''
    if model == 'Tone':
        y = audio_file_to_array(audio_file, 8000)
        #first add a random tone
        frequency = random.randint(500, 1200)
        if 'mix' in args:
            y_tone = generate_tone(8000,
                                   len(y) / 8000, np.array([frequency]),
                                   np.array([0.06]))[0]
            y = np.add(y, y_tone)
        #handle tone: this will return the URL
        #takes 2 sec clips and 8000 sr
        ys = audio_array_to_duration_segments(y, 8000, 2.0)
        #now get the results for each of the ys and, get the individual sound arrays, concatenate them, and convert to a .wav
        #file locally
        total_model_output = []
        for y in ys:
            model_output = discard_tone(np.array(y), 8000)
            total_model_output.extend(model_output.tolist())
        denoised_data = np.array(total_model_output)
        print(denoised_data.shape)
        file_name = ''.join(
            random.choices(string.ascii_uppercase + string.digits, k=8))
        file_name = file_name + '.wav'
        path_to_file = './audioFiles/' + file_name
        # librosa.output.write_wav(path_to_file, denoised_data[0], 8000)
        write(path_to_file, 8000, denoised_data)
        # return path_to_file
        return send_file(path_to_file,
                         mimetype="audio/wav",
                         as_attachment=True,
                         attachment_filename=file_name)
    elif model == "Dog":
        weights_path = './dog_model'
        model = unet()
        # audio_input_prediction = [audio_file]
        sample_rate = 8000
        min_duration = 1
        frame_length = 8064
        hop_length_frame = 8064
        n_fft = 256
        hop_length_fft = 63
        #we need to split the arbitrary length audio file into

        data_hold = audio_file_to_array(audio_file, 8000)
        sr = 8000
        # return str(len(data_hold))
        broken = audio_array_to_duration_segments(data_hold, 8000, 1.0)
        one_sec_files = []
        for i in broken:
            t = ''.join(
                random.choices(string.ascii_uppercase + string.digits, k=6))[1]
            file_name = './audioFiles/' + t + '.wav'
            nparray = np.array(i)
            print(type(nparray))
            write(file_name, sr, nparray)
            one_sec_files.append(file_name)
        print(one_sec_files)

        final_sound_list = []
        for one_sec_file in one_sec_files:
            denoised_data = prediction(weights_path, model, [one_sec_file],
                                       sample_rate, min_duration, frame_length,
                                       hop_length_frame, n_fft, hop_length_fft)
            final_sound_list.extend(denoised_data.tolist())

        file_name = ''.join(
            random.choices(string.ascii_uppercase + string.digits, k=8))[1]
        file_name = file_name + '.wav'
        path_to_file = './audioFiles/' + file_name
        librosa.output.write_wav(path_to_file, 8000,
                                 numpy.array(final_sound_list))
        return send_file(path_to_file,
                         mimetype="audio/wav",
                         as_attachment=True,
                         attachment_filename=file_name)
def compare(company1, company2):
    dicty = {}
    # company1
    cp1 = dt.DataReader(company1, data_source="yahoo", start="2010-1-1")
    sr = simple_return(company1)
    lr = log_return(company1)
    mcarlo = monte_forcast(company1)
    beta_value = beta(company1)
    prd = prediction(company1)
    plt.figure(8)
    plt.clf()
    plt.title(f"Growth Of {company1.split('.')[0]}")
    plt.plot(cp1["Close"])
    fig = plt.gcf()
    buf = io.BytesIO()
    fig.savefig(buf, format="png")
    buf.seek(0)
    string = base64.b64encode(buf.read())
    growth_plot_cp1 = urllib.parse.quote(string)
    dicty["Growth_Plot_Cp1"] = growth_plot_cp1
    dicty["Simple_return_Cp1"] = round(sr["Overall_Mean"], 3)
    dicty["Simple_return_plot_Cp1"] = sr["Plot"]
    dicty["Log_return_Cp1"] = round(lr["Overall_Mean"], 3)
    dicty["Log_return_Plot_Cp1"] = lr["Plot"]
    dicty["Var_Cp1"] = mcarlo["Variance_return"]
    dicty["Std_Cp1"] = mcarlo["Std_deviation"]
    dicty["Drift_Cp1"] = mcarlo["Drift"]
    dicty["Norm_Cp1"] = mcarlo["Norm"]
    dicty["Future_Iteration_Cp1"] = mcarlo['plot']
    dicty["Beta_Cp1"] = beta_value['Beta']
    dicty["Cov_mrkt_wrt_stk_Cp1"] = beta_value['Cov Market wrt Stock']
    dicty["Variance_Market_Cp1"] = beta_value["Var Market"]
    dicty["Volatility_Cp1"] = beta_value["Volatility_of_stock"]
    dicty["Future_Pred_Cp1"] = prd["Plot2"]
    # company2
    cp2 = dt.DataReader(company2, data_source="yahoo", start="2010-1-1")
    sr = simple_return(company2)
    lr = log_return(company2)
    mcarlo = monte_forcast(company2)
    beta_value = beta(company2)
    prd = prediction(company2)
    plt.figure(8)
    plt.clf()
    plt.title(f"Growth Of {company2.split('.')[0]}")
    plt.plot(cp2["Close"])
    fig = plt.gcf()
    buf = io.BytesIO()
    fig.savefig(buf, format="png")
    buf.seek(0)
    string = base64.b64encode(buf.read())
    growth_plot_cp2 = urllib.parse.quote(string)
    dicty["Growth_Plot_Cp2"] = growth_plot_cp2
    dicty["Simple_return_Cp2"] = round(sr["Overall_Mean"], 3)
    dicty["Simple_return_plot_Cp2"] = sr["Plot"]
    dicty["Log_return_Cp2"] = round(lr["Overall_Mean"], 3)
    dicty["Log_return_Plot_Cp2"] = lr["Plot"]
    dicty["Var_Cp2"] = mcarlo["Variance_return"]
    dicty["Std_Cp2"] = mcarlo["Std_deviation"]
    dicty["Drift_Cp2"] = mcarlo["Drift"]
    dicty["Norm_Cp2"] = mcarlo["Norm"]
    dicty["Future_Iteration_Cp2"] = mcarlo['plot']
    dicty["Beta_Cp2"] = beta_value['Beta']
    dicty["Cov_mrkt_wrt_stk_Cp2"] = beta_value['Cov Market wrt Stock']
    dicty["Variance_Market_Cp2"] = beta_value["Var Market"]
    dicty["Volatility_Cp2"] = beta_value["Volatility_of_stock"]
    dicty["Future_Pred_Cp2"] = prd["Plot2"]