Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ahmadali-jamali/cffbb5dc46273f5adc6dd24c3e40cfdb to your computer and use it in GitHub Desktop.
Save ahmadali-jamali/cffbb5dc46273f5adc6dd24c3e40cfdb to your computer and use it in GitHub Desktop.
Ml regression with python sklearn (predict of price of house's in Boston city(my second project of ML))
# Machin learning for predict of price of house's in Boston city with regression target
#Ahmadali 2019
#All of the Libraries
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVR
from sklearn.metrics import accuracy_score
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from pandas import read_csv
from matplotlib import pyplot as plt
from pandas import set_option
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
import numpy as np
from numpy import arange
from pandas.plotting import scatter_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
import sklearn.metrics
from sklearn.linear_model import Ridge
#Housing Dataset
filename= r"C:\H/housing.data.csv"
names=['CRIM','ZN','INDUS','CHAS','NOX','RM','AGE','DIS','RAD','TAX','PTRATIO','B','LSTAT','MEDV']
dataset = read_csv(filename,delim_whitespace=True, names=names)
#describe and analys
set_option('precision',1)
print(dataset.shape)
print(dataset.head(20))
print(dataset.describe())
print(dataset.dtypes)
print(dataset.describe())
#correlation
set_option('precision',2)
print(dataset.corr(method='pearson'))
#histograms
dataset.hist(sharex=False,sharey=False,xlabelsize=1,ylabelsize=1)
plt.show()
#density
dataset.plot(kind='density',subplots=True,layout=(4,4),sharex=False,legend=False,fontsize=1)
plt.show()
#compair TEST and TRAIN
array=dataset.values
X=array[:,0:13]
Y=array[:,13]
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.3,random_state=42)
reg=LinearRegression()
reg.fit(X_train,Y_train)
Y_predict=reg.predict(X_test)
plt.scatter(Y_test,Y_predict)
plt.title('Compair TEST and TRAIN')
plt.xlabel('Prices')
plt.ylabel('Predictet Prices')
plt.show()
#MSE (MEAN SQUARED ERROR)
MSE=mean_squared_error(Y_test,Y_predict)
print('This is mean squared error = ',MSE)
#Cross Validation (K-Fold cross Validation)
REG=LinearRegression()
cv_score=cross_val_score(REG,X,Y,cv=5)
Mean=np.mean(cv_score)
print('cv_score is : {} and Mean is : {}'.format(cv_score,Mean) )
#Reqularization Regression
Lasso=Lasso(alpha=0.1,normalize=True)
Lasso.fit(X,Y)
Lasso_coeff=Lasso.coef_
print('this is Lasso coeff',Lasso_coeff)
plt.plot(range(13),Lasso_coeff)
plt.xticks(range(13),names)
plt.ylabel('coef')
plt.show() #### the Romes are must important beetwen these features
ridge=Ridge(alpha=0.1,normalize=True)
ridge.fit(X_train,Y_train)
ridge_predict=ridge.predict(X_test)
print('this is predict test : ',ridge_predict)
#Predict Test
X_New=[]
for i in range(1):
column=[]
for j in range(13):
column.append(eval(input('enter Numbers : ')))
X.New.append(column)
Y_New=ridge.predict(X_New)
print(Y_New)
#End
#Programmer Ahmadali Jamali.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment