简体   繁体   English

如何使用sci-kit Learn通过Logistic回归预测单个实例?

[英]How to predict single instance with Logistic Regression using sci-kit learn?

I'm trying to build a Logistic Regression model which can predict new instance's class. 我正在尝试构建可以预测新实例的类的Logistic回归模型。
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. 如果数据具有单个功能,则使用array.reshape(-1,1)重整数据;如果包含单个样本,则使用array.reshape(1,-1)重整数据。

If I uncomment this line X_test = np.array(X_test).reshape(-1, 1) this error appears: 如果我取消注释此行X_test = np.array(X_test).reshape(-1, 1)则会出现此错误:

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; 文件“ /anaconda2/lib/python2.7/site-packages/sklearn/linear_model/base.py”,第305行,位于Decision_function%(X.shape [1],n_features)中)ValueError:每个样本X具有1个特征; expecting 8 期待8

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) . 原因是predict函数期望二维数组的尺寸为(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. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM