Skip to content

Instantly share code, notes, and snippets.

@carterprince
Created August 21, 2025 22:24
Show Gist options
  • Select an option

  • Save carterprince/d354212e2413ba1f8b72fd129a59a15f to your computer and use it in GitHub Desktop.

Select an option

Save carterprince/d354212e2413ba1f8b72fd129a59a15f to your computer and use it in GitHub Desktop.
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
df = pd.DataFrame()
df['x'] = np.random.uniform(0, 2, size=100)
df['y'] = np.random.uniform(0, 3, size=100)
corr_matrix = df.corr()
cov_matrix = df.cov()
print("Correlation Matrix")
print(corr_matrix, end='\n\n')
print("Covariance Matrix")
print(cov_matrix, end='\n\n')
sns.scatterplot(data=df, x='x', y='y')
sns.regplot(data=df, x='x', y='y')
X = df[['x']]
y = df['y']
linreg = LinearRegression()
linreg.fit(X, y)
print("slope:", linreg.coef_[0])
print("intercept:", linreg.intercept_)
plt.savefig('plot.png')
y_pred = linreg.predict(X)
print("R2:", r2_score(X, y_pred))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment