from sklearn.metrics import mean_squared_error, r2_scorefrom sklearn.linear_model import LinearRegression
时间: 2024-05-29 14:11:30 浏览: 14
# As an AI language model, I cannot give you the complete code for this task without a specific dataset. However, I can give you an example template:
# Importing the required libraries
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.linear_model import LinearRegression
# Reading the dataset
data = pd.read_csv('dataset.csv')
# Splitting the data into training and testing set
train_data = data.iloc[:80,:]
test_data = data.iloc[80:,:]
# Separating the independent and dependent variables
x_train = train_data.iloc[:,:-1]
y_train = train_data.iloc[:,-1]
x_test = test_data.iloc[:,:-1]
y_test = test_data.iloc[:,-1]
# Creating an instance of the Linear Regression model
model = LinearRegression()
# Fitting the model on the training data
model.fit(x_train, y_train)
# Predicting the dependent variable for the test data
y_pred = model.predict(x_test)
# Evaluating the model using Mean Squared Error and R-squared score
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print("Mean Squared Error:", mse)
print("R-squared score:", r2)