Created
September 25, 2023 01:41
-
-
Save eduardvercaemer/ad99bf6dfd9625da73b9cbdfcc5c9a9f to your computer and use it in GitHub Desktop.
Fish data linear regression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# **Regresión Lineal Multivariable** | |
import numpy as np | |
import pandas as pd | |
from sklearn.linear_model import LinearRegression | |
from sklearn.metrics import mean_squared_error, r2_score | |
#from sklearn.preprocessing import PolynomialFeatures | |
Ahora, la descripción de la venta de coches se realizó por medio del número de la semana y el día de la semana (dos variables) | |
**Set de datos** | |
data = pd.read_csv('Fish.csv') | |
train_size = int(data.shape[0]*0.8) | |
train_data = data.iloc[:train_size] | |
test_data = data.iloc[train_size:] | |
X = train_data[['Length1', 'Length2', 'Length3', 'Height', 'Width']] | |
Y = train_data[['Weight']] | |
data = pd.read_csv('Fish.csv') | |
num_rows = data.shape[0] | |
random_indices = np.random.permutation(num_rows) | |
data_shuffled = data.iloc[random_indices] | |
data_shuffled = data_shuffled.reset_index(drop=True) | |
train_size = int(data_shuffled.shape[0]*0.8) | |
train_data = data_shuffled.iloc[:train_size] | |
test_data = data_shuffled.iloc[train_size:] | |
X = train_data[['Length1', 'Length2', 'Length3', 'Height', 'Width']] | |
Y = train_data[['Weight']] | |
**Modelo** | |
model = LinearRegression() | |
**Entrenamiento** | |
model.fit(X, Y) | |
**Evaluación** | |
r_sq = model.score(X, Y) | |
print('Coeficiente de determinación:', r_sq) | |
print('Intercepción:', model.intercept_) | |
print('Pendiente:', model.coef_) | |
# **Predicciones** | |
Xt = test_data[['Length1', 'Length2', 'Length3', 'Height', 'Width']] | |
y_pred = model.predict(Xt) | |
print('Predicción:', y_pred) | |
y_pred = model.intercept_ + np.sum(model.coef_ * X, axis=1) | |
print('Predicción = interception + pendiente*X:', y_pred) | |
# **Predicción con un dato nuevo** | |
x_new = np.array([[23.9, 26.5, 31.1, 12.3778, 4.6961]]) | |
y_new = model.predict(x_new) | |
print(y_new) | |
print('Error cuadrático medio: %.2f' | |
% mean_squared_error(Y['Weight'], y_pred)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment