Discussion:
[Scikit-learn-general] Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19
Sasha Kacanski
2016-02-06 16:52:24 UTC
Permalink
Hi,
I am new to scikit...
Start playing with leaner regression and
Ran into this issue...
Now even after I reshaped X I still get warning ...

I am not sure if I should let it be or I am doing something fundamentally
wrong

Example:

X = np.array([10, 20, 30, 60, 108])
Y = np.array([11, 23, 43, 170.5, 934.6])

model = LinearRegression()
x = X.reshape(-1,1)
model.fit(x,y)
print("Predict for number 12 {}".format(model.predict([12])[0]))
--
Aleksandar Kacanski
Sebastian Raschka
2016-02-06 19:28:02 UTC
Permalink
Hi, Sasha,
So based on your Y array, it seems that you have 6 training samples. Now, in scikit-learn, each sample is represented as a row in the X array (and the columns are the “features” — you have 1 feature here, right?).

Now, if you do
X = X.reshape(-1, 1)
X.shape
your array will have the shape (1, 5), but what you want is (5, 1). Thus, the solution is to do
X = X.reshape(-1, 1)
The complete code would be then

import numpy as np
X = np.array([10, 20, 30, 60, 108])
y = np.array([11, 23, 43, 170.5, 934.6])
X = X.reshape(-1, 1)
model.fit(X, y)

There’s also a problem in your predict call since you only have 6 samples, you can’t have a “12” in there, only values from 0, 5. E.g.,

print("Predict for number 6 {}".format(model.predict([5])))
Predict for number 6 [-146.58922645]

Best,
Sebastian
X = np.array([10, 20, 30, 60, 108])
Y = np.array([11, 23, 43, 170.5, 934.6])
model = LinearRegression()
x = X.reshape(-1,1)
model.fit(x,y)
print("Predict for number 12 {}".format(model.predict([12])[0]))
Sasha Kacanski
2016-02-09 23:51:02 UTC
Permalink
Sebastian,
Tanks so much, perfectly explained and proof that I missed completely on
understanding how to do this...
Lot's of learning ahead ...
Thanks much again,
Post by Sebastian Raschka
Hi, Sasha,
So based on your Y array, it seems that you have 6 training samples. Now,
in scikit-learn, each sample is represented as a row in the X array (and
the columns are the “features” — you have 1 feature here, right?).
Now, if you do
X = X.reshape(-1, 1)
X.shape
your array will have the shape (1, 5), but what you want is (5, 1). Thus,
the solution is to do
X = X.reshape(-1, 1)
The complete code would be then
import numpy as np
X = np.array([10, 20, 30, 60, 108])
y = np.array([11, 23, 43, 170.5, 934.6])
X = X.reshape(-1, 1)
model.fit(X, y)
There’s also a problem in your predict call since you only have 6 samples,
you can’t have a “12” in there, only values from 0, 5. E.g.,
print("Predict for number 6 {}".format(model.predict([5])))
Predict for number 6 [-146.58922645]
Best,
Sebastian
X = np.array([10, 20, 30, 60, 108])
Y = np.array([11, 23, 43, 170.5, 934.6])
model = LinearRegression()
x = X.reshape(-1,1)
model.fit(x,y)
print("Predict for number 12 {}".format(model.predict([12])[0]))
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Scikit-learn-general mailing list
https://lists.sourceforge.net/lists/listinfo/scikit-learn-general
--
Aleksandar Kacanski
Loading...