-
Notifications
You must be signed in to change notification settings - Fork 0
/
525_f_trial2.py
145 lines (112 loc) · 4.54 KB
/
525_f_trial2.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
143
144
145
# -*- coding: utf-8 -*-
"""525_F_trial2.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1FV4EV8bcSOACO2fHRk6unylVJ0iLGXOB
"""
#Importing the libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
# 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("525_1310.csv")
df = df.iloc[100:]
df = df.iloc[:-100]
df.head()
df.shape
print(df.columns.values.tolist())
#Setting the value for X and Y
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', 'dtlb_load_misses.walk_pending:u.1', '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', 'l2_rqsts.swpf_miss:u', 'page-faults:u', 'mem-stores:u', 'l1d_pend_miss.pending:u', 'frontend_retired.itlb_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
corr = df_new.corr()
plt.figure (figsize = (20,8))
sns.heatmap(corr, annot = True)
X.drop(['l2_rqsts.all_demand_miss:u','dTLB-load-misses','ocr.demand_data_rd.l3_miss:u','offcore_requests.l3_miss_demand_data_rd:u','dtlb_store_misses.walk_pending:u','LLC-load-misses','cache-misses:u','l1d_pend_miss.pending:u','dtlb_load_misses.walk_pending:u','LLC-store-misses','l2_rqsts.code_rd_miss:u','dtlb_load_misses.walk_pending:u.1','L1-dcache-load-misses','branch-load-misses','dTLB-store-misses','itlb_misses.walk_pending:u','ocr.hwpf_l2_rfo.l3_miss:u','l2_rqsts.swpf_miss:u'],axis=1,inplace = True)
X.head()
#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)
plt.scatter(y_train,X_train.iloc[:,0].values)
plt.show()
mlr.fit(X_train,y_train)
#Intercept and Coefficient
print("Intercept: ", mlr.intercept_)
print("Coefficients:")
list(zip(X, mlr.coef_))
"""'LLC-store-misses','L1-icache-load-misses','l2_rqsts.code_rd_miss:u','dtlb_load_misses.walk_pending:u1'"""
#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)
y_mean = y.mean()
print(y_mean)
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
residuals = (y_test - y_pred_mlr)
print(residuals)
#F statistic
fstat = (r2/(1-r2))*((n-p-1)/p)
print(fstat)
print(n-p-1)
print(p)
# 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()