<jupyter_start><jupyter_text>Regression Problem: A super straightforward guide about characteristic of some regression model<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "last_expr_or_assign"
d=pd.read_csv('https://raw.githubusercontent.com/harshitroy2605/support-vector-regression/master/Position_Salaries.csv')
X=d[['Level']]
y=d[['Salary']];
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
X=scaler.fit_transform(X)
y=scaler.fit_transform(y)
model=SVR()
model.fit(X,y.ravel())
X_grid=np.linspace(-np.array(X).max()*3, np.array(X).max()*3, 10000)
X_grid=X_grid.reshape(-1,1)
plt.scatter(X, y, color='red')
plt.plot(X_grid, model.predict(X_grid));<jupyter_output><empty_output><jupyter_text>KNN: with and without scaling<jupyter_code>from sklearn.neighbors import KNeighborsRegressor
model=KNeighborsRegressor()
model.fit(X,y)
X_grid=np.linspace(-np.array(X).max()*2, np.array(X).max()*2, 10000)
X_grid=X_grid.reshape(-1,1)
plt.scatter(X, y, color='red')
plt.plot(X_grid, model.predict(X_grid));
X=d[['Level']]
y=d['Salary'];
from sklearn.linear_model import LinearRegression
model=LinearRegression()
model.fit(X,y)
X_grid=np.linspace(-np.array(X).max()*0.5, np.array(X).max()*2, 10000)
X_grid=X_grid.reshape(-1,1)
plt.scatter(X, y, color='red')
plt.plot(X_grid, model.predict(X_grid));
X=d[['Level']];
y=d['Salary'];
from sklearn.neighbors import KNeighborsRegressor
model=KNeighborsRegressor()
model.fit(X,y)
X_grid=np.linspace(-np.array(X).max()*1, np.array(X).max()*1.5, 10000)
X_grid=X_grid.reshape(-1,1)
plt.scatter(X, y, color='red')
plt.plot(X_grid, model.predict(X_grid));
X=d[['Level']]
y=d['Salary'];
from sklearn.ensemble import RandomForestRegressor
model=RandomForestRegressor(n_estimators=50, random_state=0)
model.fit(X,y)
X_grid=np.linspace(-np.array(X).max()*0.5, np.array(X).max()*2, 10000)
X_grid=X_grid.reshape(-1,1)
plt.scatter(X, y, color='red')
plt.plot(X_grid, model.predict(X_grid));
X=d[['Level']]
y=d['Salary'];
from sklearn.tree import DecisionTreeRegressor
model=DecisionTreeRegressor(random_state=1)
model.fit(X,y)
X_grid=np.linspace(-np.array(X).max()*0.5, np.array(X).max()*2, 10000)
X_grid=X_grid.reshape(-1,1)
plt.scatter(X, y, color='red')
plt.plot(X_grid, model.predict(X_grid))
plt.title(model);
X=d[['Level']]
y=d['Salary'];
from sklearn.linear_model import Perceptron
model=Perceptron()
model.fit(X,y)
X_grid=np.linspace(-np.array(X).max()*0.5, np.array(X).max()*1, 10000)
X_grid=X_grid.reshape(-1,1)
plt.scatter(X, y, color='red')
plt.plot(X_grid, model.predict(X_grid));
X=d[['Level']]
y=d['Salary'];
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

pol=PolynomialFeatures()

pol_fit=pol.fit_transform(X)

model=LinearRegression()
model.fit(pol_fit, y)
X_grid=np.linspace(-np.array(X).max()*0.5, np.array(X).max()*2, 10000)
X_grid=X_grid.reshape(-1,1)
plt.scatter(X, y, color='red')
plt.plot(X_grid, model.predict(pol.fit_transform(X_grid)));<jupyter_output><empty_output><jupyter_text>Example of wrong model to be used in regression problem. Logistic regression indeed is a classification model (just try with y(target) as float and you get the error)!<jupyter_code>X=d[['Level']]
y=d['Salary'];
from sklearn.linear_model import LogisticRegression
model=LogisticRegression(max_iter=1000)
model.fit(X,y)
X_grid=np.linspace(-np.array(X).max()*0.5, np.array(X).max()*2, 10000)
X_grid=X_grid.reshape(-1,1)
plt.scatter(X, y, color='red')
plt.plot(X_grid, model.predict(X_grid));<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup