[英]How to fit data in sklearn
I want to write code that read a csv file and then use linear regression for prediction.我想编写读取 csv 文件的代码,然后使用线性回归进行预测。
The CSV file is like this: CSV文件是这样的:
math数学 | physics物理 |
---|---|
17 17 | 15 15 |
16 16 | 12 12 |
18 18 | 19 19 |
I try this code:我试试这段代码:
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)
but it gives me this error:但它给了我这个错误:
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如果您的数据具有单个特征,则使用 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.