<jupyter_start><jupyter_text>PREDICTING WIND TURBINE POWER OUTPUT FROM WIND SPEED VALUES  using LINEAR REGRESSION  Objectives 1 Perform linear regression on the dataset. 2 Explain what this shows. 3 Predict wind speed power output from wind speed values.  SOURCEShttps://www.w3schools.com/python/python_ml_linear_regression.aspI did the tutorial on this site and because you can see worked examples itis very helpful.  I used the LinearRegression method from this site for myfirst example and to find the R² value.    https://realpython.com/linear-regression-in-python/regressionI also did the tutorial in this site to gain a better understanding of the task.    https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.htmlThis site has a example of linear regression which was useful to refer to.    https://scipy-lectures.org/packages/scikit-learn/index.htmlI did the first section of this tutorial about machine learning and the purposesof linear regression in training datasets.https://www.oreilly.com/library/view/machine-learning-with/9781491989371/ch01.htmlI used this site to help me tranform data from list to array.https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.htmlScipy documentation on Pearson's r, not as helpful as the following site.      https://realpython.com/numpy-scipy-pandas-correlation-pythonI found and used information from this site about tests to correlate two variables.I used Pearson's r, Spearman's rho and Kendall's tau to measure the correlation betweenwind speed and power output.    https://www.w3schools.com/python/python_ml_polynomial_regression.aspI did this tutorial and again found it helpful to do some worked examples before doing myown in my Jupyter notebook.  NoteThe speed variable is sometimes referred to as x, when it is changed to a 1D array it is speed_data_array.The power variable is sometimes referred to as y, when it is changed to a 1D array it is power_data_array.  IMPORTING AND PLOTTING THE DATASET<jupyter_code># importing library and dataset, which I call dfpower.
import pandas as pd

dfpower = pd.read_csv("https://raw.githubusercontent.com/ianmcloughlin/2020A-machstat-project/master/dataset/powerproduction.csv")

# examining what type is our data.
dfpower.dtypes

# looking at the heads and ends of dataset.
print(dfpower)
# import libraries
import matplotlib.pyplot as plt
import numpy as np

# x = speed, y= power
x = dfpower.iloc[:,[0]]
y = dfpower.iloc[:,[1]]

# plot each (x,y) sample on graph.
plt.style.use("ggplot")
plt.rcParams["figure.figsize"] = (18,10)
plt.title("Wind Turbine Power")
plt.xlabel("Speed")
plt.ylabel("Power")
plt.plot(x, y, '.')<jupyter_output><empty_output><jupyter_text>USING SKLEARN LINEARREGRESSION TO FIND THE RELATIONSHIP BETWEEN X & Y - Objective 1<jupyter_code># import library
from sklearn.linear_model import LinearRegression

# x = speed and y = power from the dataframe dfpower
x = dfpower.iloc[:,[0]]
y = dfpower.iloc[:,[1]]

# create the variable model as the instance of LinearRegression
model=LinearRegression()

# call .fit on the model which calculates the optimal values of the 
# weights bo (the intercept of the y axis) and b1 (slope of the estimated
# regression line)
model.fit(x,y)

# r_sq is the proportion of the variance for a dependent variable that may
# be explained by the influence of independent variable(s)
# calling .score returns the coefficient of determination (r_sq)
r_sq = model.score(x,y)

# the closer the coefficient of determination is to 1 the better the fit,
# because it indicates the sum of squared residuals (SSR) is 0 which is perfect. 
print("coefficient of determination:", r_sq)

# .intercept is the intercept of y axis, bo
print("intercept:", model.intercept_)

# .coef is the slope of regression line, b1.
print("slope:", model.coef_)
# We can now use the y intercept and the slope multiplied by each value
# of x in turn to predict y (y_pred), this is basically the equation of the line.

y_pred = model.intercept_+ model.coef_ * x
print("predicted response:",y_pred)

# Plot outputs of the x and y values as red dots and the x and y_pred
# as a blue line.
plt.scatter(x, y,  color='red')
plt.plot(x, y_pred, color='blue', linewidth=3)

plt.title("Linear Regression on Wind Speed and Turbine Power")
plt.xlabel("Speed")
plt.ylabel("Power")

plt.show()<jupyter_output><empty_output><jupyter_text>USING POLYFIT TO FIND THE RELATIONSHIP BETWEEN X & Y - Objective 1 (Method 2)<jupyter_code># Using polyfit to find the relationship between the x and y variables
import numpy as np

# polyfit expects x to be a 1D vector, convert x to an np.array called speed_data_array.
speed_data = dfpower["speed"]
#print("Original List:", speed_data)

speed_data_array = np.array(speed_data)
#print("One-dimensional NumPy array: ", speed_data_array)

# this will return an array which includes the coefficients; aka the intercept of y axis, bo and
# the slope of regression line, b1.  These are the same figures as I produced using LinearRegression.
np.polyfit(speed_data_array,y,1)

# In linear regression coefficients are the values that multiply the predictor (x) values.
coeffs = np.polyfit(speed_data_array,y,1)

# This plots the original data.
plt.plot(speed_data_array,y,".", label="Data")

# This plots the best fit line.
plt.plot(speed_data_array, coeffs[0]*speed_data_array+coeffs[1], "-","Bestfit")

# Again the plot is the same as the one produced using LinearRegression.
# Calculating the cost of the line
# Cost(m,c)= Σ(yi - mxi-c)² where m is the slope and c is the constant
# This is the same idea as polyfit and produces the same results.

# To use this method I have had to take the values of x(speed) and y(power) out of the dataframe and use
# them as an array
x_avg = np.mean(dfpower["speed"])
y_avg = np.mean(dfpower["power"])

# subtract the means from speed (x) and power (y)
x_zero = dfpower["speed"] - x_avg

y_zero = dfpower["power"] - y_avg


# to calculate the best m (slope)
m = np.sum(x_zero * y_zero) / np.sum(x_zero * x_zero)

# to calculate the best c (y intercept)
c = y_avg - m * x_avg

print("m is %8.6f and c is %6.6f." % (m,c))<jupyter_output>m is 4.917596 and c is -13.899903.<jupyter_text>INTERPRETING THE DATA - Objective 2  Three tests to find the correlation coefficient between speed and power.<jupyter_code># The Pearson correlation coefficient measures the linear relationship
# between two datasets.  Positive correlations imply that as x increases
# so does y, negative correlations imply that if x increases, y decreases.

import numpy as np 
import scipy.stats

# Changing the speed (x) value from dataframe to array.
speed_data = dfpower["speed"]
speed_data_array = np.array(speed_data)

# Changing the power (y) value from dataframe to array.
power_data = dfpower["power"]
power_data_array = np.array(power_data)

scipy.stats.pearsonr(speed_data_array,power_data_array)

# The first value is Pearson's r, this value implies a strong
# positive correlation between wind speed and turbine power output.
# The second value is the p-value which is used to test a hypothesis.
# The Spearman correlation coefficient measures the linear relationship
# between two datasets.  Positive correlations imply that as x increases
# so does y, negative correlations imply that if x increases, y decreases.

scipy.stats.spearmanr(speed_data_array,power_data_array)

# The first value is Spearman's rho, this value implies a strong
# positive correlation between wind speed and turbine power output.
# The second value is the p-value which is used to test a hypothesis.
# The Kendall correlation coefficient measures the linear relationship
# between two datasets.  Positive correlations imply that as x increases
# so does y, negative correlations imply that if x increases, y decreases.

scipy.stats.kendalltau(speed_data_array,power_data_array)

# The first value is Kendall's tau, this value implies a strong
# positive correlation between wind speed and turbine power output.
# The second value is the p-value which is used to test a hypothesis.<jupyter_output><empty_output><jupyter_text>There is a high correlation coefficient between wind speed, x, and power production, y.The values range from 0.73 on the Kendall tau to the highest correlation 0.85 onthe Pearson's r. Source  https://enerpower.ie/portfolio/wind-turbine-faq-ireland/    How strong does the wind have to blow for the wind turbines to work?"Wind turbines typically start operating at wind speeds around Beaufort Force 3 (which is around 3-5 metres per second (m/s), or 8-12 miles per hour (mph). Turbines reach maximum power output at Beaufort 5 (around 11-14 m/s or 25-30 mph). At very high wind speeds, i.e. Beaufort Storm Force 10 winds, (around 24 m/s or 55 mph) or greater the wind turbines shut down to prevent excessive wear and tear. Since winds of this strength occur only for a handful of hours per year, very little energy is lost in high wind periods." The information contained in this faq, from a wind energy company Enerpower, shows us how to interpretour plot.  The illustrated data shows that no power is produced until the wind speed reaches 8 mph, at 20-25 mph it has reached peak production and levels off.  Winds above 20 mph on our plot seemto indicate the best speed for producing power.  Higher wind speeds do not produce more power.  As the faqfrom Enerpower indicate, a turbine will turn itself off in storm force winds. Having analysed this data we have the following information to help predict a y value from an x value. coefficient of determination: 0.7289360258564073intercept: 13.89990263slope: 4.91759567Pearson's r = 0.8537775037188595   PREDICT WIND TURBINE POWER OUTPUT FROM WIND SPEED VALUES - Objective 3<jupyter_code># Using the equation of the line to predict y for 10 selected values of x.
# y1 = mx1 + c, if x1 = 2, find y.
# x = speed, y = power.

x1 = [2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0]
y1 = [4.91759567 * i - 13.89990267 for i in x1]

print(x1)
print(y1)

# Plot the selected points.

import matplotlib.pyplot as plt

plt.style.use("ggplot")
plt.rcParams["figure.figsize"] = (18,10)
plt.title("Wind Turbine Power")
plt.xlabel("Speed")
plt.ylabel("Power")
plt.plot(x1, y1, 'o')


# The plot shows the positive correlation between values of x1,
# the sample of speed variables and y1, the sample of turbine power output.<jupyter_output><empty_output><jupyter_text>The arrangement of the points shows a positive correlation but itmust also take into account that there is a horizontal section before0-7mph and after 20mph.  There must be a windspeed of 7+ for power to beproduced and speeds over 20mph do not produce more power.<jupyter_code># See if Polynomial Regression produces a better correlation between speed and power.

# x = speed, y= power
x = dfpower.iloc[:,[0]]
y = dfpower.iloc[:,[1]]

# plot each (x,y) sample on graph.
plt.style.use("ggplot")
plt.rcParams["figure.figsize"] = (18,10)
plt.title("Wind Turbine Power")
plt.xlabel("Speed")
plt.ylabel("Power")
plt.plot(x, y, '.')

# Draw the line of polynomial regression using the 1D values for speed an power.
import numpy as np

# Use the numpy method to make a polynomial regression model.
mymodel = np.poly1d(np.polyfit(speed_data_array, power_data_array, 3))

# plot 500 points in the sample from the first x value, 0, to the last x value, 25.
myline = np.linspace(0, 25, 500)

# plot the data showing the sample and the polynomial line of regression.
plt.scatter(speed_data_array, power_data_array)
plt.plot(myline, mymodel(myline))
plt.show() 
# Calculate the coefficient of determination of the data.
import numpy as np
from sklearn.metrics import r2_score

# use the polyfit method to find the coefficient of determination or R².

mymodel = np.poly1d(np.polyfit(speed_data_array, power_data_array, 3))

print(r2_score(power_data_array, mymodel(speed_data_array)))<jupyter_output>0.8796883953739737
{'fraction_non_alphanumeric': 0.07916386836803224, 'fraction_numerical': 0.02258226997985225, 'mean_word_length': 4.75785403576607, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 10, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '536919', 'license': 'MIT', 'n_tokens_mistral': 3873, 'n_tokens_neox': 3559, 'n_words': 1541, 'path': 'Linear Regression FDA Project.ipynb', 'repo_name': 'mobm65/Linear-Regression-FDA-Project', 'size': '11912'}