Home > Back-end >  Why does it give me this error when I try to predict my own result?
Why does it give me this error when I try to predict my own result?

Time:04-09

I'm using linear regression to predict the pregnancy risk using some factors, but when after training and testing and I try to use it on my own data it gives me this error:

ValueError: Expected 2D array, got 1D array instead:
array=[ 89. 150.  66.   9.  90.  70.].
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.

The code is below:

features = df[["Age", "SystolicBP", "DiastolicBP", "BS", "BodyTemp", "HeartRate"]]
riskLevel = df["RiskLevel"]
x_train, x_test, y_train, y_test = train_test_split(features, riskLevel, test_size=0.2, random_state=1)

model = LinearRegression()
model.fit(x_train, y_train)

y_predict = model.predict(x_test)

# predict the risk of your own data
my_own_data = np.array([89, 150, 66, 9.0, 90, 70])
my_own_data.reshape(-1, 1)
model.predict(my_own_data)

CodePudding user response:

Changing my_own_data = np.array([89, 150, 66, 9.0, 90, 70]) to my_own_data = np.array([[89, 150, 66, 9.0, 90, 70]]) solves the error, but I don't know if the data you are getting is valid.

Try this solution, maybe you get more info out of it: sklearn LinearRegression.Predict() issue

  • Related