Untitled

 avatar
unknown
plain_text
a month ago
909 B
2
Indexable
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import mean_squared_error
np.random.seed(42)
X = np.random.rand(100, 10)
y = np.dot(X, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + np.random.randn(100) * 0.5
X = np.unique(X, axis=0)
y = y[:len(X)]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
train_error = mean_squared_error(y_train, model.predict(X_train))
test_error = mean_squared_error(y_test, model.predict(X_test))
print("Training error:", train_error)
print("Testing error:", test_error)
num_splits = 10
cv_scores = cross_val_score(model, X, y, cv=num_splits, scoring='neg_mean_squared_error')
bias = -np.mean(cv_scores)
variance = np.var(cv_scores)
print("Bias:", bias)
print("Variance:", variance)
Leave a Comment