<jupyter_start><jupyter_text>Linear Regression with NumPy and Python  Task 1 - Load the Data and Libraries<jupyter_code>import matplotlib.pyplot as plt
plt.style.use('ggplot')
%matplotlib inline
import numpy as np
import pandas as pd
import seaborn as sns
plt.rcParams['figure.figsize']=(12,8)
data = pd.read_csv("food_truck_data.txt")
data.head()
data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 97 entries, 0 to 96
Data columns (total 2 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   Population  97 non-null     float64
 1   Profit      97 non-null     float64
dtypes: float64(2)
memory usage: 1.6 KB<jupyter_text>Task 2 - Visualize the Data<jupyter_code>ax = sns.scatterplot(x="Population",y="Profit",data=data)
ax.set_title("Profit in $10000s vs City Population in 10000s")<jupyter_output><empty_output><jupyter_text>Task 3 - Compute the J(𝜃)The objective of linear regression is to minimize the cost function$$J(\theta) = \frac{1}{2m} \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)} )^2$$where $h_{\theta}(x)$ is the hypothesis and given by the linear model$$h_{\theta}(x) = \theta^Tx = \theta_0 + \theta_1x_1$$where : $h_\theta(x^{(i)})$ -> estimate profit of model    $y^{(i)}$ -> actual profit of model<jupyter_code>def cost_function(x,y,theta):
    m = len(y)
    y_pred = x.dot(theta)
    error = (y_pred-y)**2
    return 1/(2*m)*np.sum(error)
m = data.Population.values.size
# add another dimension to accomodate the intercept terms and set it to all ones
X = np.append(np.ones((m,1)),data.Population.values.reshape(m,1),axis=1)
y = data.Profit.values.reshape(m,1)
theta = np.zeros((2,1))
cost_function(X,y,theta)<jupyter_output><empty_output><jupyter_text>Task 4 - Greadient DescentMinimize the cost function $J(\theta)$ by updating the below equation and repeat unitil convergence        $\theta_j := \theta_j - \alpha \frac{1}{m} \sum_{i=1}^m (h_{\theta}(x^{(i)}) - y^{(i)})x_j^{(i)}$ (simultaneously update $\theta_j$ for all $j$).<jupyter_code>def gradient_descent(X,y,theta,alpha,iterations):
    m = len(y)
    costs = []
    for i in range(iterations):
        y_pred = X.dot(theta)
        error = np.dot(X.transpose(),(y_pred-y))
        theta -= alpha *1/m * error
        costs.append(cost_function(X,y,theta))
    return theta, costs
theta, costs = gradient_descent(X,y,theta,alpha=0.01,iterations=1000)
print("h(x) = {} + {}x1".format(str(round(theta[0,0],2)),str(round(theta[1,0],2))))
costs[999]<jupyter_output><empty_output><jupyter_text>Task 5 - Visualising the Cost Function J(𝜃)<jupyter_code>from mpl_toolkits.mplot3d import Axes3D
theta_0 = np.linspace(-10,10,100)
theta_1 = np.linspace(-1,4,100)

cost_values = np.zeros((len(theta_0),len(theta_1)))

for i in range(len(theta_0)):
    for j in range(len(theta_1)):
        t = np.array([theta_0[i],theta_1[j]])
        cost_values[i,j] = cost_function(X,y,t)
fig = plt.figure(figsize = (12,8))
ax = fig.gca(projection = '3d')

surf = ax.plot_surface(theta_0,theta_1,cost_values,cmap='viridis',linewidth=0.2)
fig.colorbar(surf,shrink=0.5,aspect=5)

plt.xlabel("$\Theta_0$")
plt.ylabel("$\Theta_1$")
ax.set_zlabel("$J(\Theta)$")
ax.set_title("Cost Surface")
ax.view_init(30,300)

plt.show()<jupyter_output>C:\Users\chakr\AppData\Local\Temp/ipykernel_17088/4141944805.py:2: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  ax = fig.gca(projection = '3d')<jupyter_text>Task 6 - Plotting the ConvergencePlot J(𝜃) against the number of iterations of gradient descent<jupyter_code>plt.plot(costs)
plt.xlabel("Iterations")
plt.ylabel("$J(\Theta)$")
plt.title("Values of Cost Function over iterations of Gradient Descent")<jupyter_output><empty_output><jupyter_text>Task 8 - Training Data with Linear Regression Fit<jupyter_code>theta.shape
theta
theta = np.squeeze(theta)
sns.scatterplot(x="Population",y="Profit",data=data)

x_value = [x for x in range(5,25)]
y_value = [(x*theta[1]+theta[0]) for x in x_value]
sns.lineplot(x_value,y_value)

plt.xlabel("Population in 10000s")
plt.ylabel("Profit in $10,000s")
plt.title("Linear Regression Fit");<jupyter_output>C:\Users\chakr\anaconda3\lib\site-packages\seaborn\_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  warnings.warn(<jupyter_text>Task 9 - Inference using the Optimized 𝜃 Values$h_\theta(x) = \theta^Tx$<jupyter_code>def predict(x,theta):
    y_pred = np.dot(theta.transpose(),x)
    return y_pred
y_pred_1 = predict(np.array([1,4]),theta)*10000
print(f"For a population of 40,000, the model predicts a profit of ${str(round(y_pred_1,0))}")
y_pred_2 = predict(np.array([1,8.3]),theta)*10000
print(f"For a population of 83,000, the model predicts a profit of ${str(round(y_pred_2,0))}")<jupyter_output>For a population of 83,000, the model predicts a profit of $60243.0
{'fraction_non_alphanumeric': 0.15208411565903115, 'fraction_numerical': 0.03755163349605708, 'mean_word_length': 5.245017584994138, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '420042', 'license': 'MIT', 'n_tokens_mistral': 2090, 'n_tokens_neox': 1951, 'n_words': 460, 'path': 'linear_regression_with_numpy_and_python.ipynb', 'repo_name': 'abhinav-chakravarty/linear-regression-with-numpy-and-python', 'size': '5326'}