I tried these line of codes on my computer and got a tuple (1,1) for coef_ and a tuple (1,) for intercept_
I don’t think this is possible:
In [1]: from sklearn.linear_model import LinearRegression
...:
...: model = LinearRegression()
...:
...: # data with two rows and a single column, i.e. two samples and a single
...: feature
...: data = [[1], [2]]
...: target = [1, 2]
...:
...: model.fit(data, target)
...:
...: print(f'coef_: {model.coef_}')
...: print(f'coef_ shape: {model.coef_.shape}')
...: print(f'intercept_: {model.intercept_}')
coef_: [1.]
coef_ shape: (1,)
intercept_: 4.440892098500626e-16
Are you sure that you did not use a LogisticRegression
instead of a LinearRegression
?
This is perhaps to confirm the false issue from Quiz M4. 02 and Quiz M4. 03 with this python code:
from sklearn.linear_model import LinearRegression
model = LinearRegression()
data = [[1], [2]]
target = [1, 2]
model.fit(data, target)
print(f'data.__class__: {data.__class__}')
print(f'target.__class__: {target.__class__}')
print(f'coef_: {model.coef_}')
print(f'coef_ shape: {model.coef_.shape}')
print(f'intercept_: {model.intercept_}')
print(f'intercept_.shape: {model.intercept_.shape}')
data = np.array([[1], [2]]).reshape([2,1])
target = np.array([1, 2]).reshape([2,1])
model.fit(data, target)
print(f'data.__class__: {data.__class__}')
print(f'target.__class__: {target.__class__}')
print(f'coef_: {model.coef_}')
print(f'coef_ shape: {model.coef_.shape}')
print(f'intercept_: {model.intercept_}')
print(f'intercept_.shape: {model.intercept_.shape}')
data = np.array([[1], [2]]).reshape([2,1])
target = np.array([1, 2]).reshape([2,1]).ravel()
model.fit(data, target)
print(f'data.__class__: {data.__class__}')
print(f'target.__class__: {target.__class__}')
print(f'coef_: {model.coef_}')
print(f'coef_ shape: {model.coef_.shape}')
print(f'intercept_: {model.intercept_}')
print(f'intercept_.shape: {model.intercept_.shape}')
data.__class__: <class 'list'>
target.__class__: <class 'list'>
coef_: [1.]
coef_ shape: (1,)
intercept_: 4.440892098500626e-16
intercept_.shape: ()
data.__class__: <class 'numpy.ndarray'>
target.__class__: <class 'numpy.ndarray'>
coef_: [[1.]]
coef_ shape: (1, 1)
intercept_: [4.4408921e-16]
intercept_.shape: (1,)
data.__class__: <class 'numpy.ndarray'>
target.__class__: <class 'numpy.ndarray'>
coef_: [1.]
coef_ shape: (1,)
intercept_: 4.440892098500626e-16
intercept_.shape: ()
Anyway, the definition for " y
a vector " in the python meaning seems clearly the list and not the usual vector (column or row) from algebra. So this may be read as " y
a python vector " and to known what is a python vector, perhaps.