Machine Learning using Linear Regression
import pandas as pd
df = pd.read_csv('TSLA.csv')
import pandas as pd
# Load .csv file as DataFrame
df = pd.read_csv('TSLA.csv')
# print the data
print(df)
# print some summary statistics
print(df.describe())
# Indexing data using a DatetimeIndex
df.set_index(pd.DatetimeIndex(df['Date']), inplace=True)
# Keep only the 'Adj Close' Value
df = df[['Adj Close']]
# Re-inspect data
print(df)
print(df.info())
import matplotlib.pyplot as plt
plt.plot(df[['Adj Close']])
plt.title('TESLA Share Price')
plt.xlabel('Year')
plt.ylabel('Adj Close Volume')
plt.savefig('TESLA.png')
plt.show()
import pandas_ta
df.ta.ema(close='Adj Close',length=10, append=True)
# will give Nan Values for First 10 Rows
# We have to fillup data
df = df.iloc[10:]
print(df.head(10))
#
plt.plot(df['Adj Close'])
plt.plot(df['EMA_10'])
plt.xlabel('')
plt.ylabel('')
plt.title('TESLA Share Price with EMA overlaid')
plt.legend(["blue", "orange"], loc=0)
plt.savefig('TESLA_EMA_10.png')
plt.show()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(df[['Adj Close']], df[['EMA_10']], test_size=.2)
from sklearn.linear_model import LinearRegression
# Create Regression Model
model = LinearRegression()
# Train the model
model.fit(X_train, y_train)
# Use model to make predictions
y_pred = model.predict(X_test)
#Test Set
print(X_test.describe())
# Training set
print(X_train.describe())
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
# Printout relevant metrics
print("Model Coefficients:", model.coef_) # [[0.98176283]]
print("Mean Absolute Error:", mean_absolute_error(y_test, y_pred)) #6.21531704292117
print("Coefficient of Determination:", r2_score(y_test, y_pred)) #0.9942788743625711
Here is a simple Regression ML Model to find out the MAE and R-Squared error.
No comments:
Post a Comment