如何将数据输入keras?结构是什么?具体是什么是x_train和y_train,如果我有超过2列?
这是我想输入的数据:
我试图在这个例子中定义Xtrain,Keras在其文档中有多层感知器神经网络代码.(http://keras.io/examples/)这是代码:
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
model = Sequential()
model.add(Dense(64, input_dim=20, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(2, init='uniform'))
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd)
model.fit(X_train, y_train, nb_epoch=20, batch_size=16)
score = model.evaluate(X_test, y_test, batch_size=16)
Run Code Online (Sandbox Code Playgroud)
编辑(附加信息):
在这里:Python Keras深度学习包的数据类型是什么?
Keras使用包含theano.config.floatX浮点类型的numpy数组.这可以在.theanorc文件中配置.通常,对于CPU计算,它将是float64,对于GPU计算,它将是float32,但如果您愿意,也可以在处理CPU时将其设置为float32.您可以通过命令创建正确类型的零填充数组
X = numpy.zeros((4,3), dtype=theano.config.floatX)
Run Code Online (Sandbox Code Playgroud)
问题:步骤1看起来像使用excel文件中的上述数据创建一个浮点numpy数组.我如何处理获胜者专栏?
我正在使用Python,我需要将我的.csv导入数据分为两部分,一个是训练和测试集,EG 70%训练和30%测试.
我不断收到各种错误,比如'list' object is not callable等等.
这有什么简单的方法吗?
谢谢
编辑:
代码是基本的,我只是想分割数据集.
from csv import reader
with open('C:/Dataset.csv', 'r') as f:
data = list(reader(f)) #Imports the CSV
data[0:1] ( data )
Run Code Online (Sandbox Code Playgroud)
TypeError: 'list' object is not callable
我正在为数据科学课程的介绍开展一个数据科学项目,我们决定解决加利福尼亚海水淡化厂的一个问题:"我们应该把k植物放在哪里以最小化邮政编码的距离?"
我们到目前为止的数据是,拉链,城市,县,流行,拉特,长,水量.
问题是,我找不到任何关于如何迫使质心被限制在海岸上的资源.我们到目前为止所考虑的是:使用普通的kmeans算法,但是一旦聚类已经稳定,就将质心移动到海岸(坏)使用具有权重的正常kmeans算法,使得沿海拉链具有无限的权重(我已经告诉这不是一个很好的解决方案)
你们有什么感想?
试图使用金字塔的自动有马功能,却一无所获。
导入整个类:
import pyramid
stepwise_fit = auto_arima(df.Weighted_Price, start_p=0, start_q=0, max_p=10, max_q=10, m=1,
start_P=0, seasonal=True, trace=True,
error_action='ignore', # don't want to know if an order does not work
suppress_warnings=True, # don't want convergence warnings
stepwise=True) # set to stepwise
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
NameError: name 'auto_arima' is not defined
Run Code Online (Sandbox Code Playgroud)
很好,然后让我们从金字塔导入该特定程序包。
from pyramid.arima import auto_arima
Run Code Online (Sandbox Code Playgroud)
-------------------------------------------------- ------------------------- RuntimeError Traceback(最近一次调用最近)RuntimeError:针对API版本0xb编译的模块,但此版本的numpy为0xa
-------------------------------------------------- ------------------------- ImportError Traceback((last last最近调用)in()1#试图导入金字塔----> 2从金字塔.arima导入auto_arima
/usr/local/lib/python2.7/site-packages/pyramid/arima/ init .py in()3#作者:Taylor Smith 4 ----> 5 from .approx import * 6 from .arima import * 7来自.auto import * …
我的一般问题是我有一个数据框,其中的列与要素值相对应。数据框中还有一个日期列。每个功能列可能缺少NaN值。我想用诸如“ fill_mean”或“ fill zero”的填充逻辑填充一列。
但是我不想只将填充逻辑应用于整个列,因为如果较早的值之一是NaN,则我不希望此特定NaN的平均值被后来的平均值所污染。该模型应该没有任何知识。从本质上讲,这是不向模型泄漏有关未来信息的普遍问题,尤其是在尝试填充我的时间序列时。
无论如何,我已经将问题简化为几行代码。这是我对上述一般问题的简化尝试:
#assume ts_values is a time series where the first value in the list is the oldest value and the last value in the list is the most recent.
ts_values = [17.0, np.NaN, 12.0, np.NaN, 18.0]
nan_inds = np.argwhere(np.isnan(ts_values))
for nan_ind in nan_inds:
nan_ind_value = nan_ind[0]
ts_values[nan_ind_value] = np.mean(ts_values[0:nan_ind_value])
Run Code Online (Sandbox Code Playgroud)
上面脚本的输出是:
[17.0, 17.0, 12.0, 15.333333333333334, 18.0]
Run Code Online (Sandbox Code Playgroud)
这正是我所期望的。
我唯一的问题是,相对于数据集中NaN的数量,它将是线性时间。有没有办法在常量或日志时间内执行此操作,而我不会遍历nan索引值。
shapley 支持逻辑回归模型吗?
运行以下代码我得到:
logmodel = LogisticRegression()
logmodel.fit(X_train,y_train)
predictions = logmodel.predict(X_test)
explainer = shap.TreeExplainer(logmodel )
Exception: Model type not yet supported by TreeExplainer: <class 'sklearn.linear_model.logistic.LogisticRegression'>
Run Code Online (Sandbox Code Playgroud)
PS 你应该对不同的模型使用不同的解释器
python machine-learning logistic-regression data-science shap
主要问题:为什么 Lasso 回归的系数不会通过 最小化而缩小到零scipy.minimize?
我正在尝试使用 scipy.minimize 创建套索模型。然而,它仅在 alpha 为零时才起作用(因此仅像基本平方误差一样)。当 alpha 不为零时,它会返回更差的结果(更高的损失),并且仍然没有一个系数为零。
我知道 Lasso 是不可微分的,但我尝试使用 Powell 优化器,它应该处理非微分损失(我也尝试过 BFGS,它应该处理非平滑)。这些优化器都不起作用。
为了测试这一点,我创建了数据集,其中 y 是随机的(此处提供是可重现的),X 的第一个特征恰好是 y*.5,其他四个特征是随机的(此处也提供是可重现的)。我希望算法将这些随机系数缩小到零并只保留第一个系数,但它没有发生。
对于套索损失函数,我使用本文中的公式(图 1,第一页)
我的代码如下:
from scipy.optimize import minimize
import numpy as np
class Lasso:
def _pred(self,X,w):
return np.dot(X,w)
def LossLasso(self,weights,X,y,alpha):
w = weights
yp = self._pred(X,w)
loss = np.linalg.norm(y - yp)**2 + alpha * np.sum(abs(w))
return loss
def fit(self,X,y,alpha=0.0):
initw = np.random.rand(X.shape[1]) #initial weights
res = minimize(self.LossLasso,
initw,
args=(X,y,alpha),
method='Powell')
return res
if __name__=='__main__': …Run Code Online (Sandbox Code Playgroud) machine-learning lasso-regression scipy data-science loss-function
我在安装惊喜包时使用以下命令。我在安装时收到错误消息,但我无法理解。我需要帮助才能成功安装此软件包。 pip 安装 scikit-惊喜
最后一个错误代码表示需要 Microsoft Visual C++ 14 或更高版本,但我安装了 14 版本,因此应该满足要求。 控制面板的屏幕截图
(base) C:\Users\S Vishal>pip install scikit-surprise
Collecting scikit-surprise
Using cached scikit-surprise-1.1.1.tar.gz (11.8 MB)
Requirement already satisfied: joblib>=0.11 in c:\programdata\anaconda3\lib\site-packages (from scikit-surprise) (0.17.0)
Requirement already satisfied: numpy>=1.11.2 in c:\programdata\anaconda3\lib\site-packages (from scikit-surprise) (1.19.2)
Requirement already satisfied: scipy>=1.0.0 in c:\programdata\anaconda3\lib\site-packages (from scikit-surprise) (1.5.2)
Requirement already satisfied: six>=1.10.0 in c:\programdata\anaconda3\lib\site-packages (from scikit-surprise) (1.15.0)
Building wheels for collected packages: scikit-surprise
Building wheel for scikit-surprise (setup.py) ... error
ERROR: Command errored out with exit …Run Code Online (Sandbox Code Playgroud) 我使用 PyTorch 数据集类开发了一个自定义数据集。代码是这样的:
class CustomDataset(torch.utils.data.Dataset):
def __init__(self, root_path, transform=None):
self.path = root_path
self.mean = mean
self.std = std
self.transform = transform
self.images = []
self.masks = []
for add in os.listdir(self.path):
# Some script to load file from directory and appending address to relative array
...
self.masks.sort()
self.images.sort()
def __len__(self):
return len(self.images)
def __getitem__(self, item):
image_address = self.images[item]
mask_address = self.masks[item]
if self.transform is not None:
augment = self.transform(image=np.asarray(Image.open(image_address, 'r', None)),
mask=np.asarray(Image.open(mask_address, 'r', None)))
image = Image.fromarray(augment['image'])
mask = augment['mask'] …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 datafarame 转换为所需的输出格式,并满足下面提到的要求。
提供的要求:
例子:
输入:
| 指数 | 模式 | 柱子 | Trh1 | Trh2 | Trh3 | Trh4 |
|---|---|---|---|---|---|---|
| 0 | 架构_1 | 列_1 | 南 | 0.01 | 南 | 南 |
| 1 | 架构_2 | 列_2 | 0.02 | 0.03 | 南 | 南 |
| 2 | 架构_3 | 列_3 | 0.03 | 0.04 | 0.05 | 南 |
| 3 | 架构_4 | 列_4 | 南 | 南 | 0.06 | 0.07 |
预期输出:
| 指数 | 模式 | 柱子 | Trh1 | Trh2 | Trh3 | Trh4 |
|---|---|---|---|---|---|---|
| 0 | 架构_1 | 列_1 | 南 | 0.01 | 南 | 南 |
| 1 | 架构_2 | 列_2 | 0.02 | 0.03 | 南 | 南 |
| 2 | 架构_3 | 列_3 | 0.03 | 0.04 … |