<jupyter_start><jupyter_text><jupyter_code># Projet perso ML - <NAME>

# Week 2 pour linear regression
# Week 3 pour random forest

import pandas as pd
from google.colab import files
import io

uploaded = files.upload()
# data1 = pd.read_mat(io.bytesIO(uploaded['LGS1_2021.mat']))
import pandas as pd
#data1  = io.loadmat('LGS1_2021.mat')
#data2  = io.loadmat('LGS2_2021.mat')

data1 = pd.read_excel(io.BytesIO(uploaded['LGS1_2021.xlsx']))
data2 = pd.read_excel(io.BytesIO(uploaded['LGS2_2021.xlsx']))

#T = data1 [:,0]
#Q = data1 [:,1]
#C = data1 [:,3]
#QC = data1 [:,5]

#T2 = data2 [:,0]
#Q2 = data2 [:,1]
#C2 = data2 [:,3]
#QC2 = data2 [:,5]

print(data1)
print(data2)
# Normalized input variables

from sklearn.preprocessing import MinMaxScaler

# MIN MAX SCALING
minmax_scale = MinMaxScaler().fit(data1[['C','Q']])
df_minmax = minmax_scale.transform(data1[['C', 'Q']])

print('\n********** Normalisation1*********\n')
 
print('Moyenne apres le Min max Scaling :\nC={:.2f}, Q={:.2f}'
.format(df_minmax[:,0].mean(), df_minmax[:,1].mean()))
 
print('\n')
 
print('Valeur minimale et maximale pour la feature C apres min max scaling: \nMIN={:.2f}, MAX={:.2f}'
.format(df_minmax[:,0].min(), df_minmax[:,0].max()))
 
print('\n')
 
print('Valeur minimale et maximale pour la feature Q apres min max scaling : \nMIN={:.2f}, MAX={:.2f}'
.format(df_minmax[:,1].min(), df_minmax[:,1].max()))


minmax_scale2 = MinMaxScaler().fit(data2[['C2','Q2']])
df_minmax2 = minmax_scale.transform(data2[['C2', 'Q2']])

print('\n********** Normalisation2*********\n')
 
print('Moyenne apres le Min max Scaling :\nC2={:.2f}, Q2={:.2f}'
.format(df_minmax2[:,0].mean(), df_minmax2[:,1].mean()))
 
print('\n')
 
print('Valeur minimale et maximale pour la feature C2 apres min max scaling: \nMIN={:.2f}, MAX={:.2f}'
.format(df_minmax2[:,0].min(), df_minmax2[:,0].max()))
 
print('\n')
 
print('Valeur minimale et maximale pour la feature Q2 apres min max scaling : \nMIN={:.2f}, MAX={:.2f}'
.format(df_minmax2[:,1].min(), df_minmax2[:,1].max()))
# splitting the data into train, test, valid
from sklearn.model_selection import train_test_split
import numpy as np

# set % of data to be used for training
train_size=.80

# set % of data to be used for testing
test_size=.20

# set % of data to be used for testing
valid_size=.10

rnd_seed = 42

X = data2[['C2']]
y = data2[['Q2']]

X_st_dev = np.std(X)
y_st_dev = np.std(y)
print("Standard deviation of the given list: " + str(X_st_dev))
print("Standard deviation of the given list: " + str(y_st_dev))

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = train_size, test_size = test_size, random_state = rnd_seed)
#X_val, X_test, y_val, y_test = train_test_split(mnist.data, mnist.target, test_size=1000, train_size = 1000)
#X_val, X_test, y_val, y_test = train_test_split(X, y, mnist.target, test_size=1000, train_size = 1000)

#X_train
#X_test

import numpy as np 
import matplotlib.pyplot as plt 
  
from sklearn.linear_model import LinearRegression 
from sklearn.metrics import mean_squared_error, r2_score 
import statsmodels.api as sm

n = np.size(X) 
  
X_mean = np.mean(X) 
y_mean = np.mean(y) 
X_mean,y_mean 
  
Sxy = np.sum(X*y)- n*X_mean*y_mean 
Sxx = np.sum(X*X)-n*X_mean*X_mean 
  
b1 = Sxy/Sxx 
b0 = y_mean-b1*X_mean 

y_pred = b1 * X + b0 

error = y - y_pred 
se = np.sum(error**2) 
print('squared error is', se) 
  
mse = se/n   
rmse = np.sqrt(mse) 
print('root mean square error is', rmse) 
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

# Instantiate the model
lin_reg = LinearRegression()

# Fit the model
lin_reg.fit(X, y)

#Instantiate a figure having size 13,6
fig, ax = plt.subplots(figsize = (13,6))
ax.scatter(X, y, c='black')

reg_x = np.linspace(0,10).reshape(-1,1)
reg_y = lin_reg.predict(reg_x)

ax.plot(reg_x,reg_y)
#ax.autoscale(axis='x', tight=True)
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Train the classifiers on the training set
RFC = RandomForestClassifier()
RFC.fit(X_train,y_train)

# Calculate the prediction of the classifier over my test set
RFC_pred = RFC.predict(X_test)

print(accuracy_score(y_test, RFC_pred))<jupyter_output>/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:6: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples,), for example using ravel().