我正在尝试建立一个逻辑回归模型,它可以预测新实例的类
下面是我所做的:
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)
但我有一个错误:
如果数据具有单个特征,则使用array.reshape(-1,1)重塑数据;如果数据包含单个样本,则使用array.reshape(1,-1)重塑数据。
如果我取消注释这行X\u test=np。数组(X_测试)。重塑(-1,1)
出现以下错误:
文件“/anaconda2/lib/python2.7/site packages/sklearn/linear_model/base.py”,第305行,在decision_函数(X.shape[1],n_features))值中错误:X每个样本有1个特征;8岁
你必须给它作为
X_test = np.array(X_test).reshape(1, -1))
或者你可以直接做,
y_pred=model.predict([X_test])
原因是predict
函数需要一个具有维度(n个样本,n个特征)的2D数组。当您只有需要预测的记录时,创建一个列表列表并将其馈送!希望能有帮助。