Execute Python - 192 Error

YunJeong_Kang
YunJeong_Kang New Altair Community Member
edited November 5 in Community Q&A
I want to ues python cord in rapidminer.
So, I would like to conduct a test using the dataset in sklearn.
However, I confirmed that an error occurred in the code that loaded the dataset.
Is there anything I need to configure to use python's sklearn?

I will tell you the code I wrote and the error message.


-Erroe meggage - 
Execution of Python script failed

The execution of the python script failed.
Please check your Python script:KeyError: 192
(script, line11)

- Python Cord -
import matplotlib.pyplot as plt
import pandas as pd

from sklearn import datasets
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score


def rm_main():
house = datasets.fetch_california_housing()
X = house.data
y = house.target

X_train, X_test, y_train, y_test = train_test_split(house.data, house.target, test_size = 0.2)
estimator = RandomForestRegressor(n_estimators=100)

estimator.fit(X_train, y_train)

prediction = estimator.predict(X_test)

mse = mean_squared_error(y_test, prediction)
r_squared = r2_score(y_test, prediction)
print(f"MSE: {mse}")
print(f"R-squared: {r_squared}")

if __name__ == "__main__":
main()


line11 : house = datasets.fetch_california_housing()
Tagged:

Best Answer

  • Caperez
    Caperez Altair Community Member
    Answer ✓
    Hi @YunJeong_Kang,
    Please check that you have installed the sci-kit library in your Anaconda or Python environment configured in RM, this is probably the main reason. 

    Also be careful with indentation, the Rapidminer Python operator is very sensitive to this.

    Finally, I ran your script in my environment with scikit and it works. 

    See below for a simple example. 

    Best, 

    import pandas
    import matplotlib.pyplot as plt
    import pandas as pd

    from sklearn import datasets
    from sklearn.ensemble import RandomForestRegressor
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import mean_squared_error, r2_score


    def rm_main():
    house = datasets.fetch_california_housing()
    X = house.data
    y = house.target

    X_train, X_test, y_train, y_test = train_test_split(house.data, house.target, test_size = 0.2)
    estimator = RandomForestRegressor(n_estimators=100)
    estimator.fit(X_train, y_train)
    prediction = estimator.predict(X_test)
    mse = mean_squared_error(y_test, prediction)
    r_squared = r2_score(y_test, prediction)
    print(f"MSE: {mse}")
    print(f"R-squared: {r_squared}")
    df = pd.DataFrame(prediction)

    return(df)




Answers

  • Caperez
    Caperez Altair Community Member
    Answer ✓
    Hi @YunJeong_Kang,
    Please check that you have installed the sci-kit library in your Anaconda or Python environment configured in RM, this is probably the main reason. 

    Also be careful with indentation, the Rapidminer Python operator is very sensitive to this.

    Finally, I ran your script in my environment with scikit and it works. 

    See below for a simple example. 

    Best, 

    import pandas
    import matplotlib.pyplot as plt
    import pandas as pd

    from sklearn import datasets
    from sklearn.ensemble import RandomForestRegressor
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import mean_squared_error, r2_score


    def rm_main():
    house = datasets.fetch_california_housing()
    X = house.data
    y = house.target

    X_train, X_test, y_train, y_test = train_test_split(house.data, house.target, test_size = 0.2)
    estimator = RandomForestRegressor(n_estimators=100)
    estimator.fit(X_train, y_train)
    prediction = estimator.predict(X_test)
    mse = mean_squared_error(y_test, prediction)
    r_squared = r2_score(y_test, prediction)
    print(f"MSE: {mse}")
    print(f"R-squared: {r_squared}")
    df = pd.DataFrame(prediction)

    return(df)




  • YunJeong_Kang
    YunJeong_Kang New Altair Community Member
    진심 어린 답변에 감사드립니다. @ceaperez

    RM에 구성된 Python 환경에 sklearn 라이브러리가 설치되어 있는지 어떻게 확인합니까?

    내 환경에서는 작동하지 않아서 구성 문제인 것 같습니다.
    설정방법을 알려주시면 정말 감사하겠습니다.
  • YunJeong_Kang
    YunJeong_Kang New Altair Community Member
    Ah, now I understand what you mean. I'll try it. thank you