我有以下代码用于使用optim()来查找beta0和beta1来最小化偏差之和但我收到以下错误我不知道我做错了什么:
sum.abs.dev<-function(beta=c(beta0,beta1),a,b)
{
total<-0
n<-length(b)
for (i in 1:n)
{
total <- total + (b[i]-beta[1]-beta[2]*a[i])
}
return(total)
}
tlad <- function(y = "farm", x = "land", data="FarmLandArea.csv")
{
dat <- read.csv(data)
#fit<-lm(dat$farm~dat$land)
fit<-lm(y~x,data=dat)
beta.out=optim(fit$coefficients,sum.abs.dev)
return(beta.out)
}
Run Code Online (Sandbox Code Playgroud)
这是错误和警告:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels In addition: Warning message:
In model.response(mf, "numeric") : NAs introduced by coercion
Run Code Online (Sandbox Code Playgroud)

有人可以指导如何解决此错误?我刚刚开始在Keras:
1 from keras.datasets import cifar10
2 from matplotlib import pyplot
3 from scipy.misc import toimage
4
5 (x_train, y_train), (x_test, y_test) = cifar10.load_data()
6 for i in range(0, 9):
7 pyplot.subplot(330 + 1 + i)
8 pyplot.imshow(toimage(x_train[i]))
9 pyplot.show()
10
11 import numpy
12 from keras.models import Sequential
13 from keras.layers import Dense
14 from keras.layers import Dropout
15 from keras.layers import Flatten
16 from keras.constraints import maxnorm
17 from keras.optimizers import SGD
18 from keras.layers.convolutional import Convolution2D
19 …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
# Device configuration
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
seed = 42
np.random.seed(seed)
torch.manual_seed(seed)
# split the dataset into validation and test sets
len_valid_set = int(0.1*len(dataset))
len_train_set = len(dataset) - len_valid_set
print("The length of Train set is {}".format(len_train_set))
print("The length of Test set is {}".format(len_valid_set))
train_dataset , valid_dataset, = torch.utils.data.random_split(dataset , [len_train_set, len_valid_set])
# shuffle and batch the datasets
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=8, shuffle=True, num_workers=4)
test_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=8, shuffle=True, num_workers=4)
print("LOADERS",
len(dataloader),
len(train_loader),
len(test_loader))
Run Code Online (Sandbox Code Playgroud)
火车组的长度是720
测试集的长度为80
装载机 …
我使用以下代码来查找memory文件中单词的出现次数,我得到了错误的结果.你能帮助我知道我错过了什么吗?
注1:问题是寻找"记忆"一词的确切出现!注意2:我已经意识到他们正在寻找"记忆"甚至是"记忆"之类的东西是不被接受的!那是我猜想引起混乱的部分.我试了一下"动作"这个词,正确答案是7!你也可以试试.
#names=scan("hamlet.txt", what=character())
names <- scan('http://pastebin.com/raw.php?i=kC9aRvfB', what=character())
Read 28230 items
> length(grep("memory",names))
[1] 9
Run Code Online (Sandbox Code Playgroud)
这是文件
我需要在不使用任何外部库的情况下解析如下的arff文件。我不确定如何将属性与数值关联起来。就像我怎么说每一行的第一个数字是年龄,而第二行的是性别?您还可以将我链接到一些用于解析类似情况的python代码吗?
@relation cleveland-14-heart-disease
@attribute 'age' real
@attribute 'sex' { female, male}
@attribute 'cp' { typ_angina, asympt, non_anginal, atyp_angina}
@attribute 'trestbps' real
@attribute 'chol' real
@attribute 'fbs' { t, f}
@attribute 'restecg' { left_vent_hyper, normal, st_t_wave_abnormality}
@attribute 'thalach' real
@attribute 'exang' { no, yes}
@attribute 'oldpeak' real
@attribute 'slope' { up, flat, down}
@attribute 'ca' real
@attribute 'thal' { fixed_defect, normal, reversable_defect}
@attribute 'class' { negative, positive}
@data
63,male,typ_angina,145,233,t,left_vent_hyper,150,no,2.3,down,0,fixed_defect,negative
37,male,non_anginal,130,250,f,normal,187,no,3.5,down,0,normal,negative
41,female,atyp_angina,130,204,f,left_vent_hyper,172,no,1.4,up,0,normal,negative
56,male,atyp_angina,120,236,f,normal,178,no,0.8,up,0,normal,negative
57,female,asympt,120,354,f,normal,163,yes,0.6,up,0,normal,negative
57,male,asympt,140,192,f,normal,148,no,0.4,flat,0,fixed_defect,negative
56,female,atyp_angina,140,294,f,left_vent_hyper,153,no,1.3,flat,0,normal,negative
44,male,atyp_angina,120,263,f,normal,173,no,0,up,0,reversable_defect,negative
52,male,non_anginal,172,199,t,normal,162,no,0.5,up,0,reversable_defect,negative
Run Code Online (Sandbox Code Playgroud)
这是我编写的示例代码:
arr=[]
arff_file = …Run Code Online (Sandbox Code Playgroud) 我有以下代码,我不知道为什么会收到此错误:
rm(list=ls())
require("XML")
# <a href="/music/The+Beatles/Sgt.+Pepper%27s+Lonely+Hearts+Club+Band"
beatles = "http://www.last.fm/music/The+Beatles/"
beatles.albums.page = paste(sep="", beatles, "+albums")
lines = readLines(beatles.albums.page)
album.lines = grep(pattern="href.*link-reference", lines, value=TRUE)
album.names = sub(pattern=".*<h3>(.*)</h3>.*", replacement="\\1", x=album.lines)
album.names = gsub(pattern=" ", replacement="+", x=album.names)
album.names = gsub(pattern="'", replacement="%27", x=album.names)
for (album in album.names[1]) {
print(album)
album.link = paste(sep="", beatles, album)
print(album.link)
tables = readHTMLTable(album.link)
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?
我不确切知道这个错误的来源是什么以及如何修复它.我通过运行此代码得到它.
Traceback (most recent call last):
File "t1.py", line 86, in <module>
write_results(results)
File "t1.py", line 34, in write_results
dw.writerows(results)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/csv.py", line 154, in writerows
return self.writer.writerows(rows)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
任何解释都非常感谢!
我更改了代码,现在我收到此错误:
File "t1.py", line 88, in <module>
write_results(results)
File "t1.py", line 35, in write_results
dw.writerows(results)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/csv.py", line 154, in writerows
return self.writer.writerows(rows)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) …Run Code Online (Sandbox Code Playgroud) 我使用 bc 和 scale 来评估表达式,但是我希望它向上舍入而不是向下舍入。什么是最简单的方法来做到这一点?
$ read exp
5+50*3/20 + (19*2)/7
$ echo "scale=3; $exp" | bc -l
17.928
Run Code Online (Sandbox Code Playgroud)
然而我想要的答案是 17.929
我更喜欢将答案作为我的答案的附录,而不是一些不同的东西。谢谢
以下是我尝试过的一些事情:
$ echo "scale=4; ($exp+0.0005)" | bc -l
17.9290
$ echo "scale=3; ($exp+0.0005)" | bc -l
17.9285
Run Code Online (Sandbox Code Playgroud)
但是我想17.929作为最后没有零的答案。
康达新手在这里!我怎么解决这个问题?
C:\Users\mona>conda create --name universe-starter-agent python=3.5
Fetching package metadata .........
Solving package specifications: ..........
Package plan for installation in environment C:\Users\mona\.conda\envs\universe-starter-agent:
The following packages will be downloaded:
package | build
---------------------------|-----------------
pip-9.0.1 | py35_1 1.7 MB
The following NEW packages will be INSTALLED:
pip: 9.0.1-py35_1
python: 3.5.2-0 (copy)
setuptools: 27.2.0-py35_1 (copy)
vs2015_runtime: 14.0.25123-0 (copy)
wheel: 0.29.0-py35_0 (copy)
Proceed ([y]/n)? y
Fetching packages ...
pip-9.0.1-py35 100% |###############################| Time: 0:00:00 2.94 MB/s
Extracting packages ...
WARNING conda.lock:touch(53): Failed to create lock, do …Run Code Online (Sandbox Code Playgroud) 我已经使用 github 页面上的指南安装了DeepPoseKit 。
我有以下tensorflow-gpu版本:
[jalal@goku examples]$ python -c 'import tensorflow as tf; print(tf.__version__)'
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = …Run Code Online (Sandbox Code Playgroud) python ×6
r ×3
keras ×2
tensorflow ×2
arff ×1
bash ×1
bc ×1
conda ×1
csv ×1
dataloader ×1
expression ×1
file ×1
function ×1
grep ×1
html ×1
last.fm ×1
optimization ×1
parsing ×1
python-3.x ×1
pytorch ×1
regex ×1
regression ×1
sh ×1
shell ×1
unicode ×1
web-scraping ×1
windows-10 ×1