import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
dataset = pd.read_csv("/Users/heyuwei/Documents/机器学习数据集/Sales-Prediction-master/advertising.csv")
dataset.head()
dataset.drop(columns=['Radio', 'Newspaper'], inplace = True)
dataset.head()
#Setting the value for X and Y
x = dataset[['TV']]
y = dataset['Sales']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 100)
slr = LinearRegression()
slr.fit(x_train, y_train)
print("Intercept: ", slr.intercept_)
print("Coefficient: ", slr.coef_)
#Prediction of test set
y_pred_slr= slr.predict(x_test)
#Predicted values
print("Prediction for test set: {}".format(y_pred_slr))
#Actual value and the predicted value
slr_d
linear regression
于 2023-03-27 17:04:14 首次发布