admin管理员组文章数量:1402824
I am obtaining a ValueError regarding the input arrays and there dimension. I am trying to create a Random Forest Regression Model for price prediction using both numerical features and categorical features. I still have the error when adding a line of code to reshape.
Here is the code for the model:
cat_features = ['Manufacturer', 'Fuel type','Model']
num_features = ['Engine size', 'Year of manufacture', 'Mileage']
x_numerical = df[num_features]
x_cat = df[cat_features]
y = df['Price']
#scaling of the numerical data - using same scaler as previous for consistency
scale = StandardScaler()
scale.fit(x_numerical)
x_numerical_scaled = scale.transform(x_numerical)
#trasforming categorical data into numerical - one hot encoder as applying to multiple labels
from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder()
encoder.fit(x_cat)
#applying tranformation
x_cat_encoder = encoder.transform(x_cat)
#reshaping the data
#x_cat_endoder = x_cat_label.reshape(-1, 1)
#combining both categorical and numerical features
x = np.concatenate((x_numerical_scaled, x_cat_encoder), axis = 1)
#now splitting the data
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 42)
forest = RandomForestRegressor()
forest.fit(x_train, y_train)
pred_forest = forest.predict(x_test)
#evalutating the model
evaluate_model(y_test, pred_forest)
#creating the visualisation
plt.scatter(y_test, pred_forest)
plt.plot([0,160000], [0,160000], 'k-')
plt.ylabel('Actual price (GDP)')
plt.xlabel('Predicted Price (GDP)')
plt.suptitle('Actual Price Vs Predicted Price')
plt.title('Random Forest Regressor')
plt.show()
And this is the error I am receiving:
ValueError Traceback (most recent call last)
Cell In[35], line 25
19 x_cat_encoder = encoder.transform(x_cat)
21 #reshaping the data
22 #x_cat_label = x_cat_label.reshape(-1, 1)
23
24 #combining both categorical and numerical features
---> 25 x = np.concatenate((x_numerical_scaled, x_cat_encoder), axis = 1)
27 #now splitting the data
28 x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 42)
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 0 dimension(s)
本文标签:
版权声明:本文标题:python - Issue with Input Array on Random Forest Model with Both Numerical and Categorical Features - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744341308a2601491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论