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 to 1-D ones. Below is the example. Output: 2-D array: [[ 5 2 3 4 … Read more

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 is expecting to have 1-D numpy arrays or lists, since it is how columns within … Read more

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 index at the same time. Reproduces the Error Output: ValueError: If using all scalar values, … Read more

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 Combining Arrays into Dataframe Example for Method 1: In the following, we create two arrays, … Read more

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: If either a or b is 0-D (scalar), it is equivalent to multiply. Using np.multiply(a, b) or a * b is preferred. Example 1: … Read more

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 in the second row]] Method 2: np.zeros(shape(row number, column number)) Method 3: array_name=np.arange(length number)array_name=array_name.reshape(row number, … Read more

Numpy Checks it is a Scalar or an Array (Examples)

You can use the np.isscalar() to check whether a variable is a scalar or array. The following shows Python code examples checking it is a scalar or array. Example 1: A number Output: True Example 2: An array with one number Output: False Since it is not a scalar, we can check whether it is … Read more

How to Solve Linear Regression Using Linear Algebra (4 Steps)

We can solve linear regression (i.e., estimate the regression coefficients) using just linear algebra. Below is the process of 4 steps to do regression analysis via matrix multiplication. Step 1: Prepare the matrix We actually can expand the function above to another format below. The function below can give you a more detailed idea of … Read more

Use sklearn to Calculate SSR in Python

This tutorial shows how to use sklearn to calculate SSR, which stands for Sum of Squared Residuals. SSR is also known as residual sum of squares (RSS) or sum of squared errors (SSE). Steps of Using sklearn to Calculate SSR in Python Step 1: Prepare data We are going to use a built-in dataset called … Read more

How to Calculate Sum of Squared Residuals in Python

This tutorial shows how you calculate Sum of Squared Residuals in Python with detailed steps. Sum of Squared Residuals (SSR) is also known as residual sum of squares (RSS) or sum of squared errors (SSE). The following is the formula to calculate SSR. SSR can be used compare our estimated values and observed values for … Read more