# 机器学习-线性回归（Linear Regression）案例

2019/09/11 15:47

### 背景介绍

Y  - 因变量

a  - 坡度

X  - 自变量

b  - 拦截

#### 上文代码块

# importing required librariesimport pandas as pdfrom sklearn.linear_model import LinearRegressionfrom sklearn.metrics import mean_squared_error# read the train and test datasettrain_data = pd.read_csv('train.csv')test_data = pd.read_csv('test.csv')print(train_data.head())# shape of the datasetprint('\nShape of training data :',train_data.shape)print('\nShape of testing data :',test_data.shape)# Now, we need to predict the missing target variable in the test data# target variable - Item_Outlet_Sales# seperate the independent and target variable on training datatrain_x = train_data.drop(columns=['Item_Outlet_Sales'],axis=1)train_y = train_data['Item_Outlet_Sales']# seperate the independent and target variable on training datatest_x = test_data.drop(columns=['Item_Outlet_Sales'],axis=1)test_y = test_data['Item_Outlet_Sales']'''Create the object of the Linear Regression modelYou can also add other parameters and test your code hereSome parameters are : fit_intercept and normalizeDocumentation of sklearn LinearRegression:https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html '''model = LinearRegression()# fit the model with the training datamodel.fit(train_x,train_y)# coefficeints of the trained modelprint('\nCoefficient of model :', model.coef_)# intercept of the modelprint('\nIntercept of model',model.intercept_)# predict the target on the test datasetpredict_train = model.predict(train_x)print('\nItem_Outlet_Sales on training data',predict_train)# Root Mean Squared Error on training datasetrmse_train = mean_squared_error(train_y,predict_train)**(0.5)print('\nRMSE on train dataset : ', rmse_train)# predict the target on the testing datasetpredict_test = model.predict(test_x)print('\nItem_Outlet_Sales on test data',predict_test)# Root Mean Squared Error on testing datasetrmse_test = mean_squared_error(test_y,predict_test)**(0.5)print('\nRMSE on test dataset : ', rmse_test)

0
0 收藏

0 评论
0 收藏
0