def SDGRegressionExample(): import numpy as np from sklearn.datasets import load_boston from sklearn.linear_model import SGDRegressor from sklearn.cross_validation import cross_val_score from sklearn.preprocessing import StandardScaler from sklearn.cross_validation import train_test_split data = load_boston() X_train, X_test, y_train, y_test = train_test_split(data.data,data.target) X_scaler = StandardScaler() y_scaler = StandardScaler() X_train = X_scaler.fit_transform(X_train) y_train = y_scaler.fit_transform(y_train) X_test = X_scaler.transform(X_test) y_test = y_scaler.transform(y_test) regressor = SGDRegressor(loss='squared_loss') scores = cross_val_score(regressor, X_train, y_train, cv=5) print 'Cross validation r-squared scores:', scores print 'Average cross validation r-squared score:', np.mean(scores) regressor.fit_transform(X_train, y_train) print 'Test set r-squared score', regressor.score(X_test, y_test)
def SGDDemo(): import numpy as np from sklearn.datasets import load_boston from sklearn.linear_model import SGDRegressor from sklearn.cross_validation import cross_val_score from sklearn.preprocessing import StandardScaler from sklearn.cross_validation import train_test_split data = load_boston() X_train,X_test,y_train,y_test = train_test_split(data.data,data.target) X_scaler = StandardScaler() y_scaler = StandardScaler() X_train = X_scaler.fit_transform(X_train) y_train = y_scaler.fit_transform(y_train) X_test = X_scaler.transform(X_test) y_test = y_scaler.transform(y_test) regressor = SGDRegressor(loss='squared_loss') scores = cross_val_score(regressor,X_train,y_train,cv=5) print "Cross validation r-sqr ",np.mean(scores) regressor.fit_transform(X_train,y_train) print "TEST score :",regressor.score(X_test,y_test)
X_train,X_test,y_train,y_test =train_test_split(data.data,data.target) X_scaler = StandardScaler() y_scaler = StandardScaler() X_train = X_scaler.fit_transform(X_train) y_train = y_scaler.fit_transform(y_train) X_test = X_scaler.transform(X_test) y_test = y_scaler.transform(y_test) regressor = SGDRegressor(loss='squared_loss') score = cross_val_score(regressor, X_train, y_train, cv=5) print score print np.mean(score) regressor.fit_transform(X_train, y_train) print regressor.score(X_test, y_test)
import numpy as np from sklearn.datasets import load_boston from sklearn.linear_model import SGDRegressor from sklearn.cross_validation import cross_val_score from sklearn.preprocessing import StandardScaler from sklearn.cross_validation import train_test_split data = load_boston() X_train, X_test, y_train, y_test = train_test_split(data.data, data.target) X_scaler = StandardScaler() y_scaler = StandardScaler() X_train = X_scaler.fit_transform(X_train) y_train = y_scaler.fit_transform(y_train) X_test = X_scaler.transform(X_test) y_test = y_scaler.transform(y_test) regressor = SGDRegressor(loss='squared_loss') scores = cross_val_score(regressor, X_train, y_train, cv=5) print 'Cross validation r-squared scores:', scores print 'Average cross validation r-squared score:', np.mean(scores) regressor.fit_transform(X_train, y_train) print 'Test set r-squared score', regressor.score(X_test, y_test)
import numpy as np from sklearn.datasets import load_boston from sklearn.linear_model import SGDRegressor from sklearn.cross_validation import cross_val_score from sklearn.preprocessing import StandardScaler from sklearn.cross_validation import train_test_split data = load_boston() # print data X_train, X_test, y_train, y_test = train_test_split(data.data, data.target) X_scaler = StandardScaler() y_scaler = StandardScaler() # print X_train X_train = X_scaler.fit_transform(X_train) y_train = y_scaler.fit_transform(y_train) # print X_train X_test = X_scaler.fit_transform(X_test) y_test = y_scaler.fit_transform(y_test) regressor = SGDRegressor(loss='squared_loss') scores = cross_val_score(regressor, X_train, y_train, cv=5) print X_train.shape print "CV ", scores print regressor.fit_transform(X_train, y_train).shape print "Test r-ss", regressor.score(X_test, y_test)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y) X_scaler = StandardScaler() Y_scaler = StandardScaler() X_train = X_scaler.fit_transform(X_train) Y_train = Y_scaler.fit_transform(Y_train) X_test = X_scaler.transform(X_test) Y_test = Y_scaler.transform(Y_test) print X_train[0:5] print len(X_train) print Y_test clf = SGDRegressor(loss="squared_loss") scores = cross_val_score(clf, X_train, Y_train, cv=5) print scores print np.mean(scores) clf.fit_transform(X_train, Y_train) pred = clf.predict(X_test) print clf.score(X_test, Y_test) # correlation(X_train,Y_train) # feature_selection(X_train,Y_train) scatter_plot(X_train, Y_train)
Y_scaler = StandardScaler() X_train = X_scaler.fit_transform(X_train) Y_train = Y_scaler.fit_transform(Y_train) X_test = X_scaler.transform(X_test) Y_test = Y_scaler.transform(Y_test) print X_train[0:5] print len(X_train) print Y_test clf =SGDRegressor(loss="squared_loss") scores = cross_val_score(clf,X_train,Y_train,cv=5) print scores print np.mean(scores) clf.fit_transform(X_train,Y_train) pred = clf.predict(X_test) print clf.score(X_test,Y_test) # correlation(X_train,Y_train) # feature_selection(X_train,Y_train) scatter_plot(X_train,Y_train)