Pas encore de compte ?
Régression linéaire
Testé sous Anaconda et Python 3.7
import seaborn as sb from matplotlib import pyplot as plt df = sb.load_dataset('tips') sb.regplot(x = "total_bill", y = "tip", data = df) plt.show()
Testé sous Anaconda et Python 3.7
from sklearn import linear_model import numpy as np import matplotlib.pyplot as plt X=np.array([1,2,3,4,5,6,7,8,9,10 ]).reshape(-1, 1) Y=[2,4,3,6,8,9,9,10,11,13] lm = linear_model.LinearRegression() lm.fit(X, Y) plt.scatter(X, Y, color = "r",marker = "o", s = 30) y_pred = lm.predict(X) plt.plot(X, y_pred, color = "k") plt.xlabel('x') plt.ylabel('y') plt.title("Simple Linear Regression") plt.show()
Régression polynômiale
Testé sous Anaconda et Python 3.7
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns sns.set(color_codes=True) plt.rcParams["figure.figsize"] = [12,12] #plt.figure(figsize=(12,12)) np.random.seed(0) #jeu de données sous la forme y = f(x) avec f(x) = x^4 + bx^3 + c x = np.random.normal(10, 2, 500) y = x ** 4 + np.random.uniform(-1, 1,500)*(x ** 3) + np.random.uniform(0, 1,500) plt.scatter(x,y) plt.show() x = x[:, np.newaxis] y = y[:, np.newaxis] from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures polynomial_features= PolynomialFeatures(degree=4) x_poly = polynomial_features.fit_transform(x) model = LinearRegression() model.fit(x_poly, y) y_poly_pred = model.predict(x_poly) #print(r2) import operator plt.scatter(x, y, s=10) # sort the values of x before line plot sort_axis = operator.itemgetter(0) sorted_zip = sorted(zip(x,y_poly_pred), key=sort_axis) x_p, y_poly_pred_P = zip(*sorted_zip) plt.plot(x_p, y_poly_pred_P, color='g') plt.show()
Testé sous Anaconda et Python 3.7
# Importing the libraries import matplotlib.pyplot as plt def showimage(myimage, figsize=[10,10]): if (myimage.ndim>2): #This only applies to RGB or RGBA images (e.g. not to Black and White images) myimage = myimage[:,:,::-1] #OpenCV follows BGR order, while matplotlib likely follows RGB order fig, ax = plt.subplots(figsize=figsize) ax.imshow(myimage, cmap = 'gray', interpolation = 'bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.show() import pandas as pd # Importing the dataset datas = pd.read_csv('data.csv') datas X = datas.iloc[:, 1:2].values y = datas.iloc[:, 2].values # Fitting Linear Regression to the dataset from sklearn.linear_model import LinearRegression lin = LinearRegression() lin.fit(X, y) # Fitting Polynomial Regression to the dataset from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree = 4) X_poly = poly.fit_transform(X) poly.fit(X_poly, y) lin2 = LinearRegression() lin2.fit(X_poly, y) # Visualising the Linear Regression results plt.scatter(X, y, color = 'blue') plt.plot(X, lin.predict(X), color = 'red') plt.title('Linear Regression') plt.xlabel('Temperature') plt.ylabel('Pressure') plt.show() # Visualising the Polynomial Regression results plt.scatter(X, y, color = 'blue') plt.plot(X, lin2.predict(poly.fit_transform(X)), color = 'red') plt.title('Polynomial Regression') plt.xlabel('Temperature') plt.ylabel('Pressure') plt.show() # Predicting a new result with Linear Regression lin.predict(110.0) # Predicting a new result with Polynomial Regression lin2.predict(poly.fit_transform(110.0))
data.csv
sno,Temperature,Pressure
1,0,0.0002
2,20,0.0012
3,40,0.0060
4,60,0.0300
5,80,0.0900
6,100,0.2700
Régression logistique
Testé sous Anaconda et Python 3.7
import numpy as np real_beta = np.random.normal(0, 1, size=3) def sample_data(n, beta): # constructing mean mu = beta[1:3]*(-beta[0]/(np.linalg.norm(beta[1:3])**2)) # covariance is the same for each class cov = np.diag(np.ones(2)) # sampling x and adding the bias X = np.insert(np.random.multivariate_normal(mu, cov, size=n), 0, 1, axis=1) # the label is deterministic y = (np.dot(X, beta)>0)*1 return X, y X, y = sample_data(100, real_beta) import matplotlib import matplotlib.pyplot as plt matplotlib.rcParams['figure.figsize'] = (12.0, 8.0) plt.style.use('ggplot') def plot(X, y, beta=None, predictor=None, title=None): ymin_ = X[:,2].min() ymax_ = X[:,2].max() min_ = X[:,1].min() max_ = X[:,1].max() if predictor is not None: h = 0.02 xx, yy = np.meshgrid(np.arange(min_, max_, h), np.arange(ymin_, ymax_, h)) Z = predictor.predict(np.insert(np.c_[xx.ravel(), yy.ravel()], 0, 1, axis=1)) Z = Z.reshape(xx.shape) plt.pcolormesh(xx, yy, Z,shading='auto', alpha=0.01) plt.scatter(X[:,1], X[:,2], c=y) if beta is not None: x_ = np.linspace(min_, max_, 500) y_ = -beta[0]/beta[2] - x_ * beta[1] / beta[2] plt.plot(x_, y_) if title is not None: plt.title(title) plt.xlim(min_, max_) plt.ylim(ymin_, ymax_) plt.show() plot(X, y, real_beta)
Testé sous Anaconda et Python 3.7
from sklearn.datasets import load_boston from keras.models import Sequential from keras.layers import Dense, Conv1D, Flatten from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error import matplotlib.pyplot as plt boston = load_boston() x, y = boston.data, boston.target print(x.shape) x = x.reshape(x.shape[0], x.shape[1], 1) print(x.shape) xtrain, xtest, ytrain, ytest=train_test_split(x, y, test_size=0.15) model = Sequential() model.add(Conv1D(32, 2, activation="relu", input_shape=(13,1))) model.add(Flatten()) model.add(Dense(64, activation="relu")) model.add(Dense(1)) model.compile(loss="mse", optimizer="adam") model.summary() model.fit(xtrain, ytrain, batch_size=12,epochs=200, verbose=0) ypred = model.predict(xtest) print(model.evaluate(xtrain, ytrain)) print("MSE: %.4f" % mean_squared_error(ytest, ypred)) x_ax = range(len(ypred)) plt.scatter(x_ax, ytest, s=5, color="blue", label="original") plt.legend() plt.show() plt.scatter(x_ax, ytest, s=5, color="blue", label="original") plt.plot(x_ax, ypred, lw=0.8, color="red", label="predicted") plt.legend() plt.show()
Bienvenu, je m’appelle Eric Soupet et je suis l'administrateur du site elodees.com. elodees.com est un état de l'art de l'Intelligence Artificielle et se veut collaboratif, vous pouvez dès à présent proposer du contenu tels que des articles, des événements, des tutoriels, ... alors n'hésitez pas !
Crédit des images de la plate-forme : Pixabay - Pixabay License | Pexels - Pexels License