Assignment 4 (1)

py

School

Stevens Institute Of Technology *

*We aren’t endorsed by this school

Course

653

Subject

Electrical Engineering

Date

Apr 3, 2024

Type

py

Pages

8

Uploaded by aryan2580

Report
#!/usr/bin/env python # coding: utf-8 # # Submitted by: REVATI C. # In[ ]: # 1.0 Linear Regression # In[99]: import numpy as np class Linear_regression(): def __init__(self, x, y, m, c, epochs, L=0.001): #Setting default variables self.x = x self.y = y self.m = np.zeros((x.shape[1], 1)) self.c = c self.epochs = epochs self.L = L def gradient_descent(self): #Running LR based on given formula. for a in range(self.epochs): delta_m = -np.dot(self.x.T, (self.y - np.dot(self.x, self.m) - self.c)) delta_c = -(self.y - np.dot(self.x, self.m) - self.c) self.m = self.m - (self.L*delta_m) self.c = self.c - (self.L*delta_c) def predict(self,x_new): #Predicting based on the given data. output = [] output = np.dot(x_new, self.m) + self.c return output # In[100]:
N = 10 M = 5 x = np.random.rand(N, M) y = np.random.rand(N, 1) x_1 = np.random.rand(N, M) lr = Linear_regression(x, y, 0, 0, 100) lr.gradient_descent() print(lr.predict(x_1)) # In[ ]: # In[101]: #2.0 Credit Transaction data # In[4]: import csv import pandas as pd # In[8]: CT = pd.read_csv('C:/Users/Admin/Downloads/Homework4_Dataset (1)/res_purchase_2014.csv', low_memory=False) CT.head() # In[104]:
# 2.1 What is total amount spending captured in this dataset? # In[105]: # Clean the dataset CT['Amount'] = CT['Amount'].str.replace(',', '') CT['Amount'] = CT['Amount'].str.replace('$', '') CT['Amount'] = CT['Amount'].str.replace('zero','') CT['Amount'] = CT['Amount'].str.replace("(","").str.replace(")","") CT['Amount'] # In[106]: CT.dtypes # In[107]: CT['Amount'].value_counts() # In[108]: CT["Amount"] = CT['Amount'].astype('float') # In[109]: CT.dtypes # In[110]: CT.Amount.sum()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
# In[111]: #2.2 How much was spend at WW GRAINGER? Grainer_Total = CT[CT['Vendor'] =='WW GRAINGER'] ['Amount'].astype(float).sum() Grainer_Total # Hence, the total amount for Grainger is 5089417.48 # In[112]: #2.3 How much was spend at WM SUPERCENTER? SuperCenter_Total= CT[CT['Vendor'] =='WM SUPERCENTER'] ['Amount'].astype(float).sum() SuperCenter_Total # In[113]: #2.4 How much was spend at GROCERY STORES? GROCERY_Total= CT[CT['Merchant Category Code (MCC)'] == 'GROCERY STORES,AND SUPERMARKETS']['Amount'].astype(float).sum() GROCERY_Total # In[9]: #Q3] #3.1] Read and as BalanceSheet and Ratings(dataframe). import pandas as pd BalanceShet= pd.read_excel('C:/Users/Admin/Downloads/Homework4_Dataset (1)/Energy.xlsx') BalanceShet.head(5)
# In[10]: Rating= pd.read_excel('C:/Users/Admin/Downloads/Homework4_Dataset (1)/EnergyRating.xlsx') Rating.head(5) # In[116]: #3.3]For BalanceSheet, drop the column if more than 30% value in this colnmn is 0,see how many features are remaining. Rating.dtypes # In[117]: Rating.info() # In[118]: BalanceShet.shape[0] # In[119]: Var =round(BalanceShet.isnull().sum()/BalanceShet.shape[0]*100,2)>30 # In[120]: Balancesheet_new = BalanceShet.loc[:,Var] # In[121]:
Balancesheet_new # In[122]: Balancesheet_new.info() # In[123]: Var_2 = Balancesheet_new[Balancesheet_new == 0].sum()/Balancesheet_new.shape[0]*100<90 # In[124]: Var_2 # In[125]: Balancesheet_new = Balancesheet_new.loc[:,Var_2] # In[126]: Balancesheet_new # In[127]: Balancesheet_new.info() # In[128]: Balancesheet_new
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
# In[129]: Balancesheet_new # In[130]: Balancesheet_new1 = Balancesheet_new.fillna(Balancesheet_new.mean()) # In[131]: Balancesheet_new1 # # Q6. # In[132]: matrix = Balancesheet_new1.corr() # In[133]: matrix # In[134]: Matched = merge(Rating, Balancesheet_new1, on=x, how=) # In[ ]: Matched