小编Mer*_*nel的帖子

你如何使这个代码更pythonic?

你能告诉我如何使下面的代码更加pythonic吗?

代码是正确的.全面披露-它的问题1B在单张#4 机学习课程.我应该在两个数据集上使用牛顿算法来拟合逻辑假设.但是他们使用matlab而我正在使用scipy

例如我有一个问题是矩阵保持四舍五入到整数,直到我将一个值初始化为0.0.有没有更好的办法?

谢谢

import os.path
import math
from numpy import matrix
from scipy.linalg import inv #, det, eig

x = matrix( '0.0;0;1'  )
y = 11
grad = matrix( '0.0;0;0'  )
hess = matrix('0.0,0,0;0,0,0;0,0,0')
theta = matrix( '0.0;0;0'  ) 


# run until convergence=6or7
for i in range(1, 6):
  #reset
  grad = matrix( '0.0;0;0'  )
  hess = matrix('0.0,0,0;0,0,0;0,0,0')

  xfile = open("q1x.dat", "r")
  yfile = open("q1y.dat", "r")


  #over whole set=99 items  
  for i in range(1, 100):    
    xline = xfile.readline() …
Run Code Online (Sandbox Code Playgroud)

python machine-learning scipy

3
推荐指数
1
解决办法
749
查看次数

这可以更加pythonic?

我刚才遇到过这个(真的)简单的程序.它只输出第一个x素数.我很尴尬地问,有没有办法让它更"pythonic",即凝聚它,使其(更多)可读?切换功能很好; 我只对可读性感兴趣.

谢谢

from math import sqrt


def isprime(n):
  if n ==2:
    return True
  if n % 2 ==0 : # evens
    return False

  max = int(sqrt(n))+1 #only need to search up to sqrt n 
  i=3
  while i <= max: # range starts with 3 and for odd i
    if n % i == 0:
      return False
    i+=2

  return True



reqprimes = int(input('how many primes: '))
primessofar = 0
currentnumber = 2
while primessofar < reqprimes:

  result = isprime(currentnumber)

  if …
Run Code Online (Sandbox Code Playgroud)

python primes

2
推荐指数
3
解决办法
437
查看次数

弄清楚C循环

这是默认的gnome屏保之一 - "个人幻灯片".它显示来自某个位置的图片,在图片之间暂停约10秒.任何人都知道它是如何循环和暂停的?

我正在尝试增加延迟,但是我在犹豫是否添加sleep()而不知道它是如何做的.

谢谢

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>

#include "gs-theme-window.h"
#include "gs-theme-engine.h"
#include "gste-slideshow.h"

#include "xdg-user-dir-lookup.h"

int
main (int argc, char **argv)
{
        GSThemeEngine *engine;
        GtkWidget     *window;
        GError        *error;
        gboolean       ret;
        char          *location = NULL;
        char          *background_color = NULL;
        gboolean       sort_images = FALSE;
        gboolean       no_stretch = FALSE;
        GOptionEntry  entries [] = {
                { "location", 0, 0, G_OPTION_ARG_STRING, &location,
                  N_("Location to get images from"), N_("PATH") }, …
Run Code Online (Sandbox Code Playgroud)

c linux gtk

1
推荐指数
1
解决办法
460
查看次数

标签 统计

python ×2

c ×1

gtk ×1

linux ×1

machine-learning ×1

primes ×1

scipy ×1