简体   繁体   中英

How to fit data in sklearn

I want to write code that read a csv file and then use linear regression for prediction.
The CSV file is like this:

math physics
17 15
16 12
18 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

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.]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM