Python Jupyter Notebook没有正确应用函数

时间:2018-04-16 00:20:12

标签: python jupyter-notebook

我有一个jupyter笔记本,我在其中使用logistic函数返回一个规范化的数组。

代码:

import math
import numpy as np

# takes a list of numbers as input
def logistic_transform(nums):
    e = math.e
    print(e)
    print(nums)
    for num in nums:
        num = 1 / 1 + (e ** num)
    return nums

input = [1, 2, 3]
test = logistic_transform(input)
print(test)

输出结果为:

2.718281828459045
[1, 2, 3]
[1, 2, 3]

为什么更改未应用于input[]中的值?

3 个答案:

答案 0 :(得分:1)

将结果放在另一个列表中!

  public static void main(String[] args) {
    HashMap<String, String> map = new HashMap<String, String>();

    try {
        File file = new File("test.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuffer stringBuffer = new StringBuffer();
        String line;
        String weightLimit = Files.readAllLines(Paths.get("test.txt")).get(0);
        int count = 0;
        while ((line = bufferedReader.readLine()) != null) {
            if (count != 0) { // ignore the first line
                String[] splitValue = line.split(" ");
                map.put(splitValue[0], splitValue[1]);
            }     
            count++;
        }
        fileReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//            System.out.println(entry.getKey());
//            System.out.println(entry.getValue());
//        }
   }

2.718281828459045

nums [1,2,3]

测试[3.718281828459045,8.389056098930649,21.085536923187664]

答案 1 :(得分:0)

您只需获取新列表,请尝试新列表:

    new_list = []
    for num in nuns:
        #do ur stuff
        new_list.append(num)
    return new_list

答案 2 :(得分:0)

首先,你的后勤功能没有意义, 应该是这样的,我想:

(1 / (1 + (e ** (-num)))

您正在做的是取出一个元素,将其保存在变量num中并更新变量num,而不是在列表nums中更新它。

使用列表理解:

nums = [(1 / (1 + (e ** (-num)))) for num in nums]

或更新列表

for num in range(len(nums)):
    nums[num] = 1 /(1 + (e ** (-nums[num])))

在这里,这应该可以正常工作:

import math
def logistic_transform(nums):
    e = math.e
    print(e)
    print(nums)
    nums = [(1 / (1 + (e ** (-num)))) for num in nums]
    return nums

input = [1, 2, 3]
test = logistic_transform(input)
print(test)