繁体   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