<jupyter_start><jupyter_text>Fundamentals of Data Analysis Student: <NAME> Student ID number: G00387884  InstructionsIn this project you must perform and explain simple linear regression using Python on the ``powerproduction`` dataset available on Moodle. The goal is to accurately predict wind turbine power output from wind speed values using the data set as a basis.Your submission must be in the form of a git repository containing, at a minimum, the following items:**1. Jupyter notebook that performs simple linear regression on the data set.****2. In that notebook, an explanation of your regression and an analysis of its accuracy.****3. Standard items in a git repository such as a README.**To enhance your submission, you might consider comparing simple linear regression to other types of regression on this data set. Rest assured, all the above concepts will be explored in lecture videos and other materials in the coming semester.  IntroductionLinear regression is a statistical method that tries to model the relationship between a continuous variable and one or more independent variables by fitting a linear equation. It is called simple linear regression when there is only one independent variable and multiple linear regression when there is more than one. Depending on the context, the modeled variable is known as a dependent variable or response variable, and the independent variables as regressors, predictors or features.![imagen regression](https://miro.medium.com/max/560/1*N2usf10aKCq1JBIqxj-YFQ.png)As you can see, a linear relationship can be positive (the independent variable goes up, the dependent variable goes up) or negative (the independent variable goes up, the dependent variable goes down).  I. Linear regression.Linear regression is a supervised learning algorithm used in machine learning and statistics.For more information you can enter wikipedia [here](https://es.wikipedia.org/wiki/Regresi%C3%B3n_lineal)![imagen](https://upload.wikimedia.org/wikipedia/commons/3/3a/Linear_regression.svg) Let's quickly recall the formula for the line:``Y = mX + b``Where ``Y`` is the result, ``X`` is the variable, ``m`` the slope (or coefficient) of the line and ``b`` the constant or also known as the "cut-off point with the ``Y`` axis" in the graph (when ``X = 0``) Here we see an example where we see data collected on the prices of pizzas in Denmark (the points in red) and the black line is the trend. That is the regression line that we want the algorithm to learn and calculate only.![imagen pizza](https://i1.wp.com/www.aprendemachinelearning.com/wp-content/uploads/2018/05/pizza_en_dinamarca.png)  II. Linear regression algorithm.Let us remember that Supervised Machine Learning algorithms learn by themselves and -in this case- to automatically obtain that "line" that we are looking for with the prediction trend. To do this, the error is measured with respect to the input points and the actual output “Y” value. The algorithm should minimize the cost of a quadratic error function and those coefficients will correspond to the optimal line. There are several methods to minimize the cost. The most common is to use a vector version and the so-called Normal Equation that will give us a direct result.<jupyter_code># Data:
x=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
y=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
n = len(x)
x = np.array(x)
y = np.array(y)
sumx = sum(x)
sumy = sum(y)
sumx2 = sum(x*x)
sumy2 = sum(y*y)
sumxy = sum(x*y)
promx = sumx/n
promy = sumy/n
m = (sumx*sumy - n*sumxy)/(sumx**2 - n*sumx2)
b = promy - m*promx
m , b
plt.plot(x,y,'o', label='Data')
plt.plot(x, m*x + b, label='settings')
plt.ylabel('x')
plt.ylabel('y')
plt.title('My first linear regression')
plt.grid()
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>III. Developing the linear regression of POWERPRODUCTION.CSVLet's start by importing the libraries that we will use:<jupyter_code># Required Imports
import numpy as np
import pandas as pd
import seaborn as sb
import matplotlib.pyplot as plt
%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
plt.rcParams['figure.figsize'] = (16, 9)
plt.style.use('ggplot')
from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score<jupyter_output><empty_output><jupyter_text>We read the csv file and load it as a Pandas dataset. And we see its size<jupyter_code># we load the input data
data = pd.read_csv("powerproduction.csv")
# let's see how many dimensions and records it contains
data.shape
powerproduction.describe<jupyter_output><empty_output><jupyter_text>Let's look at those first rows:<jupyter_code>## is 500 records with 2 columns. Let's see the first records
data.head()<jupyter_output><empty_output><jupyter_text>Let's look at some basic statistics of our input data:<jupyter_code># Now let's see some statistics of our data
data.describe()<jupyter_output><empty_output><jupyter_text>Now we are going to generate related variables<jupyter_code>from matplotlib import pyplot as plt
import pandas as pd
powerproduction = pd.read_csv('powerproduction.csv')
data1 = powerproduction['speed'].tolist()
data2 = powerproduction['power'].tolist()
plt.scatter(data1, data2)
plt.show()<jupyter_output><empty_output><jupyter_text>IV. Development of our simple linear regressionLinear Regression is a parametric technique used to predict continuous, dependent variables, given a set of independent variables.The goal with Simple Linear Regression is to minimize the vertical distance between all the data and our line, therefore, to determine the best line, we must minimize the distance between all the points and the distance from our line. There are many methods to accomplish this goal, but all of these methods have only one goal, which is to minimize distance.One way that the regression model finds the best line of fit is by using the least squares criterion to reduce the error.Error is an inevitable part of the prediction process, no matter how powerful the algorithm we choose is, there will always be irreducible error. We know that we cannot completely eliminate the error, but we can still try to reduce it to the lowest level. It is precisely at this time that the technique known as least squares is used.The least squares technique tries to reduce the sum of errors squared, looking for the best possible value of the regression coefficients.Least squares is not the only technique to use in Linear Regression but it is selected because:It uses a square error that has good mathematical properties, making it easier to differences and calculate the descent of the gradient.It is easy to analyze and computationally faster, that is, it can work quickly on data sets that have miles of characteristics.Interpretation is much easier than other regression techniques.Next we visualize the simple line of the data<jupyter_code>import csv

with open('powerproduction.csv', 'r', newline='') as file:
    has_header = csv.Sniffer().has_header(file.read(1024))
    file.seek(0)  # Rewind.
    reader = csv.reader(file)
    if has_header:
        next(reader)
    your_list = list(reader)

independentVars = []
dependentVars = []

for i in range(len(your_list)):
    independentVars.append(float(your_list[i][0]))
    dependentVars.append(float(your_list[i][1]))
    
simpleLinearRegression(independentVars, dependentVars)<jupyter_output>265989.41437957867<jupyter_text>The next section shows us the energy generated.<jupyter_code>import seaborn as sns
import pandas as pd

powerproduction = pd.read_csv('powerproduction.csv')
sns.pairplot(powerproduction)<jupyter_output><empty_output><jupyter_text>V. Linear Regression with Python and SKLearnWe are going to create our input data for the moment only ``speed`` and as labels the ``power``. We create the LinearRegression object and make it "fit" (train) with the fit () method. Finally we print the coefficients and scores obtained.<jupyter_code># # We assign our input variable X for training and the labels Y.
dataX =filtered_data[["speed"]]
X_train = np.array(dataX)
y_train = filtered_data['power'].values
 
# We create the Linear Regression object
regr = linear_model.LinearRegression()
 
# We train our model
regr.fit(X_train, y_train)
 
# We make the predictions that ultimately a line (in this case, being 2D)
y_pred = regr.predict(X_train)
 
# Let's see the coefficients obtained, In our case, they will be the Tangent
print('Coefficients: \n', regr.coef_)

print('Independent term: \n', regr.intercept_)

print("Mean squared error: %.2f" % mean_squared_error(y_train, y_pred))

print('Variance score: %.2f' % r2_score(y_train, y_pred))<jupyter_output>Coefficients: 
 [0.9702016]
Independent term: 
 0.44839587792491553
Mean squared error: 14.77
Variance score: 0.34<jupyter_text>VI. Prediction in simple linear regressionLet's try to test our algorithm, assuming we wanted to predict how many "shares" a 2000 word ML article will get.<jupyter_code>y_Dosmil = regr.predict([[2000]])
print(int(y_Dosmil))
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

lin_data = pd.read_csv('powerproduction.csv')

X = lin_data.iloc[:, 0].values
y = lin_data.iloc[:, 1].values

def scatter_plot(X, y, col, mark, lab):
    plt.scatter(X, y, color=col, marker=mark, label=lab);

    # Title of plot
    plt.title("speed and power", fontsize=25)
    
    # the x label of plot
    plt.xlabel("Speed, ms$^{-1}$", fontsize=20)
    
    # the y label of plot
    plt.ylabel("Power Watts", fontsize=20)
    
    # change size of ticks of x and y axes
    ax = plt.gca()
    ax.tick_params(axis = 'both',which = 'major', labelsize = 14)
    
scatter_plot(X, y, "green", 'o', 'power')
lin_data.loc[(lin_data['power'] == 0 ) & (lin_data['speed'] > 10)]
lin_data = lin_data.drop([208, 340, 404, 456, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499]);
lin_data.loc[(lin_data['power'] == 0 ) & (lin_data['speed'] > 10)].size
X = lin_data.iloc[:, 0].values
y = lin_data.iloc[:, 1].values

X = X.reshape(-1, 1)
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0)
regressor = LinearRegression()
regressor.fit(X_train, y_train)
print(regressor.intercept_)
print(regressor.coef_)
from sklearn import metrics
import numpy as np
y_pred = regressor.predict(X_test)
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print('Mean of observed y values:', np.mean(y))
print('Coefficient of variation:', (100*np.sqrt(metrics.mean_squared_error(y_test, y_pred)))/np.mean(y))
scatter_plot(X_test, y_test, "green", "o", "Observed")
scatter_plot(X_test, y_pred, "blue", "x", "Predicted")
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.092041147807255, 'fraction_numerical': 0.020754376466341814, 'mean_word_length': 5.436120789779326, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 42, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '530036', 'license': 'MIT', 'n_tokens_mistral': 3539, 'n_tokens_neox': 3281, 'n_words': 1316, 'path': 'Project 52446.ipynb', 'repo_name': 'hansperu/project_52446', 'size': '11101'}