๐ค Scikit-Learn Linear Regression
Linear Regression is the 'Hello World' of Machine Learning. Scikit-Learn makes it a breeze to implement.
Mastering this concept will significantly boost your Python data science skills!
๐ป Code Example:
from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, r2_score from sklearn.preprocessing import StandardScaler import numpy as np # 1. Generate synthetic pynfinity study dataset np.random.seed(42) hours_studied = np.random.uniform(1, 12, 200).reshape(-1, 1) scores = 5 * hours_studied.flatten() + np.random.normal(0, 5, 200) + 40 # 2. Split into train and test sets (80/20) X_train, X_test, y_train, y_test = train_test_split( hours_studied, scores, test_size=0.2, random_state=42 ) # 3. Feature scaling scaler = StandardScaler() X_train_sc = scaler.fit_transform(X_train) X_test_sc = scaler.transform(X_test) # 4. Train model model = LinearRegression() model.fit(X_train_sc, y_train) # 5. Evaluate y_pred = model.predict(X_test_sc) print(f"Rยฒ Score : {r2_score(y_test, y_pred):.4f}") print(f"RMSE : {mean_squared_error(y_test, y_pred, squared=False):.2f}") print(f"Intercept: {model.intercept_:.2f}") print(f"Coef : {model.coef_[0]:.2f}") # 6. Predict for new student new_student_hours = scaler.transform([[8.0]]) predicted_score = model.predict(new_student_hours)[0] print(f"\nPredicted score for 8h study: {predicted_score:.1f}/100")
Keep exploring and happy coding! ๐ป