from sklearn.linear_model import LinearRegression
x = data[["val1"]] # Independent variable
y = data["val2"] # Dependent variable
reg = LinearRegression()
reg.fit(x, y) # Creates the model
reg.predict([[value]]) # Predicts the value
print(reg.coef_) # Slope of line
print(reg.intercept_) # Intercept of line
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
x = data[["val1", "val2"]] # Independent variables
y = data["val3"] # Dependent variable
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=10) # Splitting data
reg = LinearRegression()
reg.fit(x_train, y_train) # Creates the model using training data
reg.predict(x_test) # Predicts the value
reg.predict([[val1, val2]]) # Predicts the value
reg.score(x_test, y_test) # Predicts the score of the model
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
x = data[["val1"]] # Independent variables
y = data["val3"] # Dependent variable
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=10) # Splitting data
reg = LogisticRegression()
reg.fit(x_train, y_train) # Creates the model using training data
reg.predict(x_test) # Predicts the value
reg.score(x_test, y_test) # Predicts the score of the model
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler
from matplotlib import pyplot as plt
scaler = MinMaxScaler()
scaler.fit(data[["val1"]])
data["val1"] = scaler.transform(data[["val1"]])
scaler.fit(data[["val2"]])
data["val2"] = scaler.transform(data[["val2"]])
plt.scatter(data.val1, data.val2)
km = KMeans(n_clusters=N) # "N" will be equal to the number of clusters observed
predicted = km.fit_predict(data[["val1", "val2"]])
data["cluster"] = predicted
data1 = data[data.cluster=0]
data2 = data[data.cluster=1]
dataN = data[data.cluster=N]
plt.scatter(data1.val1, data1.val2, color="green")
plt.scatter(data2.val1, data2.val2, color="red")
plt.scatter(dataN.val1, dataN.val2, color="blue")
km.cluster_centers_
plt.scatter(km.cluster_centers_[:,0], km.cluster_centers_[:,1], color="black", marker="*") # Plot centroid values
plt.xlabel("val1")
plt.ylabel("val2")
plt.plot()