最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Issue with Input Array on Random Forest Model with Both Numerical and Categorical Features - Stack Overflow

programmeradmin6浏览0评论

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)
发布评论

评论列表(0)

  1. 暂无评论