Fiches Mémo Data Science
L'essentiel de NumPy, Pandas, Matplotlib et Scikit-Learn en un coup d'œil.
NumPy
Création
import numpy as np
arr = np.array([1, 2, 3])
zeros = np.zeros((3, 3))
ones = np.ones((2, 5))
range = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
rand = np.random.rand(3, 3)
Inspection
arr.shape # Dimensions (ex: (3, 3))
arr.dtype # Type des données (ex: int64)
arr.ndim # Nombre de dimensions
Opérations
arr + 5 # Ajoute 5 à tout le tableau
arr * 2 # Multiplie tout par 2
np.mean(arr) # Moyenne
np.sum(arr) # Somme
Pandas
DataFrame
import pandas as pd
df = pd.read_csv('file.csv')
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
Inspection
df.head() # 5 premières lignes
df.info() # Infos types et nulls
df.describe() # Stats descriptives
df.shape # (lignes, colonnes)
Sélection
df['col'] # Sélectionne une colonne
df[['A', 'B']] # Sélectionne plusieurs colonnes
df.iloc[0] # Première ligne (par position)
df.loc[df['Age'] > 25] # Filtrage conditionnel
Visualisation
Matplotlib
import matplotlib.pyplot as plt
plt.plot(x, y) # Ligne
plt.scatter(x, y) # Points
plt.hist(data) # Histogramme
plt.title('Titre')
plt.xlabel('X')
plt.show()
Seaborn
import seaborn as sns
sns.scatterplot(data=df, x='A', y='B')
sns.histplot(data=df, x='A')
sns.boxplot(data=df, x='Cat', y='Val')
Scikit-Learn
Préparation
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
Modélisation
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train) # Entraînement
preds = model.predict(X_test) # Prédiction
Évaluation
from sklearn.metrics import accuracy_score, mean_squared_error
acc = accuracy_score(y_test, preds)
mse = mean_squared_error(y_test, preds)