Your Excel regression is probably a mess—here’s how Python fixes it


Excel and other spreadsheets are the workhorses of modern business. You’ve probably used the regression function to find a trend line or other linear relationship in your data. Here’s why using Python on your data can turbocharge your regression analysis.

Python separates code from data

No more accidentally messing up your spreadsheet!

While spreadsheets like Excel are useful and popular, using them for actual data analysis sometimes can feel like using the wrong tool for the wrong job. The main problem is that the data and the operations on the data are intertwined in an Excel workbook.

If you want to run a regression, you’ll have to look for a free spot in your spreadsheet, click and drag through your columns, and then have your results right in the spreadsheet. This looks messy and it’s possible to mess up your data if you’re not careful.

Using Python, you can keep your data separate from your analysis. You can slurp up your spreadsheet data into pandas, then use Pingouin or statsmodels for the data. There’s less risk of you messing up your data or your analysis this way.

Jupyter notebooks are reproducible

Others can do what you did (and you can remember)

Total bill vs. tips scatterplot in Seaborn.

Another problem with commingling your spreadsheet data and your regression analysis is that it can be difficult for colleagues to figure out what you’re trying to do or what you actually ran on your data. And that includes yourself when you come back to a spreadsheet days, weeks, or even months later and find yourself scratching your head to remember what you did with your data.

Jupyter notebooks solve this problem. You can load in your data and run some analyses, because they’re separate from each other. You can run your regressions and generate the plots, and you can see the exact code you run. Not only can you run Python code in a Jupyter notebook, you can create Markdown cells with all of the usual formatting to explain your analysis. You can even export your notebooks into other formats like PDFs.

This gives Jupyter a transparency that spreadsheets by themselves can lack. This is why Jupyter notebooks are so popular in scientific computing as well as in data science.

You can run more advanced models if you need to

Why stop at simple linear regression?

Quadratic regression in Python with the Pingouin library.

While simple linear regression, with a standard independent or x variable and a dependent or y variable, is easy enough for Excel, if you want to get into more advanced regression methods, Python makes a lot more sense.

You can have multiple regression, such as more than one independent variable, in Excel and other spreadsheets, but you’ll have to click and drag multiple columns. While you may have to know enough Python to do this in a library call to statsmodels, for example, I find this easier than clicking and dragging.

For example, if I want to see if the size of the party and the total bill have any bearing on the tip in a restaurant from a dataset of restaurant customers, I can run this code in Python:

results = smf.ols('tip ~ total_bill + size',data=tips).fit()
results.summary()

This code uses a formula style that was popularized by R.

Tip regression plots on training and test sets plotted side-by-side.

You can even run sophisticated machine learning routines such as those used in scikit-learn if you need to.

Publication-quality visualizations

Make your work stand out

Lots of people are familiar with the standard scatterplot with a regression line drawn over it. These are easy to generate in spreadsheet programs like Excel or LibreOffice. They’re ubiquituous, but I think they have a characteristic look. It’s not necessarily a good one to me.

Fortunately, it’s easy to generate plots that are almost publication quality, which can help make your next report or presentation stand out.

Let’s go back to our restaurant tips example. I want to show the relationship between the total bill and the tip. This code will plot the regression with Seaborn and adjust the titles to make them more readable:

import seaborn as sns
sns.set_theme()
sns.regplot(x='total_bill',y='tip',data=tips)
plt.title("Tip vs. total bill in a New York City restaurant")
plt.xlabel("Total bill (USD)")
plt.ylabel("Tip (USD)")
plt.show()
Tip vs. bill regression and scatterplot with modified labels.

This will show the scatterplot with the regression line drawn over it, but in a nice default theme that I think looks better than most spreadsheet programs.

Better yet, this will be more transparent then just clicking and dragging in the chart wizard. If you put this code in a Jupyter notebook, not only will you be able to show your colleagues how you did it, but you’ll also be able to remember when you want to run a similar regression later.

You can exchange data between the two

Use the right tool for the job

One reason that it makes sense to use Python to run regression on spreadsheet data is that it’s easy to exchange data between Python and spreadsheets.

The pandas library can handle Excel files using the read_excel() function:

import pandas as pd

pd.read_excel('very_important_data.xls')

It will also read the very common csv format:

pd.read_csv('data.csv')

These commands will import the data into a “DataFrame” where you’ll do your work with Python, including running the regressions. You can also save DataFrames back into other formats. This is useful if you use pandas to “clean” your data to remove duplicates or missing values:

pd.to_csv('cleaned_data.xls')

This lets you use the strengths of both Excel and Python. You can use Excel for entering and formatting data, and Python for creating the regression analysis.


Excel is useful, but when you need more advanced regression analysis, Python will be the tool you need.

OS

Windows, macOS, iPhone, iPad, Android

Brand

Microsoft

Price

$100/year

Developer(s)

Microsoft

Free trial

1 month

Microsoft 365 includes access to Office apps like Word, Excel, and PowerPoint on up to five devices, 1 TB of OneDrive storage, and more.




Source link

Leave a Reply

Subscribe to Our Newsletter

Get our latest articles delivered straight to your inbox. No spam, we promise.

Recent Reviews


TL;DR

Meta stripped NameTag facial recognition code from its AI app one day after WIRED exposed it on 50 million phones. Meta says no decision has been made.

Meta removed nearly all traces of an unreleased facial recognition system from its smart glasses companion app on Friday, one day after WIRED reported that the software had been quietly embedded in an app installed on more than 50 million phones. The feature, which Meta internally called NameTag, was designed to convert faces captured by the company’s Ray-Ban smart glasses into unique biometric signatures and compare them against a database stored on the user’s device. WIRED also found that faces the system failed to recognise were cropped, indexed, and stored locally for future processing.

Andy Stone, Meta’s vice president of communications, told WIRED on Monday that the feature is “purely exploratory,” adding that no final decision has been made on what to do with it. That characterisation sits uneasily with the evidence WIRED documented. The version of Meta AI published the day of WIRED’s Thursday report contained several code libraries explicitly named for face recognition, a process for running the NameTag recognition pipeline, and a “Person recognised” alert the app would have shown if someone were identified.

Friday’s release stripped all of it out, along with a folder where the app would have stored the cropped images and biometric signatures of unrecognised faces. Meta did not answer WIRED’s questions about why the code was removed or whether the changes were planned before the story was published. A few fragments remain in the latest version, including an internal debug menu label and a dormant link meant to open a recognised person’s profile, pointing to parts of the system that are no longer there.

The 💜 of EU tech

The latest rumblings from the EU tech scene, a story from our wise ol’ founder Boris, and some questionable AI art. It’s free, every week, in your inbox. Sign up now!

The gap between Meta’s public statements and the code WIRED found is the central tension. Before the Thursday report, Stone dismissed the findings by writing that the company could not answer questions about how the system would work because “the feature does not exist.” Andrew Bosworth, Meta’s chief technology officer, called the reporting “incredibly misleading” and “absolutely dishonest.” Yet the code was functional enough to include three AI models, one to detect faces, another to crop them, and a third to encode them as biometric data, all embedded in the companion app for a product already at the centre of a mounting privacy crisis.

Meta declined to answer ten questions WIRED posed before publishing, including whether it had already created the database of face profiles NameTag uses, how long the app retains photographs and biometric data of unrecognised people, and whether that data would ever be sent back to Meta’s servers. The company also did not respond to questions about whether it was building NameTag for blind or low-vision users, or to criticism from privacy advocates who warned the system could let stalkers and abusers identify strangers in public.

NameTag first surfaced in February, when The New York Times, citing internal Meta documents, reported that the company was developing face recognition for its smart glasses and considering a launch as early as this year. One internal memo reportedly described releasing the feature during a “dynamic political environment” when privacy and civil liberties advocates would be distracted by other concerns. WIRED subsequently found that much of NameTag’s machinery had been built into the Meta AI app as early as January, months before any public acknowledgement, adding another layer to the company’s pattern of shipping first and disclosing later when it comes to its smart glasses.

Kade Crockford, director of the technology for liberty programme at the American Civil Liberties Union of Massachusetts, said the removal does not undo the original decision to ship the code and pointed to it as evidence that consumer privacy needs stronger legal protection than Congress has been willing to provide. The Massachusetts House of Representatives last week unanimously passed a consumer privacy bill that, if enacted as written, would impose strong enforcement provisions including a private right of action allowing aggrieved users to sue. “State lawmakers need to do their job and step up to protect consumer privacy,” Crockford said.

Meta’s sneaky tactics in slipping the face-recognition code into its smart glasses show exactly why data privacy bills need the teeth of strong enforcement,” Crockford added. “Companies like Meta prioritise their bottom line, so lawmakers need to speak in the only language its C-suite understands.” Whether a code removal prompted by investigative reporting constitutes a victory or merely a tactical retreat depends on what Meta does next, and on whether the regulatory pressure building on both sides of the Atlantic produces enforceable consequences before the feature quietly returns under a different name.



Source link