# Učitaj osnovne biblioteke... import scipy as sp import sklearn %pylab inline X = sp.array([[0],[1],[2],[4]]) y = sp.array([4,1,2,5]) from sklearn.preprocessing import PolynomialFeatures from scipy import linalg from sklearn.metrics import mean_squared_error from sklearn.linear_model import LinearRegression #generira N jednodimenzijskih primjera uniformno u intervalu [x1,x2] def make_instances(x1,x2,N) : return sp.array([sp.array([x]) for x in linspace(x1,x2,N)]) from sklearn import cross_validation from sklearn.linear_model import Ridge def nonzeroes(coef) : return size(coef[coef!=0]) from sklearn.linear_model import Lasso from sklearn.datasets import load_boston boston = load_boston() print boston.data.shape print boston.target.shape print boston.DESCR from sklearn import cross_validation X_train, X_rest, y_train, y_rest = cross_validation.train_test_split(boston.data,boston.target,train_size=0.6,random_state=42) X_validate, X_test, y_validate, y_test = cross_validation.train_test_split(X_rest,y_rest,test_size=0.5,random_state=42) print X_train.shape, X_validate.shape ,X_test.shape seven_X = sp.array([[2,1],[2,3],[1,2],[3,2],[5,2],[5,4],[6,3]]) seven_y = sp.array([1,1,1,1,-1,-1,-1]) def plot_problem(X, y, h=None, surfaces=True) : ''' Plots a two-dimensional labeled dataset (X,y) and, if function h(x) is given, the decision boundaries (surfaces=False) or decision surfaces (surfaces=True) ''' assert X.shape[1] == 2, "Dataset is not two-dimensional" if h!=None : # Create a mesh to plot in r = 0.02 # mesh resolution x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, r), np.arange(y_min, y_max, r)) XX=np.c_[xx.ravel(), yy.ravel()] try: Z_test = h(XX) if shape(Z_test) == () : # h returns a scalar when applied to a matrix; map explicitly Z = sp.array(map(h,XX)) else : Z = Z_test except ValueError: # can't apply to a matrix; map explicitly Z = sp.array(map(h,XX)) # Put the result into a color plot Z = Z.reshape(xx.shape) if surfaces : plt.contourf(xx, yy, Z, cmap=plt.cm.Pastel1) else : plt.contour(xx, yy, Z) # Plot the dataset scatter(X[:,0],X[:,1],c=y, cmap=plt.cm.Paired,marker='o',s=50); plot_problem(seven_X,seven_y) from sklearn.linear_model import RidgeClassifier X2 = sp.append(seven_X,[[2,2]],axis=0) y2 = sp.append(seven_y,-1) from sklearn.metrics import accuracy_score X3 = sp.append(seven_X,[[12,8]],axis=0) y3 = sp.append(seven_y,-1) from sklearn.datasets import make_classification from sklearn.linear_model import Perceptron from sklearn.metrics import log_loss from sklearn.metrics import zero_one_loss from scipy.stats import multivariate_normal from sklearn.linear_model import LogisticRegression from sklearn.datasets import make_classification from sklearn.preprocessing import PolynomialFeatures #poly=PolynomialFeatures(2) #... #plot_problem(X,y,lambda x : model.predict(poly.transform(x)) from sklearn.cross_validation import train_test_split data = sp.loadtxt("/path/to/glass.data", delimiter=",") glass_X, glass_y = data[:,1:10], data[:,10] from sklearn import cross_validation X_train, X_test, y_train, y_test = cross_validation.train_test_split(glass_X,glass_y,train_size=2.0/3,random_state=42) print X_train.shape, X_test.shape from sklearn.preprocessing import StandardScaler