Data Science Basics with Python


Data Science Basics with Python

Corey
Alright Anna, I want to teach you data science and Python in one hour. Sound impossible? It's not. We're going to start from absolute zero—no Python, no data science—and by the end you'll understand slope-intercept form and even build a predictive model.

Anna
I'm ready. But first, what do I need to install? The content mentioned Visual Studio Code and Anaconda.

Corey
Exactly. Anaconda makes setting up your environment really easy. There's a quick video showing how to install it. Once that's done, you create a folder, open it in VS Code, and install the Python extension. That gives you Jupyter Notebooks built right in.

Anna
So I can write Python code in notebooks inside VS Code. That's neat. And we're using student data—pass/fail stuff, right?

Corey
Yeah. We start with a simple list of grades. But plain Python lists aren't great for math. That's where NumPy comes in. Let's import NumPy and convert that list into an array.

Anna
What's the difference? I saw the content showed that multiplying a list by 2 duplicates the items, but multiplying a NumPy array multiplies each value. That's huge for calculations.

Corey
Right. NumPy arrays let you do vectorized operations. So we can get the mean of the grades easily—turns out it's 49. That's a terrible average. Now we want to bring in study hours to see if there's a relationship.

Anna
So we create an array with two sub-arrays: study hours and grades. But working with nested arrays gets messy. That's why Pandas exists.

Corey
Exactly. Pandas gives you a DataFrame, which is like a table with named columns. It's way more intuitive. You can add a column for student names, and now your data looks like a spreadsheet.

Anna
I saw we can access rows and columns easily. Like using `.loc` to get a specific student's data, or filtering with conditions like `df[df['name'] == 'Vicky']`. That's really powerful.

Corey
And you can also use the `.query()` method. But the typical way is dot notation. Now, real-world data is messy—it has missing values. So we check for nulls with `isnull().sum()` and decide how to handle them.

Anna
The content showed two options: fill with the mean, or drop the rows. Filling can distort the data, so dropping is often safer unless you have a good reason.

Corey
Right. We drop rows with missing values using `dropna(axis=0)`. Now our data is clean. Next we want to add a 'passed' column based on whether grade >= 60. That's easy with a boolean series.

Anna
And then we can group by that column to compare averages. The content showed that students who passed studied on average 14 hours, while those who failed studied only 8. That's a huge difference.

Corey
That's a clear pattern. But to really see it, we need charts. Matplotlib is the go-to library. We can make a bar chart of grades by student, but it's messy if we don't rotate the labels. A simple fix: `plt.xticks(rotation=90)`.

Anna
And we can customize everything: titles, colors, figure size. The figure is the whole canvas; it can hold multiple subplots. So we put a bar chart next to a pie chart showing pass/fail counts. That's two plots in one figure.

Corey
And since DataFrames have a built-in `.plot()` method, you can make a bar chart in one line. It's incredibly convenient. Now let's talk about distributions. A histogram shows how data is spread out.

Anna
Our grade histogram is symmetric, with most scores around 45-60. That's the central tendency. But we also need to know the mean, median, and mode. The content plotted them as vertical lines on the histogram.

Corey
Another great visualization is the box plot. It shows quartiles and outliers. For grades, the box is between the 25th and 75th percentile, and the whiskers show the range. When we rotate it horizontally, it aligns with the histogram.

Anna
And then there's the density plot, which gives a bell curve. That's a probability density function. From that we can make assumptions about the population. But the real magic is looking at relationships.

Corey
Right. We compared study hours vs. grade with a box plot broken down by pass/fail. It's obvious that more study leads to higher grades. But correlation isn't causation. Speaking of correlation, we can compute it: 0.91, which is extremely high.

Anna
So study hours and grade are strongly correlated. Now we want to model that relationship, and that means going back to algebra class. Remember y = mx + b?

Corey
The dreaded slope-intercept form! But here's where it gets practical. X is study hours, Y is grade. We use linear regression to find the best-fit line. Python does the heavy lifting with SciPy's `linregress`.

Anna
It gives us the slope m and intercept b. For our data, m is about 6.31 and b is -17.9. So the line is: predicted grade = 6.31 * study hours - 17.9. That means every extra hour of study adds about 6.3 points to the grade.

Corey
And we can plot that line on top of a scatter plot of the actual data. It's the line of best fit, also called the regression line. Now we have a predictive model. If a student studies 14 hours, we predict a grade of about 70.

Anna
Or if they study 20 hours, the prediction is 108, which is impossible but shows the model needs a cap. This is linear regression, a basic machine learning algorithm. We just used it to predict grades from study time.

Corey
Exactly. And that's the foundation of data science: collect data, clean it, explore it visually, find relationships, build a model, and make predictions. You've learned the whole pipeline in one hour. Pretty cool, right?

Anna
Absolutely. I never thought I'd use slope-intercept form in real life, but now I've actually built a prediction function with it. That's amazing.

Ask anything
We've read this whole study set, so questions about it get the best answers.