Sklearn Main Cheat Sheet

Plotting Graphs

Import

import matplotlib.pyplot as plt

Data

months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
revenue = [52, 74, 79, 95, 115, 110, 129, 126, 147, 146, 156, 184]

Plot

# as points
plt.plot(months, revenue, "o")

# as a line
plt.plot(months, revenue)

plt.show()

Getting Data

Import

import pandas as pd

Read

dataFrame = pd.read_csv("/data.csv")

Show

# Basically prints the data frame
print(dataFrame.head(numOfRows))
# argument can be len(dataFrame)

Linear Regression

Import

from sklearn.linear_model import LinearRegression

Fit data

line_fitter = LinearRegression()

# x and y are lists
line_fitter.fit(x, y)


# get slope
line_fitter.coef_

# get intercept
line_fitter.intercept_

Predict

# takes in a list of x, returns a list of y
y_predicted = line_fitter.predict(x)