<jupyter_start><jupyter_text>Huber Regression Huber Regression is the Huber loss. A loss function used in robust regression and is less sensitive to outliers in data than the squared error loss. A variant for classification is also sometimes used. Therefore, huber regression is a technique that is robust to outliers.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

import warnings
warnings.filterwarnings("ignore")

# yahoo finance is used to fetch data 
import yfinance as yf
yf.pdr_override()
# input
symbol = 'AMD'
start = '2014-01-01'
end = '2018-08-27'

# Read data 
dataset = yf.download(symbol,start,end)

# View Columns
dataset.head()
# Create more data
dataset['Increase_Decrease'] = np.where(dataset['Volume'].shift(-1) > dataset['Volume'],1,0)
dataset['Buy_Sell_on_Open'] = np.where(dataset['Open'].shift(-1) > dataset['Open'],1,-1)
dataset['Buy_Sell'] = np.where(dataset['Adj Close'].shift(-1) > dataset['Adj Close'],1,-1)
dataset['Return'] = dataset['Adj Close'].pct_change()
dataset = dataset.dropna()
dataset.head()
dataset.shape
X = dataset['Open']
y = dataset['Adj Close'] 
X = np.array(X).reshape(1170,-1)
y = np.array(y).reshape(1170,-1)
from sklearn.linear_model import HuberRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold
from numpy import absolute, arange
# Define the model
model = HuberRegressor()
# Define model evaluation
cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
# Evaluate model
scores = cross_val_score(model, X, y, scoring='neg_mean_absolute_error', cv=cv, n_jobs=-1)
# Change scores to be positive
scores = absolute(scores)
 
model.fit(X, y)
plt.scatter(X,y)
xaxis = arange(X.min(), X.max(), 0.01)
y_pred = model.predict(xaxis.reshape((len(xaxis), 1)))
plt.plot(xaxis, y_pred, color='r')
plt.title('Huber Regression for Stock Price')
plt.show()
def evaluate_model(X, y, model):
	# Define model evaluation
	cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
	# Evaluate model
	scores = cross_val_score(model, X, y, scoring='neg_mean_absolute_error', cv=cv, n_jobs=-1)
	# Scores to be positive
	return absolute(scores)
results = evaluate_model(X, y, model) 
print('Average MAE: %.3f (%.3f)' % (results.mean(), results.std())) 
model.coef_
model.intercept_
model.predict(y)
from sklearn.metrics import r2_score 
r2_score(X,y)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13291925465838508, 'fraction_numerical': 0.02194616977225673, 'mean_word_length': 6.14792899408284, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '295767', 'license': 'MIT', 'n_tokens_mistral': 885, 'n_tokens_neox': 837, 'n_words': 225, 'path': 'Stock_Algorithms/Huber_Regression.ipynb', 'repo_name': 'NTForked-ML/Deep-Learning-Machine-Learning-Stock', 'size': '2415'}