1

I'm trying to build a Logistic Regression model which can predict new instance's class.
Here what I've done:

path = 'diabetes.csv'
df = pd.read_csv(path, header = None)
print "Classifying with Logistic Regression"
values = df.values
X = values[1:,0:8]
y = values[1:,8]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10, random_state=42)
model=LogisticRegression()
model.fit(X_train,y_train)

X_test = []
X_test.append(int(pregnancies_info))
X_test.append(int(glucose_info))
X_test.append(int(blood_press_info))
X_test.append(int(skin_thickness_info))
X_test.append(int(insulin_info))
X_test.append(float(BMI_info))
X_test.append(float(dpf_info))
X_test.append(int(age_info))
#X_test = np.array(X_test).reshape(-1, 1)
print X_test
y_pred=model.predict(X_test)
if y_pred == 0:
    Label(login_screen, text="Healthy").pack()
if y_pred == 1:
    Label(login_screen, text="Diabetes Metillus").pack()

pregnancies_entry.delete(0, END)
glucose_entry.delete(0, END)
blood_press_entry.delete(0, END)
skin_thickness_entry.delete(0, END)
insulin_entry.delete(0, END)
BMI_entry.delete(0, END)
dpf_entry.delete(0, END)
age_entry.delete(0, END)

But I got this error:

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.

If I uncomment this line X_test = np.array(X_test).reshape(-1, 1) this error appears:

File "/anaconda2/lib/python2.7/site-packages/sklearn/linear_model/base.py", line 305, in decision_function % (X.shape[1], n_features)) ValueError: X has 1 features per sample; expecting 8

Venkatachalam
  • 16,288
  • 9
  • 49
  • 77
user1234
  • 145
  • 5
  • 20
  • Possible duplicate of [Preprocessing in scikit learn - single sample - Depreciation warning](https://stackoverflow.com/questions/35082140/preprocessing-in-scikit-learn-single-sample-depreciation-warning) – Vivek Kumar Dec 26 '18 at 09:55

1 Answers1

2

You have to give it as

X_test = np.array(X_test).reshape(1, -1))

or you can directly do,

y_pred=model.predict([X_test])

The reason is predict function expects a 2D array with dimension (n_samples, n_features). When you have only record for which you need prediction, create a list of list and feed it! Hope it helps.

Venkatachalam
  • 16,288
  • 9
  • 49
  • 77