amueller / introduction_to_ml_with_python

Notebooks and code for the book "Introduction to Machine Learning with Python"
7.45k stars 4.57k forks source link

Solved problem in [66] ndarray DecisionTreeRegressor page 81. #160

Open RodrigoJoaquimAlmeida opened 2 years ago

RodrigoJoaquimAlmeida commented 2 years ago

I found many notebooks with same error, but no one gave me back a solution to this issue:

X_train = data_train.date[:, np.newaxis]

"Support for multi-dimensional indexing (e.g. obj[:, None]) is deprecated and will be removed in a future version."

So, after looking for some answers i found this:

instead using ndarray, use just an array and them reshape it from an 1D array to 2D array with array.reshape(-1, 1) and it will be done

from sklearn.tree import DecisionTreeRegressor

data_train = ram_prices[ram_prices.date < 2000] data_test = ram_prices[ram_prices.date >= 2000]

X_train = np.array(data_train.date) X_train = X_train.reshape(-1, 1) y_train = np.log(data_train.price)

tree = DecisionTreeRegressor().fit(X_train, y_train) linear_reg = LinearRegression().fit(X_train, y_train)

X_all = np.array(ram_prices.date) X_all = X_all.reshape(-1, 1)

pred_tree = tree.predict(X_all) pred_lr = linear_reg.predict(X_all)

price_tree = np.exp(pred_tree) price_lr = np.exp(pred_lr)