gpt4 book ai didi

python - 在 Python 中使用 sklearn.preprocessing 进行数据转换

转载 作者:行者123 更新时间:2023-11-30 09:43:55 24 4
gpt4 key购买 nike

我使用 Python 和 sklearn 编写了多项式回归的代码。我使用预处理和 PolynomialFeatures 来转换数据。是否可以使用预处理并转换我的数据,以便我可以进行对数回归?我到处都找过了,但什么也没找到。这是多项式回归的代码,我的问题是,如何将此代码更改为对数回归:

import numpy as np

import pandas as pd
import math
import xlrd
from sklearn import linear_model
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import PolynomialFeatures


#Reading data from excel

data = pd.read_excel("DataSet.xls").round(2)
data_size = data.shape[0]
#print("Number of data:",data_size,"\n",data.head())

def polynomial_prediction_of_future_strength(input_data, cement, blast_fur_slug,fly_ash,
water, superpl, coarse_aggr, fine_aggr, days):

variables = prediction_accuracy(input_data)[2]
results = prediction_accuracy(input_data)[3]
n = results.shape[0]
results = results.values.reshape(n,1) #reshaping the values so that variables and results have the same shape

#transforming the data into polynomial function
Poly_Regression = PolynomialFeatures(degree=2)
poly_variables = Poly_Regression.fit_transform(variables)

#accuracy of prediction(splitting the dataset on train and test)
poly_var_train, poly_var_test, res_train, res_test = train_test_split(poly_variables, results, test_size = 0.3, random_state = 4)

input_values = [cement, blast_fur_slug, fly_ash, water, superpl, coarse_aggr, fine_aggr, days]
input_values = Poly_Regression.transform([input_values]) #transforming the data for prediction in polynomial function

regression = linear_model.LinearRegression() #making the linear model
model = regression.fit(poly_var_train, res_train) #fitting polynomial data to the model

predicted_strength = regression.predict(input_values) #strength prediction
predicted_strength = round(predicted_strength[0,0], 2)

score = model.score(poly_var_test, res_test) #accuracy prediction
score = round(score*100, 2)

accuracy_info = "Accuracy of concrete class prediction: " + str(score) + " %\n"
prediction_info = "Prediction of future concrete class after "+ str(days)+" days: "+ str(predicted_strength)

info = "\n" + accuracy_info + prediction_info

return info

#print(polynomial_prediction_of_future_strength(data, 214.9 , 53.8, 121.9, 155.6, 9.6, 1014.3, 780.6, 7))

最佳答案

如果您想实现平稳过渡,最好的方法是使用 scikit-learn 的风格定义您自己的估计器。您可以找到更多信息here .

这是一种可能性:

from sklearn.base import BaseEstimator, TransformerMixin

class LogarithmicFeatures(BaseEstimator, TransformerMixin):

def __init__(self):
pass

def fit(self, X, y=None):
self.n_features_ = X.shape[1]
return self

def transform(self, X, y=None):
if X.shape[1] != self.n_features_:
raise ValueError("X must have {:d} columns".format(self.n_features_))
return np.log(X)

然后您可以使用以下方法将其插入代码中:

lf = LogarithmicFeatures()
log_variables = lf.fit_transform(variables)

关于python - 在 Python 中使用 sklearn.preprocessing 进行数据转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55112074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com