import matplotlib.pyplot as plt import numpy as np from sklearn.linear_model import LinearRegression import pandas as pd houses_full=pd.read_csv('dados/Melbourne_housing_FULL.csv') houses=houses_full.dropna() houses=houses.loc[houses['BuildingArea']<1500] houses=houses.sample(1000,random_state=123) predictors=['BuildingArea','Rooms','Distance'] houses[predictors].head() x=np.array(houses.loc[:,predictors]) y=np.array(houses.loc[:,'Price']) def priceshrink(v,t): return int(v/1000000) fig, sc = plt.subplots(1,len(predictors)) fig.suptitle('Scatter plots between predictors and target') for i in range(len(predictors)): sc[i].scatter(x[:,i], y) sc[i].set(xlabel=predictors[i]) #sc[i].yaxis.set_major_formatter(plt.NullFormatter()) sc[i].yaxis.set_major_formatter(plt.FuncFormatter(priceshrink)) sc[0].set(ylabel='Price') #sc[0].yaxis.set_major_formatter(plt.FuncFormatter(priceshrink)) plt.show() model = LinearRegression(fit_intercept=True) model.fit(x[:,0:1], y) xfit = np.linspace(0, max(x[:,0]), 1000) # newaxis gives xfit the shape of a matrix yfit = model.predict(xfit[:,np.newaxis]) ax=plt.axes(label='lrfit') ax.scatter(x[:,0], y) ax.set_ylabel('Price') ax.set_xlabel('BuildingArea') ax.set_xlim(left=0) ax.set_xlim(left=0,right=max(x[:,0])) ax.yaxis.set_major_formatter(plt.FuncFormatter(priceshrink)) ax.plot(xfit, yfit,color='red') plt.show() print("Model slope: ", model.coef_[0]) print("Model intercept:", model.intercept_) print("Model R2: ", model.score(x[:,0:1],y)) model.fit(x, y) print("Predictors:",predictors) print("Coefficients (alphas): ", model.coef_) print("Model intercept:", model.intercept_) print("Model R2: ", model.score(x,y)) rng = np.random.RandomState(1) x = 10 * rng.rand(50) y = (np.sin(x) + 0.1 * rng.randn(50) +5)*24 ax=plt.axes(label='lrknn') ax.set_xlim(0,10) ax.scatter(x, y) # KNN from sklearn.neighbors import KNeighborsRegressor neigh = KNeighborsRegressor(n_neighbors=2) neigh.fit(x[:,np.newaxis], y) yfitn=neigh.predict(xfit[:,np.newaxis]) ax=plt.axes(label='lrknn') ax.set_xlim(0,10) ax.scatter(x, y) ax.plot(xfit, yfitn,color='green') plt.show()