<jupyter_start><jupyter_text>Importing Libraries<jupyter_code>import pandas as pd
import numpy as np
import math
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import normalize
Forcast=pd.read_csv('/Users/Asus/Documents/InternCsv/Forcast_Official.csv')
Forcast.head(4)
#Renaming the Unnamed Column into Plants and creating and Index
Forcast = Forcast.rename(columns={'Unnamed: 0': 'Plants'})
Forcast.drop('Plants',inplace=True,axis=1)
#Forcast.head(2)
Forcast.drop('Date',inplace=True,axis=1)
Plant_Forcast=Forcast.groupby(["Plant"])
Plant_Sum=Plant_Forcast.sum()
Plant_Sum["Total_Dispatch"] = Plant_Sum.sum(axis=1)
Plant_Sum.head(5)
Plant_Sum.shape
Plant_Sum.reset_index(level=0,inplace=True)
Plant_Sum.head(3)
Plant_Sum.shape<jupyter_output><empty_output><jupyter_text>Define X & Y<jupyter_code>x=Plant_Sum.iloc[:,1:48].values
y=Plant_Sum.iloc[:,49].values
x
y<jupyter_output><empty_output><jupyter_text>Split the dataset in training Set & test Set<jupyter_code>from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)
from sklearn.linear_model import LinearRegression
ml=LinearRegression()
ml.fit(x_train,y_train)
reg= linear_model.LinearRegression()
reg.fit(Plant_Sum.iloc[:,1:48],Plant_Sum['Total_Dispatch'])
reg.coef_
reg.intercept_<jupyter_output><empty_output><jupyter_text>Evaluate Model<jupyter_code>y_pred=ml.predict(x_test)
print(y_pred)
from sklearn.metrics import r2_score
r2_score (y_test,y_pred)         #High Accurcay Rate can Be Gain From the Model<jupyter_output><empty_output><jupyter_text>Plotting Predicting Model with Actual Model<jupyter_code>import matplotlib.pyplot as plt
plt.figure(figsize=(8,5))
plt.scatter(y_test,y_pred)
myline = np.linspace(0, 10, 100)
#plt.plot(myline, my_model(myline), color ="r")
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.title('Actual Vs. Predicted')  #With The Accuracy is Very High Prediction and Actual Values are Almost Similar<jupyter_output><empty_output><jupyter_text>Model Value Differances<jupyter_code>pred_y_df=pd.DataFrame({'Actual Value':y_test,'Predicted value':y_pred,'Differance':y_test-y_pred})
pred_y_df[0:20]<jupyter_output><empty_output><jupyter_text>Data Normalization in Plant_Sum<jupyter_code>Plant_Sum = normalize(Plant_Sum.iloc[:,1:49] , axis=0) #Without using normalization first dataset importing mechanism can be used
Plant_Sum
from numpy import genfromtxt
x1=Plant_Sum[1:,0:47]
y1=Plant_Sum[1:,47]
print(x1[0:10])
print(y1[0:10])<jupyter_output>[[3.54599849e-03 1.77120693e-03 1.18516873e-03 1.78255753e-03
  1.19111467e-03 1.19434565e-03 1.19293263e-03 1.19163354e-03
  1.18041800e-03 3.57079806e-03 5.68084424e-03 2.34971631e-02
  2.76950084e-02 2.94150238e-02 2.73161014e-02 2.98801636e-02
  3.03769989e-02 3.04999714e-02 3.05094914e-02 3.04325658e-02
  3.04203222e-02 3.02994814e-02 3.01441029e-02 3.00852042e-02
  3.01618750e-02 2.97668789e-02 3.04800946e-02 3.05012717e-02
  3.04414533e-02 3.03297885e-02 3.03414581e-02 3.02666662e-02
  3.02185715e-02 3.02356261e-02 3.02827483e-02 3.02403799e-02
  2.97521541e-02 2.98137412e-02 3.11649169e-02 3.10227730e-02
  3.04612171e-02 3.09093448e-02 2.97032803e-02 2.95786224e-02
  2.72147585e-02 2.18781789e-02 1.59059563e-02]
 [9.92657952e-06 0.00000000e+00 0.00000000e+00 0.00000000e+00
  0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
  0.00000000e+00 0.00000000e+00 0.00000000e+00 6.99320331e-04
  6.93404916e-04 6.94007540e-04 6.94336521e-04 6.90055533e-04
  6.88558530e-04 6.832[...]<jupyter_text>Analyzing Training Curve in Plant_Sum<jupyter_code>def gradient(x1,y1,alpha,epoch):
  m=x1.shape[0]  #number of samples
  ones = np.ones((m,1))
  x1=np.concatenate((ones,x1),axis=1)
  n=x1.shape[1]
  Theta = np.ones(n) #n = 5th parameter
  h = np.dot(x1, Theta) #Compute Hypothesis

  #Gradient descent Algorithm
  cost = np.ones(epoch)
  for i in range (0,epoch):
      Theta[0] = Theta[0] - (alpha/ x1.shape[0]) * sum(h-y1)
      for j in range(1,n):
        Theta[j] = Theta[j] - (alpha/ x1.shape[0]) * sum((h-y1) * x1[:, j])
      h = np.dot(x1,Theta)
      cost[i] = 1/(2*m) * sum(np.square(h-y1)) #compute Cost
  return cost, Theta 
#Calcualting Theta & Cost
cost, Theta = gradient(x1, y1, 0.005, 2000)
print(Theta)<jupyter_output>[-4.27037795e-02 -2.78030322e-03 -7.01235062e-03 -9.14424421e-03
 -1.07766942e-02 -1.07528665e-02 -1.10711805e-02 -1.24159744e-02
 -1.28940119e-02 -1.23870492e-02 -1.00236121e-02 -4.88920098e-06
  1.06622033e-02  1.83829834e-02  1.66703424e-02  1.19365529e-02
  1.46054517e-02  2.09330776e-02  2.67902574e-02  2.96023916e-02
  3.11021513e-02  3.10149870e-02  3.56007260e-02  4.07416058e-02
  4.26516840e-02  4.04776422e-02  3.45633220e-02  2.86759426e-02
  2.75963285e-02  3.02790451e-02  3.41207374e-02  3.30892621e-02
  3.60989580e-02  3.78669990e-02  3.68915210e-02  3.25895204e-02
  3.03460690e-02  4.14642363e-02  7.86035263e-02  1.02159757e-01
  9.08501502e-02  7.07413664e-02  5.58070163e-02  4.01070879e-02
  2.71471409e-02  1.51860597e-02  8.29495855e-03  1.49291187e-03]<jupyter_text>Training Data Cost in Each Plants in the Forcast Table<jupyter_code>plt.plot(cost)
plt.xlabel('Number of Iterations (epoch)')
plt.ylabel("Cost or Lost")
plt.show
print("Lowest Cost ="+ str(np.min(cost)))
#Computation Cost Of Data Training
#print("Cost after 10 iterations =" + str(cost[-1]))<jupyter_output>Lowest Cost =0.0005811410646404037
StarcoderdataJupyterStructuredCleanDedup