Category: Python
How to Interpret Interaction Effects in Linear Regression (4 Steps)
This tutorial shows how to interpret interaction effects in linear regression models. In summary, there are two perspectives, (a) mean difference perspective and (b) slope difference perspective. Interpret Interaction Effect in Linear Regression (a) Mean Difference Perspective: One way to...
Read Full Article →
How to Avoid Including Dataframe Index when Saving as CSV file in Python
To avoid including the index column when saving as a CSV file, you can add index=False in df.to_csv() to avoid including the index clumn when saving a Python Pandas dataframe as a CSV file. df.to_csv(‘filename.csv’, index=False) Output: brands models 0...
Read Full Article →
Plot for Interactions of 2 Categorical Variables in Python (with example)
This tutorial shows how to plot interactions of 2 categorical independent variables in Python. The following shows both the ANOVA and linear regression outputs. You will see that ANOVA is also a linear regression model. Thus, it does not matter...
Read Full Article →
How to Calculate Predicated Y in Linear Regression in Python
This tutorial shows how you can calculate predicted Y (or, estimated Y) in linear regression in Python. Steps of Calculating Predicated Y in Linear Model in Python Step 1: Prepare data, X and Y Output: X: [[ 5] [ 2]...
Read Full Article →
How to Convert 2-D Arrays to 1-D Arrays
There are 3 methods to convert 2-D arrays to 1-D ones in Numpy. The following shows the key syntax. Method 1: numpy.ravel() Method 2: ndarray.flatten(order=’C’) Method 3: ndarray.reshape(-1) Example 1 (Method 1): We can use numpy.ravel() to convert 2-D arrays...
Read Full Article →
How to Fix: Data must be 1-dimensional
You might encounter the following error when trying to convert Numpy arrays to a pandas dataframe. Exception: Data must be 1-dimensional 1. Reproduce the Error Output: Exception: Data must be 1-dimensional 2. Why the Error Happens It happens because pd.DataFrame...
Read Full Article →
How to Fix: if using all scalar values, you must pass an index
This tutorial shows how to fix the error when using Pandas. if using all scalar values, you must pass an index You encounter this error because you are trying to create a dataframe with all scalar values, but without adding...
Read Full Article →
How to Combine Multiple Numpy Arrays into a Dataframe
This tutorial will show how you can combine multiple arrays (e.g., 2 arrays of X and Y) into a Pandas dataframe. The following summarizes the two methods. Method 1: pd.DataFrame ({‘X’:X,’Y’:Y}) Method 2: combined_array=np.column_stack((X,Y))pd.DataFrame(combined_array, columns = [‘X’,’Y’]) Two Examples of...
Read Full Article →
3 Examples for Numpy np.dot()
Np.dot() in Python Numpy generates the product of two arrays. Specifically, for np.dot(a, b), Situation 1: If both a and b are 1-D arrays, it is inner product of vectors. Situation 2: If both a and b are 2-D arrays, it is matrix multiplication. Using matmul or a @ b is preferred. Situation 3:...
Read Full Article →
Examples of Generating 2-D Numpy Array
This tutorial provides 3 methods to create 2-D Numpy Arrays in Python. The following is the key syntax for these 3 methods. After that, 3 Python Numpy code examples are provided. Method 1: np.array[[numbers in the first row ], [numbers...
Read Full Article →