尝试对数据进行线性回归并得到“数据必须为一维”错误。
样本数据
Year ln_kar
1 4.65
2 5.63
3 6.63
4 2.33
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
data1= pd.read_excel('C:\\Users\\3004\\Desktop\\Vaibbhav\\data\\Agricultural cost and price state wise\\crop cost CACP.xlsx',2)
x=data1['Year']
y=data1['ln_kar']
x1 = np.reshape(x,(-1,1))
y1 = np.reshape(y,(-1,1))
model = LinearRegression().fit(x1,y1)
print('intercept: ',model.intercept_)
print('slope: ',model.coef_)