# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import pandas as pd
from scipy import signal
from scipy import sparse
from scipy.sparse.linalg import spsolve
from scipy.linalg import svd
from numpy.linalg import multi_dot
from scipy.linalg import solve
import numpy.polynomial.polynomial as poly
import os
import csv
from sklearn.metrics import r2_score 

plt.rc('axes', labelsize=15)
plt.rc('xtick', labelsize=13)
plt.rc('ytick', labelsize=13)
plt.rc('legend', fontsize=13)
# -

csv_file_path='C:/Users/Asus/Desktop/practical/SVD/csv files/'
file_name='green_edited.csv'

datafile = open(csv_file_path+file_name, 'r')
datareader = csv.reader(datafile, delimiter=';')
data = []
for i in datareader:
    row=[]
    i = i[0].split(",")
    for j in i:
        row.append(float(j))
    data.append(row)    
#print(data)
D=np.array(data)
print(len(D))

plt.figure(figsize=(15,3))
for i in range(len(D)):
    plt.plot(D[i],label=i+1)
plt.legend()
plt.show()    



U, S, VT = svd(D)
#print(S)
print(np.shape(D))
print(np.shape(U))
print(np.shape(S))
print(np.shape(VT))

# +
plt.figure(1)
plt.semilogy(S,'*-')
#plt.title("singular values")
plt.xlabel("Element number")
plt.ylabel("Singular value")
plt.show()

selected_vals=4                                    #edit that to change significant level
sig_level=(np.sum(S[0:selected_vals])/np.sum(S))*100
print('significance level:'+str(sig_level))

sig_ar=[]
for i in range(len(S)):
    sig_level=(np.sum(S[0:i])/np.sum(S))*100
    sig_ar.append(sig_level)
    
plt.plot(range(1,len(S)+1),sig_ar,"*-")
plt.xlabel("Number of selected s values")
plt.ylabel("Significance level")
plt.show()

# +
V=np.transpose(VT)
D_new=[]

for i in range(len(D)):
    val=0
    for j in range(selected_vals):
        val+=U[i][j]*S[j]*VT[j]
        
    D_new.append(val)

print(np.shape(D_new)) 
#print(np.matrix(D_new))
# -

# ## Concentration model

U_new=U[0:len(U),0:selected_vals]
#print(np.shape(U))
#print(np.shape(U_new))
#print(U_new)


U_new_T=np.transpose(U_new)
I=np.matmul(U_new_T,U_new)
#print(I)


con_T=np.matrix([2,4,6,8,10,12,14,16,18,20,22,24,26,28,30])
#con_T=np.matrix([2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40])
#con_T=np.matrix([1,2,2.5,3,3.5,4,5])
A= np.matrix(np.dot(con_T,U_new))
print(A)
#print(np.matmul(A,I))

M1=np.dot(con_T,U_new)
print(M1[0])

M = np.linalg.lstsq(U_new, np.transpose(con_T),rcond=-1)
#print(M[0])


# ### The modle test 1

# +
x=np.matmul(A,U_new.T)
print(np.shape(x))
print(np.shape(U_new.T))
print(np.shape(A))

x0=np.matmul(U_new,M[0])
#print(x0)
#print((np.matrix(con_T)).T)

print(np.array(x)[0])
# -

plt.plot(np.array(con_T)[0],np.array(x)[0],'*')
coefs2 = poly.polyfit(np.array(con_T)[0], np.array(x)[0], 1)
ffit2 = poly.polyval(np.array(con_T)[0], coefs2)
plt.plot(np.array(con_T)[0], ffit2)
plt.ylabel("Recalculated time (days)")
plt.xlabel("Time (days)")
plt.show()
print(coefs2 )

cc2=np.corrcoef(np.array(con_T)[0],np.array(x)[0])
print(cc2)

# ### test 2



as_c=[]
for k in range(len(D)):
    con_T1=np.delete(con_T,k)
    U_new1=np.delete((U_new),k,0)
    
    A1= np.matrix(np.matmul(con_T1,U_new1))
    
    x1=np.matmul(A1,(U_new[k]).T)
    x2=np.matmul(A1,U_new1.T)
    print(x1)
    
    as_c.append(np.array(x1)[0][0])
plt.show()    
print(as_c)    

# +
plt.plot(np.array(con_T)[0],as_c,'*',label="data points")

coefs = poly.polyfit(np.array(con_T)[0], as_c, 1)
ffit = poly.polyval(np.array(con_T)[0], coefs)
plt.plot(np.array(con_T)[0], ffit,label="regression line")
plt.xlabel('Actual time (days)')
plt.ylabel('Estimated time (days)')
plt.legend()
plt.show()
print(coefs)
# -

cc=np.corrcoef(np.array(con_T)[0],as_c)
print(cc)

R_square = r2_score(as_c, ffit) 
print(R_square)



StarcoderdataJupyterScriptsDedupFiltered