52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
import numpy as np
|
|
import pandas as pd
|
|
import matplotlib.pyplot as plt
|
|
from sklearn.datasets import make_regression
|
|
|
|
from mlfromscratch.utils import train_test_split, polynomial_features
|
|
from mlfromscratch.utils import mean_squared_error, Plot
|
|
from mlfromscratch.supervised_learning import LinearRegression
|
|
|
|
def main():
|
|
|
|
X, y = make_regression(n_samples=100, n_features=1, noise=20)
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
|
|
|
|
n_samples, n_features = np.shape(X)
|
|
|
|
model = LinearRegression(n_iterations=100)
|
|
|
|
model.fit(X_train, y_train)
|
|
|
|
# Training error plot
|
|
n = len(model.training_errors)
|
|
training, = plt.plot(range(n), model.training_errors, label="Training Error")
|
|
plt.legend(handles=[training])
|
|
plt.title("Error Plot")
|
|
plt.ylabel('Mean Squared Error')
|
|
plt.xlabel('Iterations')
|
|
plt.show()
|
|
|
|
y_pred = model.predict(X_test)
|
|
mse = mean_squared_error(y_test, y_pred)
|
|
print ("Mean squared error: %s" % (mse))
|
|
|
|
y_pred_line = model.predict(X)
|
|
|
|
# Color map
|
|
cmap = plt.get_cmap('viridis')
|
|
|
|
# Plot the results
|
|
m1 = plt.scatter(366 * X_train, y_train, color=cmap(0.9), s=10)
|
|
m2 = plt.scatter(366 * X_test, y_test, color=cmap(0.5), s=10)
|
|
plt.plot(366 * X, y_pred_line, color='black', linewidth=2, label="Prediction")
|
|
plt.suptitle("Linear Regression")
|
|
plt.title("MSE: %.2f" % mse, fontsize=10)
|
|
plt.xlabel('Day')
|
|
plt.ylabel('Temperature in Celcius')
|
|
plt.legend((m1, m2), ("Training data", "Test data"), loc='lower right')
|
|
plt.show()
|
|
|
|
if __name__ == "__main__":
|
|
main() |