pass 2D array to linear regression (sklearn)

2024/11/18 5:45:59

I want to pass 2D array to linear regression:

x = [[1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 3, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1, 1, 0, 0, 3]]
y = [3.9857,  3.6877, 3.6877]x = numpy.array(x)
y = numpy.array(y)model = LinearRegression(z,y).fit(z,y)

I am not using reshape (-1,1) as it makes the 2D array to 3D

But I am getting error:

ValueError: setting an array element with a sequence
TypeError: float() argument must be a string or a number, not 'list'

How can I correctly pass a two dimensional array to linear regression?

Answer

A bit long to type as a comment, so if you look at your x, before converting to a numpy array:

print([len(i) for i in x])
[36, 10, 10]

And y has length 3. It is ok to do a linear regression, but your independent variable needs to have the same number of variable, per observation.

In your case, the first element of list x should have 10 entries, like the others.

So for example:

import numpy as np
from sklearn import linear_model
clf = linear_model.LinearRegression()# take the first ten
exo = np.array([i[:10] for i in x])# fit
clf.fit(exo,np.array(y))
clf.coef_array([ 8.12727273e-02, -6.93889390e-18,  8.12727273e-02,  0.00000000e+00,0.00000000e+00, -2.70909091e-02,  5.41818182e-02,  8.12727273e-02,2.70909091e-02,  0.00000000e+00])

You get 10 coefficients, one for each column of x.

https://en.xdnf.cn/q/118711.html

Related Q&A

How do I fix this OverflowError?

I keep getting a "OverflowError: math range error". No matter what I input, the result is the same. Im running Python 3.3, and its finding the problem at the last line. How do I fix this? (A…

Pyinstaller subprocess.check_output error

Ive bundled my app with pyinstaller to 2 *.exegui_app.exe (onefile) config.ini \libs (onedir)winservice.exe+ all DLLs and libsWhen I manually install service with command winservice.exe install everyth…

Exception handler to check if inline script for variable worked

I need to add exception handling that considers if line 7 fails because there is no intersection between the query and array brands. Im new to using exception handlers and would appreciate any advice o…

Parameter list with single argument

When testing Python parameter list with a single argument, I found some weird behavior with print.>>> def hi(*x): ... print(x) ... >>> hi() () >>> hi(1,2) (1, 2) >>…

Scatter plot of values in pandas dataframe

I have a pandas dataframe in the following format. I am trying to plot this data based on ClusterAssigned, with probably different colors for 0 and 1. Distance ClusterAssigned23 135 120 …

String Delimiter in Python

I want to do split a string using "},{" as the delimiter. I have tried various things but none of them work.string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3 "Split it i…

Wrong encoding of email attachment

I have a python 2.7 script running on windows. It logs in gmail, checks for new e-mails and attachments:#!/usr/bin/env python # -*- coding: utf-8 -*-file_types = ["pdf", "doc", &quo…

Blank lines in txt files in Python

I want to write sensor values to a text file with Python. All is working fine but one thing; in the text file, there are blank lines between each value. Its really annoying because I cant put the value…

Python 3 - decode spectroscopy data (Base64, IEEE754)

Im a chemist and working with spectroscopic data that was stored as a list (501 pairs of X,Y data) of Base64-encoded floating point values according to IEEE754.I tried to get an array of X, Y data to w…

Fill new column by following conditions listed in a dictionary [duplicate]

This question already has an answer here:Insert data to new column based on conditions given in dictionary(1 answer)Closed 2 years ago.I have the dictionary specifying the value the row should take if …