簡體   English   中英

如何在sklearn中擬合數據

[英]How to fit data in sklearn

我想編寫讀取 csv 文件的代碼,然后使用線性回歸進行預測。
CSV文件是這樣的:

數學 物理
17 15
16 12
18 19

我試試這段代碼:

import pandas as pd
from sklearn.linear_model import LinearRegression

score_file = pd.read_csv('scores.csv')
math_score = score_file['math']
physic_score = score_file['physics']
cls = LinearRegression().fit(math_score,physic_score)

但它給了我這個錯誤:

如果您的數據具有單個特征,則使用 array.reshape(-1, 1) 重塑您的數據,如果它包含單個樣本,則使用 array.reshape(1, -1)

import pandas as pd
from sklearn.linear_model import LinearRegression

score_file = pd.read_csv('scores.csv')
# score_file = pd.DataFrame.from_dict(
#     {'math': [17, 16, 18], 
#      'physics': [15, 12, 19]})

physic_score = score_file['physics']

print(score_file.shape)  # (3, 2)
print(physic_score.shape) # (3,)

# take care of the dimentions
cls = LinearRegression().fit(score_file,physic_score)

# this should be made with a test subdataset or so...
predictions = cls.predict(score_file)

print(predictions) # [15. 12. 19.]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM