Python Tutorial: Machine learning basics

Learn the machine learning workflow from data inspection to prediction. The tutorial covers using NumPy and Pandas to explore raw data, reshaping data into the two-dimensional format Scikit-Learn requires, and training a Support Vector Classifier with fit and predict methods.
Chapters
Now we'll cover the basics of machine learning. This should be a recap of material that you've already covered in previous Data Camp courses. We'll start with the basics of how to fit and predict a model using Scikit Learn. Before performing any data analysis, you should always take a look at your raw data. This gives you a quick, high level take on the quality and kind of your data. In Numpy, you can do so by printing out the first few rows of the data. In Pandas, this can be done by using the Head method which shows the first five rows and all columns. By default, it is also crucial to visualize your data. The proper visualization will depend on the kind of data you've got, though histograms and scatter plots are a good place to start. Look at the distribution of your data. Does it seem reasonable? Are there any outliers? Are you missing data? Each of these questions is important to answer before doing any analysis. Once you've gotten to know your data, it's time to start modeling it. The most popular library for machine learning in Python is called Scikit Learn. It has a standardized API so that you can fit many different models with a similar code structure. Here we import Support Vector Machine to classify data points. SciKit Learn expects data to have a particular shape. Before using Scikit Learn, your data should be two dimensional. The first axis should correspond to sample number and the second should correspond to feature number. This pattern is used in almost all Scikit Learn functions. If your data is not in this shape, there are a few options for reshaping it so that you can use it. With Scikit Learn, the most common approach is to transpose your data. This will swap the first and the last axis. This is most useful when your data is two dimensional. Another option is to use the Reshape method, which lets you specify the shape you want. Now that your data has the correct shape, it's time to fit a model. First, we must create an instance of the model we've imported. In this case, a support vector classifier. You can call the method Fit on this instance to train the model. Here we show how you can input X the training data and Y labels for each data point. To fit the model, it is often useful to investigate what kinds of patterns the model has found. Most models will store this information in attributes that are created after calling Fit. Here we show the coefficients the model has given to each feature. Once your model is fit, you can call the Predict method on the model to determine labels for unseen data points. Now that we've practiced loading and preparing data and fitting the model. It's time to put this in.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







