import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from loadData import loaddata from plotData import plotData data = loaddata('ex2data1.txt', ',') X = np.c_[np.ones( (data.shape[0], 1) ), data[:, 0: 2]] # adds column of ones (first argument) to X data (second argument) AND creates X from data at the same time y = np.c_[data[:, 2]] # creates Y from data plotData(data, 'Exam 1 score', 'Exam 2 score', 'Admitted', 'Not admitted') #plots the data plt.show() X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.1, random_state=0 ) #split X and y data into training and testing set. Model will train on train set and will check accuracy on test set logreg = LogisticRegression() #Initializing the model logreg.fit(X_train, y_train) #Feeding values in the model print('Theta ', logreg.coef_) #theta value after the model is trained y_pred = logreg.predict(X_test) # generating the predictions on the test set print('Accuracy of logistic regression classifier on test set: {:.2f}'.format( logreg.score(X_test, y_test)))
import numpy as np import matplotlib.pyplot as plt from scipy.optimize import minimize from plotData import plotData from loadData import loaddata from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn import metrics from predict import predict data = loaddata( 'C:\\Users\\Ananya\\Desktop\\ML-Exercises-\\Week3\\ex2data1.txt', ',') X = np.c_[np.ones( (data.shape[0], 1) ), data[:, 0: 2]] # adds column of ones (first argument) to X data (second argument) AND creates X from data at the same time y = np.c_[data[:, 2]] # creates Y from data plotData(data, 'Exam 1 score', 'Exam 2 score', 'Admitted', 'Not admitted') #plots the data plt.show() input( "proceed only if you've completed completed ALL the files, else press Ctrl+C then enter to exit the program" ) from costFunction import costFunction, gradient initial_theta = np.zeros(X.shape[1]) cost = costFunction(initial_theta, X, y) grad = gradient(initial_theta, X, y) print('Cost: \n', cost) print('Grad: \n', grad)
import numpy as np import matplotlib.pyplot as plt from scipy.optimize import minimize from sigmoid import sigmoid from plotData import plotData from predict import predict from loadData import loaddata from sklearn.preprocessing import PolynomialFeatures from costFunctionReg import costFunctionReg, gradientReg data2 = loaddata('ex2data2.txt', ',') y = np.c_[data2[:, 2]] X = data2[:, 0:2] ''' Since we see that the data cannot be separated linearly, we shall create more features to let the logistic regressor design a more complex boundary which will fit the data. We use a technique called feature mapping as shown below. However feature mapping makes the data prone to overfitting. ''' poly = PolynomialFeatures(6) XX = poly.fit_transform( data2[:, 0:2] ) # create more features from data as terms of X1 and X2 upto the sixth power, called feature mapping, helps us fit the data better initial_theta = np.zeros(XX.shape[1]) print(costFunctionReg(initial_theta, 1, XX, y)) fig, axes = plt.subplots(1, 3, sharey=True, figsize=(17, 5)) # Decision boundaries # Lambda = 0 : No regularization --> too flexible, overfitting the training data # Lambda = 1 : Looks about right # Lambda = 100 : Too much regularization --> high bias
#img = resize_image(img, 224, 224) def pil_to_nparray(pil_image): """ Convert a PIL.Image to numpy array. """ pil_image.load() return np.asarray(pil_image, dtype="float32") #img = pil_to_nparray(img) print(u'用于测试的图片加载完成!') # Data loading and preprocessing import tflearn.datasets.oxflower17 as oxflower17 (coll, collmask, testcoll, testcollmask) = loaddata() #print('------') #print('666666666666') #X, Y = oxflower17.load_data(one_hot=True) # Building ‘VGG Network‘以下为模型的加载,其中3是卷积核的大小即3*3.64/128/256/512是卷积核的个数 network = input_data(shape=[None, 224, 224, 1]) network = conv_2d(network, 64, 3, activation='relu') network = conv_2d(network, 64, 3, activation='relu') network = max_pool_2d(network, 2, strides=2) network = conv_2d(network, 128, 3, activation='relu') network = conv_2d(network, 128, 3, activation='relu') network = max_pool_2d(network, 2, strides=2)