-
Notifications
You must be signed in to change notification settings - Fork 0
/
531_f.py
142 lines (109 loc) · 4.12 KB
/
531_f.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
"""531_F.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1V0eZs6vFKkLZRCbT7NusL5xe2dUsQtvW
"""
#Importing the libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# import warnings
import warnings
warnings.filterwarnings("ignore")
# We will use some methods from the sklearn module
from sklearn import linear_model
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.model_selection import train_test_split, cross_val_score
# Reading the Dataset
df = pd.read_csv("531.csv")
df.head()
df.shape
#Setting the value for X and Y
df.drop(['dtlb_load_misses.walk_pending:u.1'],axis=1,inplace = True)
df.drop_duplicates(inplace = True)
X = df[['branch-misses:u', 'cache-misses:u', 'L1-dcache-load-misses', 'L1-icache-load-misses', 'LLC-load-misses', 'LLC-store-misses', 'branch-load-misses', 'dTLB-load-misses', 'dTLB-store-misses', 'iTLB-load-misses', 'l2_rqsts.code_rd_miss:u', 'l2_rqsts.demand_data_rd_miss:u', 'l2_rqsts.all_demand_miss:u', 'dtlb_load_misses.walk_pending:u', 'itlb_misses.walk_pending:u', 'dtlb_store_misses.walk_pending:u', 'offcore_requests.l3_miss_demand_data_rd:u', 'ocr.hwpf_l2_rfo.l3_miss:u', 'ocr.demand_data_rd.l3_miss:u', 'icache_64b.iftag_miss:u']]
i = df['instructions:u'].values.reshape(-1,1)
y = df['CPI']
X = np.divide(X,i)
df_new = X.copy()
df_new['CPI'] = y
df_new.head()
corr = df_new.corr()
plt.figure (figsize = (15,8))
sns.heatmap(corr)
X.drop(['LLC-store-misses','branch-load-misses','cache-misses:u','dTLB-load-misses','dTLB-store-misses','offcore_requests.l3_miss_demand_data_rd:u','ocr.hwpf_l2_rfo.l3_miss:u','l2_rqsts.all_demand_miss:u','iTLB-load-misses','dtlb_store_misses.walk_pending:u','itlb_misses.walk_pending:u'],axis=1,inplace = True)
X.head()
from sklearn.feature_selection import mutual_info_regression
import numpy as np
mi = mutual_info_regression(X, y)
print(mi)
#Fitting the Multiple Linear Regression model
mlr = LinearRegression()
#Splitting dataset
X_train,X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 100)
mlr.fit(X_train,y_train)
X_test.head()
#Intercept and Coefficient
print("Intercept: ", mlr.intercept_)
print("Coefficients:")
list(zip(X, mlr.coef_))
#Prediction of test set
y_pred_mlr= mlr.predict(X_test)
#Predicted values
print("Prediction for test set: {}".format(y_pred_mlr))
#Actual value and the predicted value
mlr_diff = pd.DataFrame({'Actual value': y_test, 'Predicted value': y_pred_mlr})
mlr_diff.head()
#Model Evaluation
from sklearn import metrics
meanAbErr = metrics.mean_absolute_error(y_test, y_pred_mlr)
meanSqErr = metrics.mean_squared_error(y_test, y_pred_mlr)
rootMeanSqErr = np.sqrt(metrics.mean_squared_error(y_test, y_pred_mlr))
r2 = mlr.score(X_test,y_test)
print('R squared: {:.2f}'.format(r2*100))
print('Mean Absolute Error:', meanAbErr)
print('Mean Square Error:', meanSqErr)
print('Root Mean Square Error:', rootMeanSqErr)
final = X.mean()
print(final)
R = np.multiply(final,mlr.coef_)
print(R)
print(R.sum()+mlr.intercept_)
finaly = y.mean()
print(finaly)
n = df_new.shape[0]
p = X.shape[1]
print(n,p)
#Adjusted r2 score
r2adj = 1-(1-r2)*(n-1)/(n-p-1)
print(r2adj*100)
#Residuals (added abs for now)
residuals = (y_test - y_pred_mlr)
print(residuals)
#F statistic
fstat = ((r2)/(1-r2))/(p/(n-p-1))
print(fstat)
#p-value
from scipy.stats import f
p_value = 1-f.cdf(fstat,p,n-p-1)
print(p_value)
import pandas as pd
R = pd.concat([pd.Series([mlr.intercept_],index = ['Base CPI']), R])
print(R)
sns.set(style="whitegrid")
fig, ax = plt.subplots(figsize =(5,5))
sns.residplot(x=y_pred_mlr,y=residuals,ax=ax, lowess=True, line_kws={"color": "red"})
ax.set(ylabel='Residuals',xlabel='Predicted values')
fig, ax = plt.subplots()
groups = ['']
plt.figure (figsize = (2,5))
# Stacked bar chart with loop
for i in range(len(R)):
plt.bar(groups, R[i],label = R.index[i], bottom = np.sum(R[:i], axis = 0))
plt.legend(bbox_to_anchor = (1.25, 0.6), loc='upper left')
plt.tight_layout()