python 如何使用html将逗号分隔的值插入到numpy数组中进行预测

gg0vcinb  于 2024-01-05  发布在  Python
关注(0)|答案(1)|浏览(117)

我试

  1. <td align="left"><input type="text" name="n1"></td>

字符串
接受输入

  1. 17.99,10.38,122.8,1001,0.1184,0.2776,0.3001,0.1471,0.2419,0.07871,1.095,0.9053,8.589,153.4,0.006399,0.04904,0.05373,0.01587,0.03003,0.006193,25.38,17.33,184.6,2019,0.1622,0.6656,0.7119,0.2654,0.4601,0.1189


我把它放入view.py中的numpy数组。

  1. np.array((request.GET['n1']))


但我收到以下错误消息。

  1. ValueError at /prediction/output
  2. Expected 2D array, got 1D array instead:
  3. array=['17.99,10.38,122.8,1001,0.1184,0.2776,0.3001,0.1471,0.2419,0.07871,1.095,0.9053,8.589,153.4,0.006399,0.04904,0.05373,0.01587,0.03003,0.006193,25.38,17.33,184.6,2019,0.1622,0.6656,0.7119,0.2654,0.4601,0.1189'].
  4. 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.


view.pyv1 = np.array((request.GET['n1']))在第8行)

  1. def output(request):
  2. dff = pd.read_csv(r'C:\Users\Downloads\data.csv')
  3. y = dff['diagnosis'].values
  4. x = dff.drop('diagnosis', axis=1).values
  5. x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.40)
  6. model = LogisticRegression()
  7. model.fit(x_train, y_train)
  8. v1 = np.array((request.GET['n1']))
  9. pred = model.predict([v1])
  10. pred1 = ""
  11. if pred==[1]:
  12. pred1 = "positive"
  13. else:
  14. pred1 = "negative"
  15. return render(request, 'prediction.html', {"predictResult":**pred1**})


prediction.html(第6行是获取输入)

  1. <div>
  2. <form action="output">
  3. <table >
  4. <tr>
  5. <td align="right">Pregnancies</td>
  6. <td align="left"><input type="text" name="n1"></td>
  7. </tr>
  8. </table>
  9. <input type="submit">
  10. </form>
  11. Result:{{ predictResult }}
  12. </div>

4szc88ey

4szc88ey1#

错误消息显示v1是一个只有一个条目的numpy数组。该条目是一个带有逗号分隔列表的字符串。您需要将其拆分为各个数字。您可能希望这样做:

  1. v1 = np.array([float(num) for num in request.GET['n1'].split(",")])

字符串
该消息还提到了数组的形状,因此您需要根据您的情况重新塑造它(只需按照错误消息所说的那样)。

相关问题