如何在python中创建一系列float值?

时间:2019-10-18 12:26:29

标签: python numpy

我试图在x + y + z等于1的条件下互相进行多个循环,以进入最后一个循环。

我使用了以下内容:

import numpy as np
for gbrCount in np.arange(0, 1.0, 0.1):
    for xgbCount in np.arange(0, 1.0, 0.1):
        for regCount in np.arange(0, 1.0, 0.1):
            #check if sum is 1
            if int(gbrCount+xgbCount+regCount) == 1:

                y_p = (xgbCount*xgb.predict(testset)+ gbrCount*gbr.predict(testset)+regCount*regressor.predict(testset))
                testset['SalePrice']=np.expm1(y_p)
                y_train_p = xgb.predict(dataset)
                y_train_p = np.expm1(y_train_p)
                rmse.append(np.sqrt(mean_squared_error(y, y_train_p)))
                rmse.append(xgbCount)
                rmse.append(gbrCount)
                rmse.append(regCount)

但是,即使总和大于1,也会进入循环。 (xgb,reg和gbr)的某些值类似于0.70000001。

因此,我尝试使用linspace,但它不适用于float。所以我尝试了范围:

for gbrCount in range(0, 1):
    gbrCount += 0.1
    for xgbCount in range(0, 1):
        xgbCount += 0.1
        for regCount in range(0, 1):
            regCount += 0.1
            if int(gbrCount+xgbCount+regCount)==1:
                #y_p = (xgbCount*xgb.predict(testset)+ gbrCount*gbr.predict(testset)+regCount*regressor.predict(testset))
                #testset['SalePrice']=np.expm1(y_p)
                y_train_p = (xgbCount*xgb.predict(dataset)+ gbrCount*gbr.predict(dataset)+regCount*regressor.predict(dataset))
                y_train_p = np.expm1(y_train_p)
#                print(np.sqrt(mean_squared_error(y, y_train_p)))
#                print(xgbCount)
#                print(gbrCount)
#                print(regCount)
                print(xgbCount,  gbrCount, regCount, np.sqrt(mean_squared_error(y, y_train_p)))

但是它在控制台上完全没有错误。

1 个答案:

答案 0 :(得分:2)

那呢? (我认为没有必要进行最后四舍五入,只是为了安全起见,我把它留在那里。)

    import numpy as np
    for _gbrCount in np.arange(0, 1.0, 0.1):
        for _xgbCount in np.arange(0, 1.0, 0.1):
            gbrCount = np.round(_gbrCounr, decimals=1)
            xgbCount = np.round(_cgbCount, decimals=1)
            regCount = np.round(1 - gbrCount - xgbCount, decimals=1)
            y_p = (xgbCount*xgb.predict(testset)+ gbrCount*gbr.predict(testset)+regCount*regressor.predict(testset))
            testset['SalePrice']=np.expm1(y_p)
            y_train_p = xgb.predict(dataset)
            y_train_p = np.expm1(y_train_p)
            rmse.append(np.sqrt(mean_squared_error(y, y_train_p)))
            rmse.append(xgbCount)
            rmse.append(gbrCount)
            rmse.append(regCount)