'NameError: name 'predictions' is not defined
i am running the below code and getting this error. Please help:
Error: NameError: name 'predictions' is not defined
Code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from sklearn import linear_model
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns; sns.set(color_codes=True)
import seaborn
from datetime import date
from datetime import datetime
today = date.today()
sns.set_color_codes("dark")
from sklearn.linear_model import LinearRegression
from sklearn.metrics import accuracy_score
d3 = today.strftime("%Y%m%d")
d5 = "S:\\Investment Process\\LCRV_Strategy\\1.VOLS Pack\\Drop\\Main_HY.CDX."+d3+".csv"
data = df=pd.read_csv(d5, skiprows=3)
#df.head()
plt.figure(figsize=(11.5, 8.5))
plt.scatter(
df['1M 10-50 HY'],
df['Spread'],
c='black'
)
plt.scatter(x='1M 10-50 HY', y='Spread', data=data.iloc[-1], c='orange')
plt.xlabel("1M 10-50 HY")
plt.ylabel("Spread")
plt.plot(
df['1M 10-50 HY'],
predictions,
c='blue',
linewidth=2
)
X = df['1M 10-50 HY'].values.reshape(-1,1)
y = df['Spread'].values.reshape(-1,1)
reg = LinearRegression()
reg.fit(X, y)
print("The linear model is: Y = {:.5} + {:.5}X".format(reg.intercept_[0], reg.coef_[0][0]))
X = df['1M 10-50 HY']
y = df['Spread']
#X2 = sm.add_constant(X)
#est = sm.OLS(y, X2)
#est2 = est.fit()
#print(est2.summary())
plt.show()
Solution 1:[1]
of course that is because the Python compiler does not know what is "predictions"! if you want to predict you must call
predictions= reg.predict(x)
after the reg.fit()
line.
then you can plot.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Awadelrahman M. A. Ahmed |