def linear_regression_example(): df = pd.read_csv('datasets/test.csv') x = df.iloc[1:, 0].values y = df.iloc[1:, 1].values fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(x[:], y[:], color='blue', marker='o', label='diamonds') regession = LinearRegression() regession.fit(x, y) fit_x = np.linspace(np.min(x), np.max(x), 100) fit_y = regession.get_slope() * fit_x + regession.get_intercept() min_y = regession.get_slope() * fit_x + regession.get_intercept() - regession.get_interval() max_y = regession.get_slope() * fit_x + regession.get_intercept() + regession.get_interval() ax.plot(fit_x, fit_y, 'g--') ax.plot(fit_x, min_y, 'r-') ax.plot(fit_x, max_y, 'r-') plt.show()
def linear_regression_example(): df = pd.read_csv( 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/diamonds.csv' ) df = df[df['price'] > 1] x = df.iloc[1:, 6].values y = df.iloc[1:, 0].values print(y[0]) fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(x[:], y[:], color='blue', marker='o', label='diamonds') ax.legend() regession = LinearRegression() regession.fit(x, y) fit_x = np.linspace(np.min(x), np.max(x), 100) fit_y = regession.get_slope() * fit_x + regession.get_intercept() ax.plot(fit_x, fit_y) plt.show()
from utils import * import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import axes3d import pandas as pd # Linear Regression example with randomly generated data print("Running Linear Regression Demo") print("\tLinear Regression Example on Randomly Generated Data") X, Y = genPointsFittedToLine(np.array([-2]), 10) plotTitle = "Linear Regression Demo:\n" plotTitle += "Data Visualization for Randomly Generated Data Fitted to a Line" plotRegression(X, Y, title=plotTitle) # Create the Model model = LinearRegression() # Fit the model model.fit(X, Y) print("\t\tSquared Error: ", end="") print(model.squaredError(X, Y)) # Display regression plane plotTitle = "Regression Line Determined by Linear Regression Model for the\n" plotTitle += "Randomly Generated Data" plotRegression(X, Y, w=model.getWeights(), title=plotTitle) # Linear Regression example with the Iris Plants Databse print("\tLinear Regression Example on the Iris Plants Database") # Download and read the data df = pd.read_csv( 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
def demo_helper(X, Y, learning_rate, iterations, title, x_label, y_label, x_lim=None, y_lim=None): # Splitting the Dataset X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size= 0.33, random_state= 101) # Custom ML linear regression module ml_regressor = LinearRegression(learning_rate=learning_rate, iterations=iterations) # scikit-learn linear regression module scikit_regressor = skLinearRegression() # Perform linear regression using both scikit-learn and ML.py library ml_regressor.fit(X_train, y_train) # custom version scikit_regressor.fit(X_train, y_train) # scikit-learn version # Make predictions mlY_pred = ml_regressor.predict(X_test) skY_pred = scikit_regressor.predict(X_test) # Plot the X, Y data plt.scatter(X, Y) plt.title(title + ": Raw Data") plt.xlabel(x_label) plt.ylabel(y_label) plt.show() # Plot the X, Y_pred data custom_label = title + ": ML.py Linear Regression Module\nLearning Rate: " + \ str(learning_rate) + ", Iterations: " + str(iterations) plot_regression_line(mlY_pred, X_test, y_test, x_label, y_label, custom_label, x_range=x_lim, y_range=y_lim) plot_regression_line(skY_pred, X_test, y_test, x_label, y_label, title + ": scikit-learn Linear Regression Module", x_range=x_lim, y_range=y_lim) # Evaluate Each Model's Performance d_ml = { 'Metrics': ['Mean Absolute Error:', 'Mean Squared Error:', 'Mean Root Squared Error:'], 'Values': [mean_absolute_error(y_test, mlY_pred), mean_squared_error(y_test, mlY_pred), np.sqrt(mean_squared_error(y_test, mlY_pred))] } d_sk = { 'Metrics': ['Mean Absolute Error:', 'Mean Squared Error:', 'Mean Root Squared Error:'], 'Values': [mean_absolute_error(y_test, skY_pred), mean_squared_error(y_test, skY_pred), np.sqrt(mean_squared_error(y_test, skY_pred))] } df_ml = pd.DataFrame(data=d_ml) df_sk = pd.DataFrame(data=d_sk) fig, ax = plt.subplots(2) cell_text = [] for row in range(len(df_ml)): cell_text.append(df_ml.iloc[row]) ax[0].set_title(title + ": ML.py Linear Regression Module\nLearning Rate: " + str(learning_rate) + ", Iterations: " + str(iterations)) ax[0].table(cellText=cell_text, colLabels=df_ml.columns, loc='center') ax[0].axis(False) cell_text = [] for row in range(len(df_sk)): cell_text.append(df_sk.iloc[row]) ax[1].set_title(title + ": scikit-learn Linear Regression Module") ax[1].table(cellText=cell_text, colLabels=df_sk.columns, loc='center') ax[1].axis(False) plt.show() print("ML.py Linear Regression Weights for y = mx + b (" + title + "):") print("Slope:", ml_regressor.slope) print("Intercept:", ml_regressor.intercept, "\n")
print y def all_func(x, y, model, state, window_state): if draw and model: linear_regression.plot(x, y, model.w) if __name__ == "__main__": i = 0 w = np.random.rand(num_features + 1, 1) * 2 - 1 w *= 5 m = LinearRegression.LinearRegressionStream(draw=False, output=output, alpha=0.001) x = Stream('x') linear_regression.init_plot() model = Stream_Learn(data_train=x, data_out=x, train_func=m.train, predict_func=m.predict, min_window_size=min_window_size, max_window_size=max_window_size, step_size=step_size, num_features=num_features, all_func=all_func) y = model.run()
def test_linearRegression(): df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None) print("Creating a weak positive correlation data set") y = df.iloc[0:50, 0] x = df.iloc[0:50, 1] print("Creating Linear-Regression objet...") linear = LinearRegression() print("Predicting line of best fit given a weak positive correlation...") linear.predict(x, y) print("Graphing weak positive correlation...") linear.graph(x, y) print("Creating a random strong negative correlation dataset...") x = np.random.randn(50) y = np.random.randn(1) - 2 * x print("Graphing weak positive correlation vs strong negative correlation dataset...") linear.graph(x, y) print("Retraining the linear regression leaner for a strong positive correlation dataset...") linear.predict(x, y) print("Graphing strong negative correlation...") linear.graph(x, y) print("Creating a weak negative correlation...") y = df.iloc[0:50, 0] x = df.iloc[0:50, 1] np.negative(x) print("Retraining the linear regression learner for a weak negative correlation dataset...") linear.predict(x, y) print("Graphing weak negative correlation...") linear.graph(x, y) print("Making a uniform dataset...") x = np.random.rand(50) print("Retraining the linear regression learner for a uniform distribution...") linear.predict(x, y) print("Graphing uniform distribution...") linear.graph(x, y)