# -*- coding: utf-8 -*-
"""
Created on Sun Feb 24 21:24:24 2019

@author: Abdussamet
"""

#1. kutuphaneler
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import r2_score
import statsmodels.api as sm

# veri yukleme
veriler = pd.read_csv('maaslar_yeni.csv')

x = veriler.iloc[:,2:5]
y = veriler.iloc[:,5:]
X = x.values
Y = y.values

print(veriler.corr())#colonlar arsındaki bağlantıyı gösteren bir köşegen matris basar

#linear regression
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X,Y)

print("linear ols:")
model = sm.OLS(lin_reg.predict(X), X)#X tahminlerini al, X ile karşılaştır
print(model.fit().summary())

print("Linear R2 degeri:")
print(r2_score(Y, lin_reg.predict((X))))

#polynomial regression
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
x_poly = poly_reg.fit_transform(X)
print(x_poly)
lin_reg2 = LinearRegression()
lin_reg2.fit(x_poly,y)


#tahminler

print("poly ols:")
model2 = sm.OLS(lin_reg2.predict(poly_reg.fit_transform(X)), X)
print(model2.fit().summary())

print("Polynomial R2 degeri:")
print(r2_score(Y, lin_reg2.predict(poly_reg.fit_transform(X)) ))


#verilerin olceklenmesi
from sklearn.preprocessing import StandardScaler

sc1 = StandardScaler()
x_olcekli = sc1.fit_transform(X)
sc2 = StandardScaler()
y_olcekli = sc2.fit_transform(Y)

from sklearn.svm import SVR

svr_reg = SVR(kernel = 'rbf')
svr_reg.fit(x_olcekli,y_olcekli)

print("svr ols:")
model3 = sm.OLS(svr_reg.predict(x_olcekli), x_olcekli)
print(model3.fit().summary())

print("SVR R2 degeri:")
print(r2_score(y_olcekli, svr_reg.predict(x_olcekli)) )


#Decision Tree Regression
from sklearn.tree import DecisionTreeRegressor
r_dt = DecisionTreeRegressor(random_state=0)
r_dt.fit(X,Y)

print("dt ols:")
model4 = sm.OLS(r_dt.predict(X), X)
print(model4.fit().summary())

print("Decision Tree R2 degeri:")
print(r2_score(Y, r_dt.predict(X)) )



#Random Forest Regresyonu
from sklearn.ensemble import RandomForestRegressor
rf_reg = RandomForestRegressor(n_estimators = 10, random_state=0)
rf_reg.fit(X,Y)

print("rf ols:")
model5 = sm.OLS(rf_reg.predict(X), X)
print(model5.fit().summary())

print("Random Forest R2 degeri:")
print(r2_score(Y, rf_reg.predict(X)) )

#ozett R2 degerleri
print('----------------')
print("Linear R2 degeri:")
print(r2_score(Y, lin_reg.predict((X))))


print("Polynomial R2 degeri:")
print(r2_score(Y, lin_reg2.predict(poly_reg.fit_transform(X)) ))


print("SVR R2 degeri:")
print(r2_score(y_olcekli, svr_reg.predict(x_olcekli)) )


print("Decision Tree R2 degeri:")
print(r2_score(Y, r_dt.predict(X)) )

print("Random Forest R2 degeri:")
print(r2_score(Y, rf_reg.predict(X)) )