Posts

Showing posts with the label Machine Learning

Can Anyone Give A Real Life Example Of Supervised Learning And Unsupervised Learning?

Answer : Supervised learning: You get a bunch of photos with information about what is on them and then you train a model to recognize new photos. You have a bunch of molecules and information about which are drugs and you train a model to answer whether a new molecule is also a drug. Unsupervised learning: You have a bunch of photos of 6 people but without information about who is on which one and you want to divide this dataset into 6 piles, each with the photos of one individual. You have molecules, part of them are drugs and part are not but you do not know which are which and you want the algorithm to discover the drugs. Supervised Learning: is like learning with a teacher training dataset is like a teacher the training dataset is used to train the machine Example: Classification: Machine is trained to classify something into some class. classifying whether a patient has disease or not classifying whether an email is spam or no...

Converting LinearSVC's Decision Function To Probabilities (Scikit Learn Python )

Image
Answer : scikit-learn provides CalibratedClassifierCV which can be used to solve this problem: it allows to add probability output to LinearSVC or any other classifier which implements decision_function method: svm = LinearSVC() clf = CalibratedClassifierCV(svm) clf.fit(X_train, y_train) y_proba = clf.predict_proba(X_test) User guide has a nice section on that. By default CalibratedClassifierCV+LinearSVC will get you Platt scaling, but it also provides other options (isotonic regression method), and it is not limited to SVM classifiers. I took a look at the apis in sklearn.svm.* family. All below models, e.g., sklearn.svm.SVC sklearn.svm.NuSVC sklearn.svm.SVR sklearn.svm.NuSVR have a common interface that supplies a probability: boolean, optional (default=False) parameter to the model. If this parameter is set to True, libsvm will train a probability transformation model on top of the SVM's outputs based on idea of Platt Scaling. The form of transformation is similar to a lo...

Calculate The Cumulative Distribution Function (CDF) In Python

Image
Answer : (It is possible that my interpretation of the question is wrong. If the question is how to get from a discrete PDF into a discrete CDF, then np.cumsum divided by a suitable constant will do if the samples are equispaced. If the array is not equispaced, then np.cumsum of the array multiplied by the distances between the points will do.) If you have a discrete array of samples, and you would like to know the CDF of the sample, then you can just sort the array. If you look at the sorted result, you'll realize that the smallest value represents 0% , and largest value represents 100 %. If you want to know the value at 50 % of the distribution, just look at the array element which is in the middle of the sorted array. Let us have a closer look at this with a simple example: import matplotlib.pyplot as plt import numpy as np # create some randomly ddistributed data: data = np.random.randn(10000) # sort the data: data_sorted = np.sort(data) # calculate the proportional ...

Adding L1/L2 Regularization In PyTorch?

Answer : Following should help for L2 regularization: optimizer = torch.optim.Adam(model.parameters(), lr=1e-4, weight_decay=1e-5) This is presented in the documentation for PyTorch. Have a look at http://pytorch.org/docs/optim.html#torch.optim.Adagrad. You can add L2 loss using the weight decay parameter to the Optimization function. For L2 regularization, l2_lambda = 0.01 l2_reg = torch.tensor(0.) for param in model.parameters(): l2_reg += torch.norm(param) loss += l2_lambda * l2_reg References: https://discuss.pytorch.org/t/how-does-one-implement-weight-regularization-l1-or-l2-manually-without-optimum/7951. http://pytorch.org/docs/master/torch.html?highlight=norm#torch.norm.

Cannot Connect To X Server GOOGLE COLAB

Answer : An X server is a program in the X Window System that runs on local machines (i.e. the computers used directly by users) and handles all access to the graphics cards, display screens and input devices (typically a keyboard and mouse) on those computers. With that said Colab runs as a terminal instance in the server, if you are using GPU runtime, then the problem is not with X server accessing your Graphics card, neither with Input devices, generally this occurs when you try to parse some data that should be displayed as separate window on your desktop, commands like cv2.imshow() , there can be other similar functions that can cause this problem, if you have to use graphical ouput you might want to look into %matplotlib notebook and displaying the data in the interactable matplot plots. If this is not your issue, just post a link to your modified code and I might be able to help more. I had the same problem in Colab for a simple OpenCV program to track a tennis ball in ...