Q: ValueError: Expected 2D array, got 1D array instead. Python Function for Linear Regression I am not sure how to solve this error message. I hope someone can help. Thanks.
import numpy as np
from sklearn import linear_model
from sklearn.model_selection import train_test_split


def desired_marketing_expenditure(x_train_marketing_expenditure, y_train_units_sold, x_test_units_sold):

    X_train, X_test, y_train, y_test = train_test_split(x_train_marketing_expenditure, y_train_units_sold, test_size=0.4, random_state=101)
    lm = linear_model.LinearRegression()
    lm.fit(X_train,y_train)
    print(lm.intercept_)
    print(lm.coef_)

    #predictions = lm.predict(x_test_units_sold)

print(desired_marketing_expenditure([300000, 200000, 400000, 300000, 100000],[60000, 50000, 90000, 80000, 30000],60000))

OUT:ValueError: Expected 2D array, got 1D array instead:
array=[400000 200000 300000].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

A: try to reshape your X_train to (-1,1) as mentioned in error
import numpy as np
from numpy import array
from sklearn import linear_model
from sklearn.model_selection import train_test_split


def desired_marketing_expenditure(x_train_marketing_expenditure, y_train_units_sold, x_test_units_sold):

    X_train, X_test, y_train, y_test = train_test_split(x_train_marketing_expenditure, y_train_units_sold, test_size=0.4, random_state=101)
    lm = LinearRegression()
    X_train=array(X_train).reshape(-1,1)
    lm.fit(X_train,y_train)
    print(lm.intercept_)
    print(lm.coef_)

    #predictions = lm.predict(x_test_units_sold)

print(desired_marketing_expenditure([300000, 200000, 400000, 300000, 100000],[60000, 50000, 90000, 80000, 30000],60000))

Output:
13333.333333333343
[0.2]
None


A: Expects vectors. In numpy, vectors are vertical arrays.
[[1], [2]] instead of [1, 2].
import numpy as np
from sklearn import linear_model
from sklearn.model_selection import train_test_split


def desired_marketing_expenditure(x_train_marketing_expenditure, y_train_units_sold, x_test_units_sold):

    X_train, X_test, y_train, y_test = train_test_split(x_train_marketing_expenditure, y_train_units_sold, test_size=0.4, random_state=101)
    lm = linear_model.LinearRegression()
    lm.fit(X_train,y_train)
    print(lm.intercept_)
    print(lm.coef_)

    #predictions = lm.predict(x_test_units_sold)

print(desired_marketing_expenditure([[300000], [200000], [400000], [300000], [100000]],[[60000], [50000], [90000], [80000], [30000]], [[60000]]))


[13333.33333333]
[[0.2]]
None

Note: You can't transpose 1D arrays. np.array([1, 2, 3]).T is the same as np.array([1, 2, 3]) because transpose needs a second axis. You can add an extra axis and transpose as follow: np.array([1, 2, 3])[np.newaxis].T is the same as np.array([[1], [2], [3]]).
