简体   繁体   English

如何将图像文件从URL转换为scikit-learn中可用于执行多元线性回归的格式

[英]how to convert an image file from a URL to a format in scikit-learn that can be used to perform a Multivariate Linear Regression

I am trying to do a simple multivatiate Linear Regressions where one of the features columns are images. 我正在尝试做一个简单的多元线性回归,其中一个特征列是图像。 I currently have the URL of each image. 我目前有每个图像的URL。 How do I convert this URL column into a format that scikit learn will understand(a numerical version of a picture)? 如何将此URL列转换为scikit learn将理解的格式(图片的数字版本)?

import pandas as pd
import numpy as np
from sklearn import linear_model

df = pd.read_csv('dtaset.csv')
df.head()

index followers  likes      urls
  0   14928252   132771   https://url1
  1   14928252   57671    https://url2
  2   14928252   161580   https://url3
  3   14928252   60969    https://url4
  4   14928252   375567   https://url5

The Feature columns are 'total_followers' and 'urls' and the target column is "likes" 功能列是'total_followers'和'urls',目标列是“赞”

Anyone? 任何人? Thanks! 谢谢!

Your question has an easy part and a hard part. 你的问题有一个简单的部分和困难的部分。 The easy one is how to read an image from URL. 简单的方法是如何从URL读取图像。 For this you will need OpenCV. 为此,您将需要OpenCV。 If you don't have it ( import cv2 fails), install it, eg using pip install opencv-python . 如果你没有它( import cv2失败),安装它,例如使用pip install opencv-python Than you can use this function: 比你可以使用这个功能:

import cv2
import requests
def readImage(url, color = False):
    with requests.get(url) as r:
        return cv2.imdecode(np.frombuffer(r.content, np.uint8), cv2.IMREAD_COLOR if color else cv2.IMREAD_GRAYSCALE)

And the hard question is what you will do with the images? 困难的问题是你将如何处理图像? An image is (usually) a large array of numbers, most probably, you will need to extract some characteristics and features from it in order to use them in a linear regression. 图像(通常)是一大堆数字,最有可能的是,您需要从中提取一些特征和特征,以便在线性回归中使用它们。 But it depends on the nature of your task. 但这取决于你的任务的性质。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM