Reputation:
I have the following data in a pandas dataframe:
freq = [10, 2, 1, 10, 6, 4, 1, 1,
6, 3, 4, 10, 6, 3, 9, 5,
5, 5, 4, 2, 2, 9, 11, 7, 5,
1, 3, 10, 7, 5, 5, 5, 8,
7, 25, 17, 9, 6, 7, 8, 4,
10, 3, 1, 7, 11, 6, 5, 10,
11, 8, 11, 15, 4, 6, 11, 6,
10, 10, 10, 4, 5, 7, 15, 15,
10, 12, 17, 25, 26, 22, 14, 15,
15, 7, 9, 8, 6, 1]
date=[737444, 737445, 737446, 737447, 737448,
737449, 737450, 737451, 737452, 737453, 737454, 737455, 737456,
737457, 737458, 737459, 737460, 737461, 737462, 737463, 737464,
737465, 737466, 737467, 737468, 737469, 737470, 737472, 737473,
737474, 737475, 737476, 737477, 737478, 737479, 737480, 737481,
737482, 737483, 737484, 737485, 737486, 737487, 737488, 737489,
737490, 737491, 737492, 737493, 737494, 737495, 737496, 737497,
737498, 737499, 737500, 737501, 737502, 737503, 737504, 737505,
737506, 737507, 737508, 737509, 737510, 737511, 737512, 737513,
737514, 737515, 737516, 737517, 737518, 737519, 737520, 737521,
737522, 737523]
I have calculated the coefficient and intercept for regression as follows:
from sklearn.model_selection import train_test_split
y = np.asarray(df['Frequency'])
X = df[['Date']]
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
model.score(X_train, y_train)
coefs = zip(model.coef_, X.columns)
model.__dict__
Getting the following results:
Coefficient:
[0.08711929]
Intercept:
-64241.58584385233
sl = -64241.6 + 0.1 Date
I would like to plot this line above the plot that shows the trend of actual data. How can I do?
Upvotes: 3
Views: 865
Reputation: 88236
A linear regression's model is defined by X*model.coef_ + model.intercept_
(essentially, the result of the prediction). So you can plot the slope along with a scatter plot of the data for a nice visualization of the result:
freq=[10, 2, 1, 10, 6, 4, 1, 1, 6, 3, 4, 10, 6, 3, 9, 5, 5, 5, 4, 2, 2, 9, 11, 7, 5, 1, 3, 10, 7, 5, 5, 5, 8, 7, 25, 17, 9, 6, 7, 8, 4, 10, 3, 1, 7, 11, 6, 5, 10, 11, 8, 11, 15, 4, 6, 11, 6, 10, 10, 10, 4, 5, 7, 15, 15, 10, 12, 17, 25, 26, 22, 14, 15, 15, 7, 9, 8, 6, 1]
date=[737444, 737445, 737446, 737447, 737448, 737449, 737450, 737451, 737452, 737453, 737454, 737455, 737456, 737457, 737458, 737459, 737460, 737461, 737462, 737463, 737464, 737465, 737466, 737467, 737468, 737469, 737470, 737472, 737473, 737474, 737475, 737476, 737477, 737478, 737479, 737480, 737481, 737482, 737483, 737484, 737485, 737486, 737487, 737488, 737489, 737490, 737491, 737492, 737493, 737494, 737495, 737496, 737497, 737498, 737499, 737500, 737501, 737502, 737503, 737504, 737505, 737506, 737507, 737508, 737509, 737510, 737511, 737512, 737513, 737514, 737515, 737516, 737517, 737518, 737519, 737520, 737521, 737522, 737523]
X = np.array(date)
y = np.array(freq)
X_train, X_test, y_train, y_test = train_test_split(X[:,None],y,test_size=0.3,
random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
plt.subplots(figsize=(15, 8))
plt.scatter(date, freq, color='lightblue')
# here's how using the actual equation so is is clear how it works
# essentially same as model.precict(X)
plt.plot(date, X*model.coef_ + model.intercept_)
Upvotes: 0
Reputation: 153460
Use model.predict
:
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pandas as pd
import matplotlib.pyplot as plt
y = np.asarray(freq)
X = np.array(date).reshape(-1,1)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
model.score(X_train, y_train)
y_pred = model.predict(X)
model.__dict__
plt.subplots(figsize=(15, 8))
plt.scatter(date, freq)
plt.plot(date, y_pred)
Output:
Upvotes: 0
Reputation: 1488
Use matplotlib.pyplot
for plotting.
import numpy as np
import matplotlib.pyplot as plt
freq_reg = intersect + slope * np.array(date, np.dtype=float)
fig, ax = plt.subplot(1, 1)
ax.scatter(date, freq)
plt.plot(date, freq_reg)
plt.set_xlabel('date')
plt.set_ylabel('frequency')
plt.show()
Upvotes: 0
Reputation: 538
import matplotlib.pyplot as plt
datemin = min(date)
datemax = max(date)
x_new = np.linspace(datemin, datemax , 100)
y_new = model.predict(x_new[:, np.newaxis])
plt.figure(figsize=(4, 3))
ax = plt.axes()
ax.scatter(date, freq)
ax.plot(x_new, y_new)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.axis('tight')
plt.show()
Check a detailed explanation here
Upvotes: 1