亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

導入 CSV,為邏輯回歸重塑變量數組

導入 CSV,為邏輯回歸重塑變量數組

炎炎設計 2022-10-06 18:41:43
我希望每個人都在 COVID-19 大流行中保持安全。我是 Python 新手,有一個關于將數據從 CSV 導入 Python 以進行簡單邏輯回歸分析的快速問題,其中因變量是二元的,自變量是連續的。我導入了一個 CSV 文件,然后希望使用一個變量(Active)作為自變量,另一個變量(Smoke)作為響應變量。我能夠將 CSV 文件加載到 Python 中,但每次我嘗試生成邏輯回歸模型來預測來自運動的煙霧時,我都會收到一個錯誤,即運動必須重新整形為一列(二維),因為它目前是一列維度。import matplotlib.pyplot as pltimport numpy as npimport pandas as pdfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import classification_report, confusion_matrixdata = pd.read_csv('Pulse.csv') # Read the data from the CSV filex = data['Active'] # Load the values from Exercise into the independent variablex = np.array.reshape(-1,1)y = data['Smoke'] # The dependent variable is set as Smoke我不斷收到以下錯誤消息:83. 102. 102. 106. 79. 80. 79. 110. 144. 80. 97. 60. 80. 108. 107. 51. 68. 80. 80. 60. 64. 87. 110. 110. 82. 154. 139. 86. 95. 112. 120. 79. 64. 84. 65. 60. 79. 79. 70. 75. 107. 78. 74. 80. 121. 120. 96. 75. 106. 88. 91. 98. 63. 95. 85. 83. 92. 81. 89. 103. 110. 78. 122. 122. 71. 65. 92. 93. 88. 90. 56. 95. 83. 97. 105. 82. 102. 87. 81.]。如果您的數據具有單個特征,則使用 array.reshape(-1, 1) 重塑您的數據,如果它包含單個樣本,則使用 array.reshape(1, -1) 。89. 103. 110. 78. 122. 122. 71. 65. 92. 93. 88. 90. 56. 95. 83. 97. 105. 82. 102. 87. 81.]。如果您的數據具有單個特征,則使用 array.reshape(-1, 1) 重塑您的數據,如果它包含單個樣本,則使用 array.reshape(1, -1) 。89. 103. 110. 78. 122. 122. 71. 65. 92. 93. 88. 90. 56. 95. 83. 97. 105. 82. 102. 87. 81.]。如果您的數據具有單個特征,則使用 array.reshape(-1, 1) 重塑您的數據,如果它包含單個樣本,則使用 array.reshape(1, -1) 。以下是包含錯誤的完整更新代碼(2020 年 4 月 12 日): *我無法將錯誤日志輸入到此文檔中,因此我已將其復制并粘貼到此公共 Google 文檔中:https://docs.google。 com/document/d/1vtrj6Znv54FJ4Zvv211TQvvCN6Ac5LDaOfvHicQn0nU/edit?usp=sharing此外,這里是 CSV 文件: https ://drive.google.com/file/d/1g_-vPNklxRn_3nlNPsR-IOflLfXSzFb1/view?usp=sharing
查看完整描述

2 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

下面的代碼應該可以工作:


import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import classification_report, confusion_matrix

data = pd.read_csv('Pulse.csv')

x = pd.DataFrame(data['Smoke'])

y = data['Smoke']

lr = LogisticRegression()

lr.fit(x,y)

p_pred = lr.predict_proba(x)

y_pred = lr.predict(x)

score_ = lr.score(x,y)

conf_m = confusion_matrix(y,y_pred)

report = classification_report(y,y_pred)


print(score_)

0.8836206896551724


print(conf_m)

[[204   2]

 [ 25   1]]


查看完整回答
反對 回復 2022-10-06
?
四季花海

TA貢獻1811條經驗 獲得超5個贊

嘗試這個:


import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import classification_report, confusion_matrix


data = pd.read_csv('Pulse.csv') # Read the data from the CSV file

x = data['Active'] # Load the values from Exercise into the independent variable

y = data['Smoke'] # The dependent variable is set as Smoke


lr = LogisticRegression().fit(x.values.reshape(-1,1), y)


查看完整回答
反對 回復 2022-10-06
  • 2 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號