Skip to content

Instantly share code, notes, and snippets.

@prashdash112
Created March 14, 2021 07:06
Show Gist options
  • Save prashdash112/a230cc736fd609864807fff9002efdf6 to your computer and use it in GitHub Desktop.
Save prashdash112/a230cc736fd609864807fff9002efdf6 to your computer and use it in GitHub Desktop.
df=pd.read_excel(r'C://Users//Prashant//Desktop//airfoil_new.xlsx')
columns = ['Frequency','Angle','Chord length','Free-stream velocity','Suction side displacement thickness','Scaled sound pressure level']
df.columns=columns
class LassoRegression():
def __init__( self, lr, iterations, l1_penality ):
self.lr = lr
self.iterations = iterations
self.l1_penality = l1_penality
# Function for model training
def fit( self, X, Y ):
self.m, self.n = X.shape #rows,columns
# weight initialization
self.W = np.zeros( self.n )
self.b = 0
self.X = X
self.Y = Y
# optimization learning using the helper function
for i in range( self.iterations ):
self.update_weights()
return self
# Helper function to update weights in gradient descent
def update_weights( self ):
Y_pred = self.predict(self.X)
dW = np.zeros( self.n ) # zero array of the size of columns of training seyt
for j in range( self.n ):
if self.W[j] > 0:
#calculating the differential of the expression for weight values>0
dW[j] = (-(2*(self.X.iloc[:, j]).dot(self.Y - Y_pred))+self.l1_penality)/self.m
else :
#calculating the differential of the expression for weight values<0
dW[j] = ( - ( 2 * ( self.X.iloc[:,j] ).dot( self.Y - Y_pred ) ) - self.l1_penality ) / self.m
db = - 2 * np.sum( self.Y - Y_pred )/self.m #taking the differential of the error function
# update weights
self.W = self.W - self.lr * dW
self.b = self.b - self.lr * db
return self
# prediction function
def predict( self, X ):
return X.dot( self.W ) + self.b
if __name__ == '__main__':
model = LassoRegression( iterations = 1000, lr = 0.01, l1_penality = 50 )
model.fit( X_train, y_train )
Y_pred = model.predict( X_test )
print( "Predicted values ", np.round( Y_pred, 2 ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment