Random Forest Classifier ValueError : 입력에 NaN, 무한대 또는 dtype ( 'float32')에 비해 너무 큰 값이 있습니다.

패트리샤 파 둘라 로페스

RandomForest메서드를 데이터 세트 에 적용하려고 하는데이 오류가 발생합니다.

ValueError: Input contains NaN, infinity or a value too large for dtype ('float32')

누군가가 코드가 작동하도록 함수에서 수정할 수있는 것을 말해 줄 수 있습니까?

def ranks_RF(x_train, y_train, features_train, RESULT_PATH='Results'):
    """Get ranks from Random Forest"""

    print("\nMétodo_Random_Forest")

    random_forest = RandomForestRegressor(n_estimators=10)
    np.nan_to_num(x_train)
    np.nan_to_num(y_train)
    random_forest.fit(x_train, y_train)

    # Get rank by doing two times a sort.
    imp_array = np.array(random_forest.feature_importances_)
    imp_order = imp_array.argsort()
    ranks = imp_order.argsort()

    # Plot Random Forest
    imp = pd.Series(random_forest.feature_importances_, index=x_train.columns)
    imp = imp.sort_values()

    imp.plot(kind="barh")
    plt.xlabel("Importance")
    plt.ylabel("Features")
    plt.title("Feature importance using Random Forest")
    # plt.show()
    plt.savefig(RESULT_PATH + '/ranks_RF.png', bbox_inches='tight')

    return ranks
StupidWolf

nan을 교체 할 때 값을 덮어 쓰지 않았으므로 오류가 발생합니다.

예제 데이터 세트를 시도합니다.

import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor

from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(data= iris['data'],
                     columns= iris['feature_names'] )
df['target'] = iris['target']
# insert some NAs
df = df.mask(np.random.random(df.shape) < .1)

우리는 당신과 같은 기능을 가지고 있습니다. 그것은 완전히 다른 질문이기 때문에 플로팅 부분을 제거했습니다.

def ranks_RF(x_train, y_train):

    var_names = x_train.columns
    random_forest = RandomForestRegressor(n_estimators=10)
# here you have to reassign back the values
    x_train = np.nan_to_num(x_train)
    y_train = np.nan_to_num(y_train)
    random_forest.fit(x_train, y_train)

    res = pd.DataFrame({
    "features":var_names,
    "importance":random_forest.feature_importances_,
    })
    res = res.sort_values(['importance'],ascending=False)
    res['rank'] = np.arange(len(res))+1

    return res

우리는 그것을 실행합니다 :

ranks_RF(df.iloc[:,0:4],df['target'])

    features    importance  rank
3   petal width (cm)    0.601734    1
2   petal length (cm)   0.191613    2
0   sepal length (cm)   0.132212    3
1   sepal width (cm)    0.074442    

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관