xxxxxxxxxx
# MSE - Mean Squared Error
# Import modules
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
# Loading data
file = 'http://lib.stat.cmu.edu/datasets/boston'
columns = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO','B', 'LSTAT', 'MEDV']
data = read_csv(file, delim_whitespace = True, names = columns)
array = data.values
# Separating the array into input and output components
X = array[:,0:13]
Y = array[:,13]
# Divides the data into training and testing
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.33, random_state = 5)
# Creating model
model = LinearRegression()
# Traning model
model.fit(X_train, Y_train)
# Making predictions
Y_pred = model.predict(X_test)
# Metric Result
mse = mean_squared_error(Y_test, Y_pred)
print("The MSE of the model is::", mse)
xxxxxxxxxx
# ElasticNet Regression
# Import modules
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import ElasticNet
# Loading data
file = 'http://lib.stat.cmu.edu/datasets/boston'
columns = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO','B', 'LSTAT', 'MEDV']
data = read_csv(file, delim_whitespace = True, names = columns)
array = data.values
# Separating the array into input and output components
X = array[:,0:13]
Y = array[:,13]
# Divides the data into training and testing
train_test_split()
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.33, random_state = 5)
# Creating model
model = ElasticNet()
# Training model
model.fit(X_train, Y_train)
# Making predictions
Y_pred = model.predict(X_test)
# Metric Result
mse = mean_squared_error(Y_test, Y_pred)
print("The MSE of the model is:", mse)