Skip to content

Instantly share code, notes, and snippets.

View lundquist-ecology-lab's full-sized avatar

Lundquist Ecology Lab lundquist-ecology-lab

View GitHub Profile
@lundquist-ecology-lab
lundquist-ecology-lab / linear_regression_ML.py
Last active January 16, 2023 22:36
Linear regression example in Python
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# Load data
url = 'https://gist.githubusercontent.com/ZeccaLehn/4e06d2575eb9589dbe8c365d61cb056c/raw/898a40b035f7c951579041aecbfb2149331fa9f6/mtcars.csv'
data = pd.read_csv(url, index_col=0)
print(data.head(5))
@lundquist-ecology-lab
lundquist-ecology-lab / decision_tree.py
Created January 16, 2023 22:31
Example decision tree using Iris dataset in Python
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# Load data
iris = load_iris()
X = iris.data
y = iris.target
# Split data into training and testing sets
@lundquist-ecology-lab
lundquist-ecology-lab / ANN.r
Last active January 16, 2023 22:04
Artificial neural network (ANN) analysis in R using iris data
# Load data
data <- iris
# Split data into training and testing sets
set.seed(123)
index <- sample(1:nrow(data), 0.8 * nrow(data))
train <- data[index,]
test <- data[-index,]
# Build ANN model
@lundquist-ecology-lab
lundquist-ecology-lab / linear_regression_ML.r
Last active January 16, 2023 22:13
Linear regression machine learning model example in R using mtcars data
# Load data
data <- mtcars
# Build linear regression model
fit <- lm(mpg ~ wt + hp, data = data)
# Summarize the model
summary(fit)
@lundquist-ecology-lab
lundquist-ecology-lab / decision_tree.r
Last active January 16, 2023 22:08
Decision tree using the Iris datas in R
library(rpart)
# Load data
data <- iris
# Build decision tree
fit <- rpart(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data = data, method = "class")
# Plot the decision tree
library(rpart.plot)
@lundquist-ecology-lab
lundquist-ecology-lab / ChatGPT_model.py
Last active January 16, 2023 21:24
Logististic model report in Python produced by ChatGPT
# Load data
import pandas as pd
data = pd.read_csv("data.csv")
# Run logistic regression analysis
from sklearn.linear_model import LogisticRegression
X = data[['age', 'income']]
y = data['default']
clf = LogisticRegression(random_state=0).fit(X, y)
@lundquist-ecology-lab
lundquist-ecology-lab / ChatGPT_model.r
Created January 16, 2023 21:12
Linear model output in R as provided by ChatGPT
library(tidyverse)
# Load data
data <- mtcars
# Run linear regression analysis
fit <- lm(mpg ~ wt, data = data)
# Provide ChatGPT with the summary of the model
summary_model <- summary(fit)
@lundquist-ecology-lab
lundquist-ecology-lab / anova.py
Last active January 15, 2023 01:20
Analysis of variance (ANOVA) using the Iris data set in Python
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt
import seaborn as sns
import statsmodels.api as sm
from sklearn.datasets import load_iris
from bioinfokit.analys import stat
from statsmodels.formula.api import ols
@lundquist-ecology-lab
lundquist-ecology-lab / t_test.py
Created January 14, 2023 20:17
T-test suite in Python
import scipy.stats as stats
import numpy as np
## One sample t
data=[13, 14, 13, 12, 14, 15, 16, 13, 14, 12]
x=stats.ttest_1samp(a=data, popmean=15)
print(x)
# Two sample t
@lundquist-ecology-lab
lundquist-ecology-lab / binomial_test.py
Last active January 14, 2023 20:12
Binomial test in Python
# Binomial test in Python
from scipy.stats import binom_test
# Six-sided die flipped 24 times and lands on three exactly 6 times
x = binom_test(x=6, n=24, p=1/6, alternative='greater')
print(x)
# Flip coin 30 times and it lands on heads exactly 19 times
y = binom_test(x=19, n=30, p=1/2, alternative='greater')