030520201303
The tank prototype can really be checked in combat conditions!
https://archive.ics.uci.edu/ml/datasets/Bike+Sharing+Dataset
import torch
I’m starting a GPU graphics card (which I don’t have)
Odpalam karte graficzną GPU (której nie mam)
device = torch.device('cpu') # obliczenia robie na CPU
#device = torch.device('cuda') # obliczenia robie na GPU
import pandas as pd
df = pd.read_csv('/home/wojciech/Pulpit/3/BikeSharing.csv')
print(df.shape)
df.head(3)
cnt: count of total rental bikes including both casual and registered
I fill all holes with values out of range
Wypełniam wszystkie dziury wartościami z poza zakresu
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize=(10,6))
CORREL =df.corr()
sns.heatmap(CORREL, annot=True, cbar=False, cmap="coolwarm")
plt.title('Macierz korelacji ze zmienną wynikową y', fontsize=20)
import matplotlib.pyplot as plt
plt.figure(figsize=(10,6))
CORREL['cnt'].plot(kind='barh', color='red')
plt.title('Korelacja ze zmienną wynikową', fontsize=20)
plt.xlabel('Poziom korelacji')
plt.ylabel('Zmienne nezależne ciągłe')
Variables: 'registered’, 'casual’ are also results only shown differently, therefore they must be removed from the data.
Zmienne: 'registered’,’casual’ są to też wyniki tylko inazej pokazane dlatego trzeba je usunąć z danych.
a,b = df.shape #<- ile mamy kolumn
b
print('NUMBER OF EMPTY RECORDS vs. FULL RECORDS')
print('----------------------------------------')
for i in range(1,b):
i = df.columns[i]
r = df[i].isnull().sum()
h = df[i].count()
pr = (r/h)*100
if r > 0:
print(i,"--------",r,"--------",h,"--------",pr)
import seaborn as sns
sns.heatmap(df.isnull(),yticklabels=False,cbar=False,cmap='viridis')
#del df['Unnamed: 15']
#del df['Unnamed: 16']
df = df.dropna(how='any') # jednak je kasuje te dziury
# df.fillna(-777, inplace=True)
df.isnull().sum()
print(df.dtypes)
df.head(3)
to_datetime¶
df['dteday'] = pd.to_datetime(df['dteday'])
df['weekday'] = df.dteday.dt.weekday
df['month'] =df.dteday.dt.month
df['weekofyear'] =df.dteday.dt.weekofyear
del df['dteday']
print(df.dtypes)
df.head(3)
Encodes text values¶
Koduje wartości tekstowe
import numpy as np
a,b = df.shape #<- ile mamy kolumn
b
print('DISCRETE FUNCTIONS CODED')
print('------------------------')
for i in range(1,b):
i = df.columns[i]
f = df[i].dtypes
if f == np.object:
print(i,"---",f)
if f == np.object:
df[i] = pd.Categorical(df[i]).codes
continue
df[’Time’] = pd.Categorical(df[’Time’]).codes
df[’Time’] = df[’Time’].astype(int)
df.dtypes
df.columns
I’m cutting out an iron test reserve¶
Wycinam żelazną rezerwę testową
Wycinam 0.5% procent ostatnich rekordów które będa słuzyły do sprawdzenia zdolności prognostycznych
R,C =df.shape
F = R*0.005
L = R - F
L
df5 = df[df.index>=L]
df2 = df[df.index<L]
print('Zbiór super testowy df5:',df5.shape)
print('df2: ',df2.shape)
I specify what is X and what is y¶
Określam co jest X a co y
X = df2.drop(['cnt','registered','casual'],1)
y = df2['cnt']
X_SuperT = df5.drop(['cnt','registered','casual'],1)
y_SuperT = df5['cnt']
Scaling (normalization) of the X value¶
X should never be too big. Ideally, it should be in the range [-1, 1]. If this is not the case, normalize the input.
Skalowanie (normalizacja) wartości X
X nigdy nie powinien być zbyt duży. Idealnie powinien być w zakresie [-1, 1]. Jeśli tak nie jest, należy znormalizować dane wejściowe.
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
print(np.round(X.std(), decimals=2), np.round(X.mean(), decimals=2))
y.value_counts()
y = (y / 100) # max test score is 100
#print(y.head(3))
print(np.round(y.std(), decimals=2), np.round(y.mean(), decimals=2))
Creates random input and output¶
Tworzy losowe dane wejściowe i wyjściowe
import numpy as np
#X = X.values #- jak była normalizacja to to nie działa
X = torch.tensor(X)
print(X[:3])
X = X.type(torch.FloatTensor)
print(X[:3])
y = y.values # tworzymy macierz numpy - jak była normalizacja to to nie działa
y = torch.tensor(y)
print(y[:3])
y = y.view(y.shape[0],1)
y[:5]
y = y.type(torch.FloatTensor)
print('X:',X.shape)
print('y:',y.shape)
Dodanie jednego wymiaru do wektora wynikowego
y = y.view(y.shape[0],1)
y.shape
Podział na zbiór testowy i zbiór treningowy¶
a,b = X.shape
a
total_records = a
test_records = int(a * .2)
X_train = X[:total_records-test_records]
X_test = X[total_records-test_records:total_records]
y_train = y[:total_records-test_records]
y_test = y[total_records-test_records:total_records]
print('X_train: ',X_train.shape)
print('X_test: ',X_test.shape)
print('----------------------------------------------------')
print('y_train: ',y_train.shape)
print('y_test: ',y_test.shape)
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer
self.predict = torch.nn.Linear(n_hidden, n_output) # output layer
def forward(self, x):
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.predict(x) # linear output
return x
Definicja krztałtu sieci¶
N, D_in = X.shape
N, D_out = y.shape
H = 100
device = torch.device('cpu')
net = torch.nn.Sequential(
torch.nn.Linear(D_in, H),
torch.nn.LeakyReLU(),
torch.nn.Linear(H, H),
torch.nn.LeakyReLU(),
torch.nn.Linear(H, D_out),
).to(device)
net(X_train)
Алгоритм оптимизации:¶
Optymalizator¶
lr: Speed of learning -> The speed at which our model updates the weights in the cells each time backward propagation is carried out
lr: Szybkość uczenia się -> Szybkość, z jaką nasz model aktualizuje wagi w komórkach za każdym razem, gdy przeprowadzana jest wsteczna propagacja
#optimizer = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0, dampening=0, weight_decay=0, nesterov=False) #-2.401
#optimizer = torch.optim.SGD(net.parameters(), lr=0.1) #-4.086
optimizer = torch.optim.Adam(net.parameters(), lr=0.01) #-5.298
#optimizer = torch.optim.Adamax(net.parameters(), lr=0.01) #-6.610
#optimizer = torch.optim.ASGD(net.parameters(), lr=0.01, lambd=0.0001, alpha=0.15, t0=000000.0) #-2.315
#optimizer = torch.optim.LBFGS(net.parameters(), lr=0.01, max_iter=20, max_eval=None, tolerance_grad=1e-05, tolerance_change=1e-09, history_size=100, line_search_fn=None)
#optimizer = torch.optim.RMSprop(net.parameters(), lr=0.01, alpha=0.99, eps=1e-08) #-5.152
#optimizer = torch.optim.Rprop(net.parameters(), lr=0.01, etas=(0.5, 1.2), step_sizes=(1e-06, 50)) #R2:-7.388
Определение функции потерь¶
to jest R2 dla regresji
loss_func = torch.nn.MSELoss()
Definiowanie procesu nauki i nauka¶
inputs = X_train #1. deklarujemy x i y do nauki
outputs = y_train
for i in range(2000): #2. pętla 1050 powtórzeń (epok)
prediction = net(inputs)
loss = loss_func(prediction, outputs)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i print(i, loss.item()) # <=# wartości y, a funkcja straty zwraca Tensor zawierający stratę.
There are many potential reasons. Most likely exploding gradients. The two things to try first:¶
- Normalize the inputs
- Lower the learning rate
Istnieje wiele potencjalnych przyczyn. Najprawdopodobniej wybuchające gradienty. Dwie rzeczy do wypróbowania w pierwszej kolejności:
- – Normalizuj wejścia
- – Obniż tempo uczenia msię
import matplotlib.pyplot as plt
plt.plot(range(epochs), aggregated_losses)
plt.ylabel(’Loss’)
plt.xlabel(’epoch’)
plt.show
Forecast based on the model¶
- substitute the same equations that were in the model
- The following loss result shows the last model sequence
- Loss shows how much the model is wrong (loss = sum of error squares) after the last learning sequence
Prognoza na podstawie modelu
- podstawiamy te same równania, które były w modelu
- Poniższy wynik loss pokazuje ostatnią sekwencje modelu
- Loss pokazuuje ile myli się model (loss = suma kwadratu błedów) po ostatniej sekwencji uczenia się
with torch.no_grad():
y_pred = net(X_test)
loss = (y_pred - y_test).pow(2).sum()
print(f'Loss train_set: {loss:.8f}')
Ponieważ ustaliliśmy, że nasza warstwa wyjściowa będzie zawierać 1 neuron, każda prognoza będzie zawierać 1 wartości. Przykładowo pierwsze 5 przewidywanych wartości wygląda następująco:
y_pred[:5]
We save the whole model¶
Zapisujemy cały model
torch.save(net,'/home/wojciech/Pulpit/7/byk15.pb')
We play the whole model¶
Odtwarzamy cały model
KOT = torch.load('/home/wojciech/Pulpit/7/byk15.pb')
KOT.eval()
By substituting other independent variables, you can get a vector of output variables¶
We choose a random record from the tensor
Podstawiając inne zmienne niezależne można uzyskać wektor zmiennych wyjściowych
Wybieramy sobie jakąś losowy rekord z tensora
y_pred = y_pred*10
foka = y_pred.cpu().detach().numpy()
df11 = pd.DataFrame(foka)
df11.columns = ['y_pred']
df11=np.round(df11.y_pred)
df11.head(3)
y_test = y_test*10
foka = y_test.cpu().detach().numpy()
df_t = pd.DataFrame(foka)
df_t.columns = ['y']
df_t.head(3)
NOWA = pd.merge(df_t,df11, how='inner', left_index=True, right_index=True)
NOWA.head(3)
NOWA.to_csv('/home/wojciech/Pulpit/7/NOWA.csv')
fig, ax = plt.subplots( figsize=(16, 2))
for ewa in ['y', 'y_pred']:
ax.plot(NOWA, label=ewa)
ax.set_xlim(1340, 1500)
#ax.legend()
ax.set_ylabel('Parameter')
ax.set_title('COURSE OF THE PROJECTING PROCESS ON THE TEST SET')
## marginesy
plt.subplots_adjust( left = None , bottom = None , right = None , top = None , wspace = None , hspace = None )
plt.figure(figsize=(16,5))
ax = plt.subplot(1, 2, 1)
NOWA.plot.kde(ax=ax, legend=True, title='Histogram: y vs. y_pred')
NOWA.plot.hist(density=True,bins=40, ax=ax, alpha=0.3)
ax.set_title("Dystributions")
ax = plt.subplot(1, 2, 2)
sns.boxplot(data = NOWA)
plt.xticks(rotation=-90)
ax.set_title("Boxes")
sns.lmplot(data=NOWA, x='y', y='y_pred')
Regression_Assessment¶
## Robi ocenę tylko dla jednej zmiennej
def Regression_Assessment(y, y_pred):
from sklearn.metrics import r2_score
import scipy.stats as stats
from statsmodels.graphics.gofplots import qqplot
from matplotlib import pyplot
print('-----two methods--------------')
SS_Residual = sum((y-y_pred)**2)
SS_Total = sum((y-np.mean(y))**2)
r_squared = 1 - (float(SS_Residual))/SS_Total
adjusted_r_squared = 1 - (1-r_squared)*(len(y)-1)/(len(y)-X.shape[1]-1)
print('r2_score: #print('adjusted_r_squared:
#print('----r2_score------secound-method--------')
print('r2_score: print()
print('-------------------------------')
MAE = (abs(y-y_pred)).mean()
print('Mean absolute error MAE: RMSE = np.sqrt(((y-y_pred)**2).mean())
print('Root mean squared error RMSE: pt = (100*(y-y_pred))/y
MAPE = (abs(pt)).mean()
print('Mean absolute error MAPE: print('-------------------------------')
stat,pvalue0 = stats.ttest_1samp(a=(y-y_pred),popmean=0.0)
if pvalue0 > 0.01:
print('t-test H0: the sum of the model residuals is zero')
print('OKAY! Model remains do not differ from zero - pvalue:else:
print('Bad - Model remains DIFFERENT FROM ZERO - pvalue:print('--------------------------------------------------------------------------------------------')
stat,pvalue2_1 = stats.shapiro(y)
stat,pvalue2_2 = stats.shapiro(y_pred)
if pvalue2_1 > 0.01:
#print('Shapiro-Wilk H0: y have normal distribution?--------------------------------')
print('OK Shapiro-Wolf! y have normal distribution - pvalue:else:
print('Bad Shapiro-Wilk - y NO NORMAL DISTRIBUTION - pvalue:print('--------------------------------------------------------------------------------------------')
if pvalue2_2 > 0.01:
#print('Shapiro-Wilk: y_pred have a normal distribution?--')
print('OK Shapiro-Wolf! y_pred has a normal distribution - pvalue:else:
print('Bad Shapiro-Wilk y_pred NO NORMAL DISTRIBUTION - pvalue:qqplot(y, line='s')
pyplot.show()
qqplot(y_pred, line='s')
pyplot.show()
print('--------------------------------------------------------------------------------------------')
stat,pvalue3 = stats.kruskal(y_pred,y)
stat,pvalue4 = stats.f_oneway(y_pred,y)
if pvalue2_1 < 0.01 or pvalue2_2 < 0.01:
print('Шапиро-Вилк: Переменные не имеют нормального распределения! Не могу сделать анализ ANOV')
if pvalue3 > 0.01:
print('Kruskal-Wallis NON-PARAMETRIC TEST: whether empirical forecast and observations. have equal means?')
print('OKAY! Kruskal-Wallis H0: forecast and observations empir. have equal means - pvalue:else:
print('Bad - Kruskal-Wallis: forecast and observations empir. DO NOT HAVE EQUAL Averages - pvalue:else:
if pvalue4 > 0.01:
print('F-test (ANOVA): whether empirical forecast and observations. have equal means?--------------------------------')
print('OKAY! forecast and observations empir. have equal means - pvalue:else:
print('Bad - forecast and observations empir. DO NOT HAVE EQUAL Averages - pvalue:print('--------------------------------------------------------------------------------------------')
y = NOWA['y']
y_pred = NOWA['y_pred']
Regression_Assessment(y, y_pred)
Танк Супер Тест в боевых условиях!¶
print(X_SuperT.shape)
X_SuperT.head(3)
y_SuperT.head(3)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_SuperT = sc.fit_transform(X_SuperT)
print(np.round(X_SuperT.std(), decimals=2), np.round(X_SuperT.mean(), decimals=2))
X_SuperT = torch.tensor(X_SuperT)
X_SuperT = X_SuperT.type(torch.FloatTensor)
print(X_SuperT[:3])
y_SuperT = (y_SuperT / 100) # max test score is 100
#print(y.head(3))
print(np.round(y_SuperT.std(), decimals=2), np.round(y_SuperT.mean(), decimals=2))
y_SuperT = y_SuperT.values
y_SuperT = torch.tensor(y_SuperT)
y_SuperT = y_SuperT.view(y_SuperT.shape[0],1)
y_SuperT.shape
print('X_SuperT:',X_SuperT.shape)
print('y_SuperT:',y_SuperT.shape)
with torch.no_grad():
y_predST = net(X_SuperT)
loss = (y_predST - y_SuperT).pow(2).sum()
print(f'Loss train_set: {loss:.8f}')
y_predST = y_predST*100
foka = y_predST.cpu().detach().numpy()
df11 = pd.DataFrame(foka)
df11.columns = ['y_predST']
df11=np.round(df11.y_predST)
df11.head(3)
y_SuperT = y_SuperT*100
y_SuperT = np.round(y_SuperT)
foka = y_SuperT.cpu().detach().numpy()
df_t = pd.DataFrame(foka)
df_t.columns = ['y_ST']
df_t.head(3)
Super_NOWA = pd.merge(df_t,df11, how='inner', left_index=True, right_index=True)
Super_NOWA.head(3)
fig, ax = plt.subplots( figsize=(16, 2))
for ewa in ['y_ST', 'y_predST']:
ax.plot(Super_NOWA, label=ewa)
#ax.set_xlim(1340, 1500)
#ax.legend()
ax.set_ylabel('Parameter')
ax.set_title('COURSE OF THE PROJECTING PROCESS ON THE TEST SET')
## marginesy
plt.subplots_adjust( left = None , bottom = None , right = None , top = None , wspace = None , hspace = None )
plt.figure(figsize=(16,5))
ax = plt.subplot(1, 2, 1)
Super_NOWA.plot.kde(ax=ax, legend=True, title='Histogram: y vs. y_pred')
Super_NOWA.plot.hist(density=True,bins=40, ax=ax, alpha=0.3)
ax.set_title("Dystributions")
ax = plt.subplot(1, 2, 2)
sns.boxplot(data = Super_NOWA)
plt.xticks(rotation=-90)
ax.set_title("Boxes")
sns.lmplot(data=Super_NOWA, x='y_ST', y='y_predST')